www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - String Index Support for IndexedBy

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
I have implemented a type constructor [Ii]ndexedBy at

https://github.com/nordlow/justd/blob/master/typecons_ex.d#L97

restricted by

https://github.com/nordlow/justd/blob/master/typecons_ex.d#L78

See unittests below for usage.

I would now like to extend this to enable a variant usage as

     auto xs = x.indexedBy!"I"

I want this to trigger a mixin that defines a

     struct I

inside the struct IndexedBy.

But I don't know how to change the definitions of

- isIndexableBy
- [iI]ndexedBy

in order for this work.

I started by replace all template arguments

     I

with

     alias I

which I believe is the way to go to make it possible for I to 
either a type or a CT-string. But then I get lots of strange 
compiler messages that I can't figure out what to do with.

Further is my mixin attempt at

https://github.com/nordlow/justd/blob/master/typecons_ex.d#L99

in the right direction. If so do I have to turn IndexedBy into a 
template mixin?

Could someone please help me out here?
Apr 14 2015
parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Tuesday, 14 April 2015 at 19:38:10 UTC, Nordlöw wrote:
 I have implemented a type constructor [Ii]ndexedBy at

 https://github.com/nordlow/justd/blob/master/typecons_ex.d#L97

 restricted by

 https://github.com/nordlow/justd/blob/master/typecons_ex.d#L78

 See unittests below for usage.

 I would now like to extend this to enable a variant usage as

     auto xs = x.indexedBy!"I"

 I want this to trigger a mixin that defines a

     struct I

 inside the struct IndexedBy.

 But I don't know how to change the definitions of

 - isIndexableBy
 - [iI]ndexedBy

 in order for this work.

 I started by replace all template arguments

     I

 with

     alias I

 which I believe is the way to go to make it possible for I to 
 either a type or a CT-string. But then I get lots of strange 
 compiler messages that I can't figure out what to do with.

 Further is my mixin attempt at

 https://github.com/nordlow/justd/blob/master/typecons_ex.d#L99

 in the right direction. If so do I have to turn IndexedBy into 
 a template mixin?

 Could someone please help me out here?
I managed to make it all work at https://github.com/nordlow/justd/blob/master/typecons_ex.d#L114 by using two separate definitions of [Ii]ndexedBy for the cases where I is either a string literal or a type. This is not ideal though because it causes code duplication in the two definitions of IndexedBy. Can anybody come up with an alternate definition of [Ii]ndexedBy that doesn't required two separated definitions?
Apr 15 2015