digitalmars.D - AA specialization not works
- JDavidLS (11/11) Sep 22 2007 string[] na = ["yo","tu","el"];
- Stewart Gordon (19/28) Sep 23 2007 At it happens, what you've posted isn't valid because the statements are...
- JdavidLS (85/85) Sep 24 2007 // an example for template context code:
-
Stewart Gordon
(23/37)
Sep 24 2007
"JdavidLS"
wrote in message
string[] na = ["yo","tu","el"]; string[string] aa; aa["yo"] = "qwer"; aa["tu"] = "qwer"; ... string toString(T)(Tt) { ...} //OK string toString(T:T[])(Tt) { ... } // WORKS string toString(T:T[C], C)(Tt) { ... } // NOT WORKS string toString(T:V[C], V,C)(Tt) { ... } // NOT WORKS string toString(V,C,T:V[C])(Tt) { ... } // NOT WORKS As I can use the type of key of an associative Array in a function?
Sep 22 2007
"JDavidLS" <jdavidls gmail.com> wrote in message news:fd44p2$h7t$1 digitalmars.com...string[] na = ["yo","tu","el"]; string[string] aa; aa["yo"] = "qwer"; aa["tu"] = "qwer";At it happens, what you've posted isn't valid because the statements aren't in a function. But if I wrap these statements in a main function (and add dummy code to the function bodies), then the code you've given compiles (and seems to run) without error.string toString(T)(Tt) { ...} //OK string toString(T:T[])(Tt) { ... } // WORKS string toString(T:T[C], C)(Tt) { ... } // NOT WORKS string toString(T:V[C], V,C)(Tt) { ... } // NOT WORKS string toString(V,C,T:V[C])(Tt) { ... } // NOT WORKS<snip> You've given these functions, but no code that tries to call these functions. Hence nobody can reproduce your problem. Please post minimal but complete, self-contained examples. It makes it a lot easier for everybody, including you. And don't just write "NOT WORKS", state what it's doing instead of working. The best practice is to post a code file together with the compiler (or runtime) output for that file. See also http://homepage1.nifty.com/algafield/sscce.html Stewart. -- My e-mail address is valid but not my primary mailbox. Please keep replies on the 'group where everybody may benefit.
Sep 23 2007
// an example for template context code: /* _ArraySpec template generates const info using specialization of undetermined type of Array. Excuse my poor english. */ // Specialization for static array: /* template _ArraySpec(A: A[C], const uint C) // Error: variable C foward referenced { const length = C; alias typeof(A[0]) Type; alias uint Key; }*/ // Specialization for dinamic array: template _ArraySpec(A: A[]) { alias A.length length; alias typeof(A[0]) Type; alias uint Key; } // Specialization for AArray: template _ArraySpec(A: A[C], C) { alias A.legth legth; alias typeof(A[0]) Type; alias C Key; } // Specialization for Non Array: template _ArraySpec(T) { const length = 0; alias T Type; alias void Key; } typedef uint MyType; unittest { MyType static_array[100]; MyType dinamic_array[] = new MyType[100]; MyType asoc_array[string]; MyType non_array; // error: does not match any template declaration: alias _ArraySpec!(static_array) _staticArray; alias _ArraySpec!(dinamic_array) _dinamicArray; alias _ArraySpec!(asoc_array) _asocArray; alias _ArraySpec!(non_array) _nonArray; writefln( _staticArray.length, typeid(_staticArray.Type), typeid(_staticArray.Type) ); writefln( _dinamicArray.length, typeid(_dinamicArray.Type), typeid(_dinamicArray.Type) ); writefln( _asocArray.length, typeid(_asocArray.Type), typeid(_asocArray.Type) ); writefln( _nonArray.length, typeid(_nonArray.Type), typeid(_nonArray.Type) ); // 100 MyType uint // 100 MyType uint // 100 MyType string // 0 MyType void // NOT WORKS!!. // other example for IS context code void arraySpec(T)(T array) { // from: http://www.digitalmars.com/d/expression.html IS documentation. // for static array: static if(is(T A: A[K], const uint K)) // Error: found',' when expecting ')' { wintwfln(K, typeid(T[0]), typeid(uint)); // length, Type, Key } // for asoc array else static if(is(T A: A[K], K)) // ??? not satisficed for asoc array!!! { wintwfln(array.legth, typeid(T[0]), typeid(K)); // length, Type, Key } // for dinamic array else static if(is(T A: A[])) // This satisficed only for dinamic & static array types. { wintwfln(array.legth, typeid(T[0]), typeid(uint)); // length, Type, Key } else wintwfln(0, typeid(T), typeid(void)); // 0, Type, void } arraySpec(static_array); arraySpec(dinamic_array); arraySpec(asoc_array); arraySpec(non_array); // 100 MyType uint // 100 MyType uint // 100 MyType string // 0 MyType void // NOT WORKS!!!!! PLEASE HELP ME!!! }
Sep 24 2007
"JdavidLS" <jdavidls gmail.com> wrote in message news:fd9frq$2rtg$1 digitalmars.com... <snip>template _ArraySpec(A: A[C], const uint C) // Error: variable C foward referencedYes, DMD has lots of bugs like that. <snip>// Specialization for dinamic array: template _ArraySpec(A: A[]) { alias A.length length; alias typeof(A[0]) Type; alias uint Key; }<snip>// error: does not match any template declaration: alias _ArraySpec!(static_array) _staticArray;Your template takes as parameter a type, not a variable. But if I change it to alias _ArraySpec!(typeof(static_array)) _staticArray; then it still fails, because A.length becomes uint.length, and there's no such thing. I'm not sure if there's an easy way to do what you're trying to do. You might need to depart from template specialisations and use static ifs instead. It's a shame we don't seem to have ITTI (implicit template template instantiation). <snip>// for static array: static if(is(T A: A[K], const uint K)) // Error: found',' when expecting ')'<snip> Which version of (DM)D are you using? You seem to be using D 2.x-specific syntax, which won't work in a D 1.x compiler. Stewart. -- My e-mail address is valid but not my primary mailbox. Please keep replies on the 'group where everybody may benefit.
Sep 24 2007