www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1597] New: It is not possible to specialize template on associative array.

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1597

           Summary: It is not possible to specialize template on associative
                    array.
           Product: D
           Version: 1.022
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: aarti interia.pl


void parse(T)() {
}

//What to write here???
void parse(T : ?????)() {
}

void main() {
     parse!(int[char[]])();
}


-- 
Oct 19 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1597






Also syntax of matching for normal arrays is cryptic. It would be better to
have following syntax for matching arrays:
void parse(T : E[])() {} which is much more clear (match types which are
collections of elements of type E). Similarly some cleanups could be done in
is() expression for types.

proposed syntax for associative arrays:
void parse(T : V[K])() {}

Related proposal from Bill Baxter:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=9858


-- 
Oct 19 2007
prev sibling next sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1597






While acknowledging this as a bug in the language, below is a fix for the time
being:

import std.stdio;

void parse(T, U = void, V = void)() {
    writeln("Wuddever");
}

void parse(T : U[V], U, V)() {
    writeln("Hash");
}

void main() {
    parse!(int[char[]])();
    parse!(int[])();
    parse!(int)();
}

The second suggestion:

void parse(T : E[])() {}

won't work because it's unclear whether E is a newly-introduced symbol ("must
be inferred") or a previously-defined symbol ("must be E that I defined in this
module").


-- 
Oct 19 2007
parent Marcin Kuszczak <aarti_nospam please_interia.pl> writes:
 The second suggestion:
 
 void parse(T : E[])() {}
 
 won't work because it's unclear whether E is a newly-introduced symbol
 ("must be inferred") or a previously-defined symbol ("must be E that I
 defined in this module").
Probably your are right :-) That was more pointing problem, that proposing real solution. Maybe some kind of suggestion. But in other hand probably you will admit that current solution: void parse(T : T[]) is even worse? Inside function you get something completely different than you put when instantiating template. Inside function T means element of array, while it is instantiated as T[]. PS. It seems that best way to talk with Andrei is to submit bug :D
Oct 19 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1597







 won't work because it's unclear whether E is a newly-introduced symbol ("must
 be inferred") or a previously-defined symbol ("must be E that I defined in this
 module").
What's wrong with the C++ way of specifying specializations? Just void parse(T[])() {} --
Oct 19 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1597








 won't work because it's unclear whether E is a newly-introduced symbol ("must
 be inferred") or a previously-defined symbol ("must be E that I defined in this
 module").
What's wrong with the C++ way of specifying specializations? Just void parse(T[])() {}
It has the same problem. Consider: // wanna specialize on hashes, and also pattern match T and U! Cool! void parse(T[U])() {} Now if the same module adds or imports a type called U, a bad time is being had. --
Oct 19 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1597







But in other hand probably you will admit that current solution:
void parse(T : T[]) 

is even worse? 

Inside function you get something completely different than you put when
instantiating template. Inside function T means element of array, while it
is instantiated as T[].

So maybe:
void parse(T : auto E[]) {} and
void parse(T : auto V[K]) {}
to say that these symbols are inferred?


-- 
Oct 19 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1597








 But in other hand probably you will admit that current solution:
 void parse(T : T[]) 
 
 is even worse? 
 
 Inside function you get something completely different than you put when
 instantiating template. Inside function T means element of array, while it
 is instantiated as T[].
That's very ugly but Walter cannot be convinced to give it up.
 So maybe:
 void parse(T : auto E[]) {} and
 void parse(T : auto V[K]) {}
 to say that these symbols are inferred?
This idea has been iterated a number of times. In the end, there was admission that the current syntax of appending the symbols to the list is as good as any other. --
Oct 19 2007