digitalmars.D.learn - opIndex for type list
- data pulverizer (28/28) Aug 24 2020 Hi all,
- data pulverizer (4/6) Aug 24 2020 p.s. I know I could just write a separate `get` template, but
- Adam D. Ruppe (5/7) Aug 24 2020 Can't really do that, the operator overloads work on instances
- data pulverizer (2/10) Aug 24 2020 Fair enough, it's not a show stopper.
- James Lu (4/32) Aug 24 2020 This is an interesting syntax, and it reminds me of Python's
Hi all, I am trying to implement `opIndex` (e.g. T[i]) for types in a struct. So for I have `length`: ```d struct TList(T...) { enum long length = T.length; } ``` and have tried including ```d alias opIndex(long i) = T[i]; ``` or ```d alias opIndex(alias i) = T[i]; ``` called with ```d alias tList = AliasSeq!(bool, string, ubyte, short, ushort); TList!(tList)[0]; ``` but that doesn't work and I get the error: ```d opIndex cannot deduce function from argument types !()(int), candidates are: opIndex(long i) ```
Aug 24 2020
On Monday, 24 August 2020 at 14:19:14 UTC, data pulverizer wrote:I am trying to implement `opIndex` (e.g. T[i]) for types in a struct.p.s. I know I could just write a separate `get` template, but `AliasSeq` has opIndex and opSlice operators, so I wonder whether it is possible to get those in this case.
Aug 24 2020
On Monday, 24 August 2020 at 14:19:14 UTC, data pulverizer wrote:I am trying to implement `opIndex` (e.g. T[i]) for types in a struct. So for I have `length`:Can't really do that, the operator overloads work on instances instead of static types. AliasSeq is magical because it just gives a name to something built-into the compiler.
Aug 24 2020
On Monday, 24 August 2020 at 14:36:22 UTC, Adam D. Ruppe wrote:On Monday, 24 August 2020 at 14:19:14 UTC, data pulverizer wrote:Fair enough, it's not a show stopper.I am trying to implement `opIndex` (e.g. T[i]) for types in a struct. So for I have `length`:Can't really do that, the operator overloads work on instances instead of static types. AliasSeq is magical because it just gives a name to something built-into the compiler.
Aug 24 2020
On Monday, 24 August 2020 at 14:19:14 UTC, data pulverizer wrote:Hi all, I am trying to implement `opIndex` (e.g. T[i]) for types in a struct. So for I have `length`: ```d struct TList(T...) { enum long length = T.length; } ``` and have tried including ```d alias opIndex(long i) = T[i]; ``` or ```d alias opIndex(alias i) = T[i]; ``` called with ```d alias tList = AliasSeq!(bool, string, ubyte, short, ushort); TList!(tList)[0]; ``` but that doesn't work and I get the error: ```d opIndex cannot deduce function from argument types !()(int), candidates are: opIndex(long i) ```This is an interesting syntax, and it reminds me of Python's generic syntax. Will be interested to see how you use type opIndex.
Aug 24 2020