www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Overloading templates

reply Sean Kelly <sean invisibleduck.org> writes:
I'm inclined to say this isn't possible, but just in case I'm wrong, can someone
tell me how to make this code work:

    size_t find(Buf, Pat)( Buf buf, Pat pat )
    {
        return 0;
    }

    template find( char[] exp )
    {
        size_t find(Buf, Pat)( Buf buf, Pat pat )
        {
            return 0;
        }
    }

    void main()
    {
        find( "abc", 'a' );
        find!("a == b")( "abc", 'a' );
    }

Basically, I want a template function to overload with another template
function that I can partially specialize.


Sean
Sep 24 2008
next sibling parent reply "Bill Baxter" <wbaxter gmail.com> writes:
On Thu, Sep 25, 2008 at 10:09 AM, Sean Kelly <sean invisibleduck.org> wrote:
 I'm inclined to say this isn't possible, but just in case I'm wrong, can
someone
 tell me how to make this code work:

    size_t find(Buf, Pat)( Buf buf, Pat pat )
    {
        return 0;
    }

    template find( char[] exp )
    {
        size_t find(Buf, Pat)( Buf buf, Pat pat )
        {
            return 0;
        }
    }

    void main()
    {
        find( "abc", 'a' );
        find!("a == b")( "abc", 'a' );
    }

 Basically, I want a template function to overload with another template
 function that I can partially specialize.
Before anyone tries to answer this --Are you still in D2 land? --bb
Sep 24 2008
parent Sean Kelly <sean invisibleduck.org> writes:
Bill Baxter wrote:
 On Thu, Sep 25, 2008 at 10:09 AM, Sean Kelly <sean invisibleduck.org> wrote:
 I'm inclined to say this isn't possible, but just in case I'm wrong, can
someone
 tell me how to make this code work:

    size_t find(Buf, Pat)( Buf buf, Pat pat )
    {
        return 0;
    }

    template find( char[] exp )
    {
        size_t find(Buf, Pat)( Buf buf, Pat pat )
        {
            return 0;
        }
    }

    void main()
    {
        find( "abc", 'a' );
        find!("a == b")( "abc", 'a' );
    }

 Basically, I want a template function to overload with another template
 function that I can partially specialize.
Before anyone tries to answer this --Are you still in D2 land?
I'd prefer something that works with D1 as well, but I'll take what I can get. Sean
Sep 24 2008
prev sibling parent reply "Bill Baxter" <wbaxter gmail.com> writes:
On Thu, Sep 25, 2008 at 10:09 AM, Sean Kelly <sean invisibleduck.org> wrote:
 I'm inclined to say this isn't possible, but just in case I'm wrong, can
someone
 tell me how to make this code work:

    size_t find(Buf, Pat)( Buf buf, Pat pat )
    {
        return 0;
    }

    template find( char[] exp )
    {
        size_t find(Buf, Pat)( Buf buf, Pat pat )
        {
            return 0;
        }
    }

    void main()
    {
        find( "abc", 'a' );
        find!("a == b")( "abc", 'a' );
    }

 Basically, I want a template function to overload with another template
 function that I can partially specialize.
This works in D1 and D2. Requires find!()("abc",'a') as calling syntax, though. That's the closest I was able to come up with. template find() { size_t find(Buf, Pat)( Buf buf, Pat pat ) { return 0; } } template find( string exp ) { size_t find(Buf, Pat)( Buf buf, Pat pat ) { return 0; } } void main() { find!()( "abc", 'a' ); find!("a==b")( "abc", 'a' ); } --bb
Sep 24 2008
parent reply Sean Kelly <sean invisibleduck.org> writes:
== Quote from Bill Baxter (wbaxter gmail.com)'s article
 On Thu, Sep 25, 2008 at 10:09 AM, Sean Kelly <sean invisibleduck.org> wrote:
 I'm inclined to say this isn't possible, but just in case I'm wrong, can
someone
 tell me how to make this code work:

    size_t find(Buf, Pat)( Buf buf, Pat pat )
    {
        return 0;
    }

    template find( char[] exp )
    {
        size_t find(Buf, Pat)( Buf buf, Pat pat )
        {
            return 0;
        }
    }

    void main()
    {
        find( "abc", 'a' );
        find!("a == b")( "abc", 'a' );
    }

 Basically, I want a template function to overload with another template
 function that I can partially specialize.
This works in D1 and D2. Requires find!()("abc",'a') as calling syntax, though. That's the closest I was able to come up with. template find() { size_t find(Buf, Pat)( Buf buf, Pat pat ) { return 0; } } template find( string exp ) { size_t find(Buf, Pat)( Buf buf, Pat pat ) { return 0; } } void main() { find!()( "abc", 'a' ); find!("a==b")( "abc", 'a' ); }
Weird, that works for me with D1 but not D2. I'm beginning to think that my D2 installation is broken. Sean
Sep 25 2008
parent reply Sean Kelly <sean invisibleduck.org> writes:
Okay, it turns out that this works for me in D1 as well:

    template find(alias equals)
    {
        size_t find(Buf, Pat)( Buf buf, Pat pat )
        {
            return 0;
        }
    }

    template find(char[] equals = "a == b")
    {
        size_t find(Buf, Pat)( Buf buf, Pat pat )
        {
            return 0;
        }
    }

    void main()
    {
        find!()( "abc", 'a' ); // compiles in D1
        find( "abc", 'a' ); // doesn't compile in D1
    }

However, it doesn't work in D2.  There, basically the only syntax I can
find that works is what's used in std.algorithm:

    size_t find(alias equals = "a == b", Buf, Pat)( Buf buf, Pat pat )
    {
        return 0;
    }

    void main()
    {
        bool equals(char a, char b) { return a == b; }

        find( "abc", 'a' );
        find!("a == b")( "abc", 'a' );
        find!(equals)( "abc", 'a' );
    }

This is really ideal, but the fact that I can't find any common syntax
for D1 and D2 is not.  D1 doesn't allow aliasing strings but does
seem to support some degree of overloading (even if doing so requires
supplying an empty template parameter list), while D2 does allow
aliasing strings but I can't get even that shoddy form of overloading
to work.

I was really hoping to find some way to allow tango.core.Array to use
both its current delegate syntax and the new string syntax, but as it
stands I don't think this is possible if the code must be portable
between D1 and D2.


Sean
Sep 25 2008
parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 25 Sep 2008 22:12:16 +0400, Sean Kelly <sean invisibleduck.org>  
wrote:

 Okay, it turns out that this works for me in D1 as well:

     template find(alias equals)
     {
         size_t find(Buf, Pat)( Buf buf, Pat pat )
         {
             return 0;
         }
     }

     template find(char[] equals = "a == b")
     {
         size_t find(Buf, Pat)( Buf buf, Pat pat )
         {
             return 0;
         }
     }

     void main()
     {
         find!()( "abc", 'a' ); // compiles in D1
         find( "abc", 'a' ); // doesn't compile in D1
     }

 However, it doesn't work in D2.  There, basically the only syntax I can
 find that works is what's used in std.algorithm:

     size_t find(alias equals = "a == b", Buf, Pat)( Buf buf, Pat pat )
     {
         return 0;
     }

     void main()
     {
         bool equals(char a, char b) { return a == b; }

         find( "abc", 'a' );
         find!("a == b")( "abc", 'a' );
         find!(equals)( "abc", 'a' );
     }

 This is really ideal, but the fact that I can't find any common syntax
 for D1 and D2 is not.  D1 doesn't allow aliasing strings but does
 seem to support some degree of overloading (even if doing so requires
 supplying an empty template parameter list), while D2 does allow
 aliasing strings but I can't get even that shoddy form of overloading
 to work.

 I was really hoping to find some way to allow tango.core.Array to use
 both its current delegate syntax and the new string syntax, but as it
 stands I don't think this is possible if the code must be portable
 between D1 and D2.


 Sean
Tango/D1 ought to be frozen and support dropped in favor of Tango/D2 at some point, but it is far from now, I think.
Sep 25 2008
parent Sean Kelly <sean invisibleduck.org> writes:
== Quote from Denis Koroskin (2korden gmail.com)'s article
 Tango/D1 ought to be frozen and support dropped in favor of Tango/D2 at
 some point, but it is far from now, I think.
Yeah, the issue for me now is finding a way to provide for a smooth transition. I'd love to start using some of the more advanced compile-time features, but I've been running into quirks that foils nearly all of these attempts, as this example suggests. I'm coming to the conclusion that users of D1 should really just stick to the basics and worry about making things "D2-like" once they're no longer using D1. Sean
Sep 25 2008