www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - getting keytype/valuetype in array

reply Carlos Santander <csantander619 gmail.com> writes:
David Medlock recently posted a code for mixin in opApply, opIndex and 
opIndexAssign. Having done the same code myself, a question popped to my head. 
Notice how the mixin is defined and then used:

template applyThis( K, V, alias var )
{ ... }

class MyWidgets
{
     Widget[char[]]    lookup;
     mixin applyThis!( char[], Widget, lookup );
}

My question is, could there be a way for getting K and V from var? Maybe 
something like this:

template applyThis( alias var )
{
     alias var.keytype K;
     alias var.valuetype V;
     ...
}

Which could then be used in a simpler way:

     mixin applyThis!(lookup);

These two properties could be defined for all arrays, not just AAs. Also, if 
someone wanted to define her own collection, she'd have to also define those 
types/properties. What do you guys think?

-- 
Carlos Santander Bernal
Aug 31 2005
next sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
The idea is close to implicit template instatiation. I can't help but  
think it could somehow be combined with that, if/when we get it.

Regan

On Wed, 31 Aug 2005 22:26:20 -0500, Carlos Santander  
<csantander619 gmail.com> wrote:
 David Medlock recently posted a code for mixin in opApply, opIndex and  
 opIndexAssign. Having done the same code myself, a question popped to my  
 head. Notice how the mixin is defined and then used:

 template applyThis( K, V, alias var )
 { ... }

 class MyWidgets
 {
      Widget[char[]]    lookup;
      mixin applyThis!( char[], Widget, lookup );
 }

 My question is, could there be a way for getting K and V from var? Maybe  
 something like this:

 template applyThis( alias var )
 {
      alias var.keytype K;
      alias var.valuetype V;
      ...
 }

 Which could then be used in a simpler way:

      mixin applyThis!(lookup);

 These two properties could be defined for all arrays, not just AAs.  
 Also, if someone wanted to define her own collection, she'd have to also  
 define those types/properties. What do you guys think?
Aug 31 2005
parent reply Carlos Santander <csantander619 gmail.com> writes:
Regan Heath escribió:
 The idea is close to implicit template instatiation. I can't help but  
 think it could somehow be combined with that, if/when we get it.
 
 Regan
 
 On Wed, 31 Aug 2005 22:26:20 -0500, Carlos Santander  
 <csantander619 gmail.com> wrote:
 
 David Medlock recently posted a code for mixin in opApply, opIndex 
 and  opIndexAssign. Having done the same code myself, a question 
 popped to my  head. Notice how the mixin is defined and then used:

 template applyThis( K, V, alias var )
 { ... }

 class MyWidgets
 {
      Widget[char[]]    lookup;
      mixin applyThis!( char[], Widget, lookup );
 }

 My question is, could there be a way for getting K and V from var? 
 Maybe  something like this:

 template applyThis( alias var )
 {
      alias var.keytype K;
      alias var.valuetype V;
      ...
 }

 Which could then be used in a simpler way:

      mixin applyThis!(lookup);

 These two properties could be defined for all arrays, not just AAs.  
 Also, if someone wanted to define her own collection, she'd have to 
 also  define those types/properties. What do you guys think?
