d - How to create a tuple of ranges? -


template tupindextorange(alias tup, indicies...){   import std.meta;   import std.typecons;   static if(indicies.length == 0){     enum tupindextorange = tuple();   }   else{     enum tupindextorange = tuple(tup[ indicies[0] ][], tupindextorange!(tup,indicies[1..$]));   } }  void main(){   alias integrals = aliasseq!(array!int, array!float, array!double);   tuple!integrals integrals;   integrals[0].insertback(1);   integrals[1].insertback(2);   integrals[2].insertback(3);   auto t = tupindextorange!(integrals, 0, 1, 2);   auto r = zip(t.expand); } 

err:

source/app.d(119,34): error: variable integrals cannot read @ compile time source/app.d(119,33):        called here: tuple(integrals.__expand_field_2.opslice(), tuple()) source/app.d(119,56): error: template instance app.main.tupindextorange!(integrals, 2) error instantiating source/app.d(119,56):        instantiated here: tupindextorange!(integrals, 1, 2) source/app.d(219,12):        instantiated here: tupindextorange!(integrals, 0, 1, 2) 

i assume error appears because trying access range @ compile time? how tell d need access range @ run time?

this tupindextorange should do:

auto t = tuple(integrals[0][], integrals[1][], integrals[2][]); 

how tell d need access range @ run time?

by passing runtime parameter:

template tupindextorange(indicies...) {     auto tupindextorange(t)(t tup) {       static if(indicies.length == 0){         return tuple();       }       else{         return tuple(tup[ indicies[0] ][], .tupindextorange!(indicies[1..$])(tup).expand);       }     } } 

two things note:

  1. the function nested in template allows specify indices , not type. if auto tupindextorange(t, indices)(t tup), woul have provide type explicitly.

  2. we recurse calling .tupindextorange rather tupindextorange, want recurse via template in outer scope, not nested function.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -