digitalmars.D - Access template parameters at runtime
- Henning Pohl (11/11) Aug 10 2012 A struct is meant to take only integers as parameters:
- Andrei Alexandrescu (3/13) Aug 10 2012 By fixing the typo?
- Henning Pohl (4/24) Aug 10 2012 Oups, sorry, imagine there isn't one.
- Vladimir Panteleev (9/11) Aug 10 2012 You can't index a tuple during compilation. You need to use an
- Vladimir Panteleev (3/8) Aug 10 2012 Sorry, meant to say - during runtime.
- Henning Pohl (3/12) Aug 10 2012 Thats it, thank you :]
- travert phare.normalesup.org (Christophe Travert) (3/17) Aug 10 2012 Note that if your design makes that you must have a tuple, you may build...
- Henning Pohl (4/25) Aug 10 2012 That is what I was trying first, but I could not make it work.
- David Nadlinger (4/6) Aug 10 2012 Just use the compiler tuple inside an array literal, like this:
- David Nadlinger (3/4) Aug 10 2012 Darn, that should have been: »just like when …«
- Denis Shelomovskij (11/13) Aug 10 2012 And if it's impossible/undesirable to create an array, you can do this:
- jerro (15/21) Aug 10 2012 This would be one way to do it:
- Andrej Mitrovic (12/14) Aug 10 2012 Guys I think you're overcomplecating it, you can just do:
- Dmitry Olshansky (9/23) Aug 10 2012 Internally identical to:
- Andrej Mitrovic (4/11) Aug 10 2012 t's easy to check, add "writeln(&ints[idx]);" in opIndex and index [0]
- Timon Gehr (2/31) Aug 10 2012 Still allocates in 2.060. static is the keyword of choice.
- travert phare.normalesup.org (Christophe Travert) (29/31) Aug 10 2012 For example:
- Henning Pohl (3/3) Aug 10 2012 Great, thank you :]
A struct is meant to take only integers as parameters: struct SomeStruct(intergers...) { int opIndex(size_t idx) /* ... */ { return integers[idx]; // Error ... } } alias SomeStruct!(1, 2, 3) ss; But it results in: Error: undefined identifier integers, did you mean tuple intergers? How can this problem be solved?
Aug 10 2012
On 8/10/12 9:55 AM, Henning Pohl wrote:A struct is meant to take only integers as parameters: struct SomeStruct(intergers...) { int opIndex(size_t idx) /* ... */ { return integers[idx]; // Error ... } } alias SomeStruct!(1, 2, 3) ss; But it results in: Error: undefined identifier integers, did you mean tuple intergers? How can this problem be solved?By fixing the typo? Andrei
Aug 10 2012
On Friday, 10 August 2012 at 14:02:08 UTC, Andrei Alexandrescu wrote:On 8/10/12 9:55 AM, Henning Pohl wrote:Oups, sorry, imagine there isn't one. So the error is: variable idx cannot be read at compile time.A struct is meant to take only integers as parameters: struct SomeStruct(intergers...) { int opIndex(size_t idx) /* ... */ { return integers[idx]; // Error ... } } alias SomeStruct!(1, 2, 3) ss; But it results in: Error: undefined identifier integers, did you mean tuple intergers? How can this problem be solved?By fixing the typo? Andrei
Aug 10 2012
On Friday, 10 August 2012 at 14:05:16 UTC, Henning Pohl wrote:Oups, sorry, imagine there isn't one. So the error is: variable idx cannot be read at compile time.You can't index a tuple during compilation. You need to use an array: struct SomeStruct(alias integers) { int opIndex(size_t idx) { return integers[idx]; } } alias SomeStruct!([1, 2, 3]) ss;
Aug 10 2012
On Friday, 10 August 2012 at 14:10:02 UTC, Vladimir Panteleev wrote:On Friday, 10 August 2012 at 14:05:16 UTC, Henning Pohl wrote:Sorry, meant to say - during runtime.Oups, sorry, imagine there isn't one. So the error is: variable idx cannot be read at compile time.You can't index a tuple during compilation.
Aug 10 2012
On Friday, 10 August 2012 at 14:10:38 UTC, Vladimir Panteleev wrote:On Friday, 10 August 2012 at 14:10:02 UTC, Vladimir Panteleev wrote:Thats it, thank you :]On Friday, 10 August 2012 at 14:05:16 UTC, Henning Pohl wrote:Sorry, meant to say - during runtime.Oups, sorry, imagine there isn't one. So the error is: variable idx cannot be read at compile time.You can't index a tuple during compilation.
Aug 10 2012
"Henning Pohl" , dans le message (digitalmars.D:174569), a écrit :On Friday, 10 August 2012 at 14:10:38 UTC, Vladimir Panteleev wrote:Note that if your design makes that you must have a tuple, you may build the array at compile time, so that you can index it at run time.On Friday, 10 August 2012 at 14:10:02 UTC, Vladimir Panteleev wrote:Thats it, thank you :]On Friday, 10 August 2012 at 14:05:16 UTC, Henning Pohl wrote:Sorry, meant to say - during runtime.Oups, sorry, imagine there isn't one. So the error is: variable idx cannot be read at compile time.You can't index a tuple during compilation.
Aug 10 2012
On Friday, 10 August 2012 at 14:35:29 UTC, travert phare.normalesup.org (Christophe Travert) wrote:"Henning Pohl" , dans le message (digitalmars.D:174569), a écrit :That is what I was trying first, but I could not make it work. Maybe you can show me how it's done?On Friday, 10 August 2012 at 14:10:38 UTC, Vladimir Panteleev wrote:Note that if your design makes that you must have a tuple, you may build the array at compile time, so that you can index it at run time.On Friday, 10 August 2012 at 14:10:02 UTC, Vladimir Panteleev wrote:Thats it, thank you :]On Friday, 10 August 2012 at 14:05:16 UTC, Henning Pohl wrote:Sorry, meant to say - during runtime.Oups, sorry, imagine there isn't one. So the error is: variable idx cannot be read at compile time.You can't index a tuple during compilation.
Aug 10 2012
On Friday, 10 August 2012 at 14:42:24 UTC, Henning Pohl wrote:That is what I was trying first, but I could not make it work. Maybe you can show me how it's done?Just use the compiler tuple inside an array literal, like this: [integers]. It will auto-expand, just when passing it to a method. David
Aug 10 2012
On Friday, 10 August 2012 at 15:26:51 UTC, David Nadlinger wrote:It will auto-expand, just when passing it to a method.Darn, that should have been: »just like when …« David
Aug 10 2012
10.08.2012 19:26, David Nadlinger пишет:Just use the compiler tuple inside an array literal, like this: [integers]. It will auto-expand, just when passing it to a method.And if it's impossible/undesirable to create an array, you can do this: --- foreach(i, t; your_tuple) if(i == idx) return t; // or do something else assert(0); --- -- Денис В. Шеломовский Denis V. Shelomovskij
Aug 10 2012
This would be one way to do it: auto staticArray(Elements...)(Elements elements) { alias Elements[0] E; E[Elements.length] r; foreach(i, _; elements) r[i] = elements[i]; return r; } struct SomeStruct(integers...) { enum arr = staticArray(integers); int opIndex(size_t idx){ return arr[idx]; } }Note that if your design makes that you must have a tuple, you may build the array at compile time, so that you can index it at run time.That is what I was trying first, but I could not make it work. Maybe you can show me how it's done?
Aug 10 2012
On 8/10/12, jerro <a a.com> wrote:This would be one way to do it:On 8/10/12, Christophe Travert <travert phare.normalesup.org> wrote:For example:Guys I think you're overcomplecating it, you can just do: struct SomeStruct(integers...) { enum ints = [integers]; int opIndex(size_t idx) /* ... */ { return ints[idx]; } } enum or static both work.
Aug 10 2012
On 11-Aug-12 00:58, Andrej Mitrovic wrote:On 8/10/12, jerro <a a.com> wrote:Internally identical to: int opIndex(size_t idx) { return [integers][idx]; } Allocates on every call or not? I've no idea maybe Kenji fixed this already. -- Dmitry OlshanskyThis would be one way to do it:On 8/10/12, Christophe Travert <travert phare.normalesup.org> wrote:For example:Guys I think you're overcomplecating it, you can just do: struct SomeStruct(integers...) { enum ints = [integers]; int opIndex(size_t idx) /* ... */ { return ints[idx]; } } enum or static both work.
Aug 10 2012
On 8/10/12, Dmitry Olshansky <dmitry.olsh gmail.com> wrote:Internally identical to: int opIndex(size_t idx) { return [integers][idx]; } Allocates on every call or not? I've no idea maybe Kenji fixed this already.t's easy to check, add "writeln(&ints[idx]);" in opIndex and index [0] several times. If the addresses change it means it allocates. It does allocate every time when it's an enum but not if it's static.
Aug 10 2012
On 08/10/2012 11:09 PM, Dmitry Olshansky wrote:On 11-Aug-12 00:58, Andrej Mitrovic wrote:Still allocates in 2.060. static is the keyword of choice.On 8/10/12, jerro <a a.com> wrote:Internally identical to: int opIndex(size_t idx) { return [integers][idx]; } Allocates on every call or not? I've no idea maybe Kenji fixed this already. -- Dmitry OlshanskyThis would be one way to do it:On 8/10/12, Christophe Travert <travert phare.normalesup.org> wrote:For example:Guys I think you're overcomplecating it, you can just do: struct SomeStruct(integers...) { enum ints = [integers]; int opIndex(size_t idx) /* ... */ { return ints[idx]; } } enum or static both work.
Aug 10 2012
"Henning Pohl" , dans le message (digitalmars.D:174572), a écrit :That is what I was trying first, but I could not make it work. Maybe you can show me how it's done?For example: import std.stdio; template TupleToArray(T...) { static if (T.length == 1) { enum TupleToArray = [T[0]]; } else { enum TupleToArray = TupleToArray!(T[0..$-1]) ~ T[$-1]; } } void main() { alias TupleToArray!(1, 2, 3) oneTwoThree; foreach (i; 0..3) writeln(oneTwoThree[i]); } output: 1 2 3 TupleToArray should have proper check to make clean code. There must be something like that somewhere in phobos, or it should be added. -- Christophe
Aug 10 2012
Great, thank you :] The solution provided by David seems to be shortest. You can even pass the ints directly.
Aug 10 2012