I agree it's a bit related, but not fully: with implicit template instantiation, the compiler has to deduce the types from the parameters, but what I'm proposing here is adding a couple of properties to arrays. Maybe a better idea would be like this: template applyThis( alias var ) { alias typeof(var).keytype K; alias typeof(var).valuetype V; ... } So you could do: (int[]).keytype key; //key would be size_t (char[][]).valuetype value; //value would be char[] typeof(value).valuetype anotherKey; //anotherValue would be char -- Carlos Santander Bernal
Sep 01 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
On Thu, 01 Sep 2005 18:58:49 -0500, Carlos Santander  
<csantander619 gmail.com> wrote:
 Regan Heath escribió:
 The idea is close to implicit template instatiation. I can't help but   
 think it could somehow be combined with that, if/when we get it.
  Regan
  On Wed, 31 Aug 2005 22:26:20 -0500, Carlos Santander   
 <csantander619 gmail.com> wrote:

 David Medlock recently posted a code for mixin in opApply, opIndex  
 and  opIndexAssign. Having done the same code myself, a question  
 popped to my  head. Notice how the mixin is defined and then used:

 template applyThis( K, V, alias var )
 { ... }

 class MyWidgets
 {
      Widget[char[]]    lookup;
      mixin applyThis!( char[], Widget, lookup );
 }

 My question is, could there be a way for getting K and V from var?  
 Maybe  something like this:

 template applyThis( alias var )
 {
      alias var.keytype K;
      alias var.valuetype V;
      ...
 }

 Which could then be used in a simpler way:

      mixin applyThis!(lookup);

 These two properties could be defined for all arrays, not just AAs.   
 Also, if someone wanted to define her own collection, she'd have to  
 also  define those types/properties. What do you guys think?
I agree it's a bit related, but not fully: with implicit template instantiation, the compiler has to deduce the types from the parameters, but what I'm proposing here is adding a couple of properties to arrays. Maybe a better idea would be like this: template applyThis( alias var ) { alias typeof(var).keytype K; alias typeof(var).valuetype V; ... } So you could do: (int[]).keytype key; //key would be size_t (char[][]).valuetype value; //value would be char[] typeof(value).valuetype anotherKey; //anotherValue would be char
I understand your proposal. My comment was that I think implicit template instantiation is a better way to get what you want. I imagined something like "Chris Sauls" proposed in this thread. Of course your proposal allows other things as well, as you've shown here, so it may be useful to have both. Regan
Sep 01 2005
prev sibling next sibling parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"Carlos Santander" <csantander619 gmail.com> wrote in message 
news:df5tmk$6vp$2 digitaldaemon.com...
 David Medlock recently posted a code for mixin in opApply, opIndex and 
 opIndexAssign. Having done the same code myself, a question popped to my 
 head. Notice how the mixin is defined and then used:

 template applyThis( K, V, alias var )
 { ... }

 class MyWidgets
 {
     Widget[char[]]    lookup;
     mixin applyThis!( char[], Widget, lookup );
 }

 My question is, could there be a way for getting K and V from var? Maybe 
 something like this:

 template applyThis( alias var )
 {
     alias var.keytype K;
     alias var.valuetype V;
     ...
 }

 Which could then be used in a simpler way:

     mixin applyThis!(lookup);

 These two properties could be defined for all arrays, not just AAs. Also, 
 if someone wanted to define her own collection, she'd have to also define 
 those types/properties. What do you guys think?
I agree it would be nice. In MinTL I've been using the names IndexType and ValueType. For lists and arrays which use integer indexing IndexType is size_t.
Sep 01 2005
prev sibling parent Chris Sauls <ibisbasenji gmail.com> writes:
Carlos Santander wrote:
 David Medlock recently posted a code for mixin in opApply, opIndex and 
 opIndexAssign. Having done the same code myself, a question popped to my 
 head. Notice how the mixin is defined and then used:
 
 template applyThis( K, V, alias var )
 { ... }
 
 class MyWidgets
 {
     Widget[char[]]    lookup;
     mixin applyThis!( char[], Widget, lookup );
 }
 
 My question is, could there be a way for getting K and V from var? Maybe 
 something like this:
 
 template applyThis( alias var )
 {
     alias var.keytype K;
     alias var.valuetype V;
     ...
 }
 
 Which could then be used in a simpler way:
 
     mixin applyThis!(lookup);
 
 These two properties could be defined for all arrays, not just AAs. 
 Also, if someone wanted to define her own collection, she'd have to also 
 define those types/properties. What do you guys think?
 
Currently one can use an array with a template defined such as: And T will be set to the array's value type. So maybe for AA's we could use: Or something similar. -- Chris Sauls
Sep 01 2005