digitalmars.D.learn - Static CT Factory
- Engine Machine (28/28) Aug 18 2016 I have a template that is suppose to create a type based on a
- Adam D. Ruppe (4/6) Aug 18 2016 They do.
- Engine Machine (9/16) Aug 18 2016 I get undefined identifier, but when create the variable outside
- Anonymouse (5/20) Aug 18 2016 I'm not sure I understand. static if shouldn't introduce a new
- Engine Machine (2/27) Aug 18 2016 Ok, well, I guess the error comes from something else.
- Basile B. (13/17) Aug 18 2016 *x = 1.234 for when T verifies is(T == int*) produces an error.
- Engine Machine (6/23) Aug 18 2016 It was because I had a added a new type that I didn't account for
I have a template that is suppose to create a type based on a template parameter static if (T == "int") { auto x = New!int(); } else static if (T == "double") { auto x = New!double(); } x = 1.234; This is just an example, I use custom types. The static if's prevent x's scope from being after them, even though it should work perfectly fine. I can't declare x before because I don't know the type. The point is that I don't want to have to put x = 1.234; in each static if block when I should be able to do it perfectly fine afterwards. My types that I'm creating all have a name field and I want to set it once after the object is created. Luckily, all of them inherit from the same type and I can declare that before the static if block, but in general, that won't work. I feel that in this case I feel that the scope of the static if should allow things to escape since, well, they are static if's. It would be similar to #ifdef. I realize that D doesn't work this way, I'm asking for a nicer solution than having to duplicate the same code(x = 1.234 in this example) or having use a base class.
Aug 18 2016
On Friday, 19 August 2016 at 01:10:42 UTC, Engine Machine wrote:I feel that in this case I feel that the scope of the static if should allow things to escape since, well, they are static if's.They do. What, exactly, did you do and what, exactly did you see as the error?
Aug 18 2016
On Friday, 19 August 2016 at 01:18:28 UTC, Adam D. Ruppe wrote:On Friday, 19 August 2016 at 01:10:42 UTC, Engine Machine wrote:I get undefined identifier, but when create the variable outside it works. I used Anonymouse code and it works so I guess the issue stems from something else. My code is more complex and the stuff is in a foreach. I don't know why it isn't working since the simplified code is nearly identical. Maybe a branch wasn't being taken, I thought I covered them all.I feel that in this case I feel that the scope of the static if should allow things to escape since, well, they are static if's.They do. What, exactly, did you do and what, exactly did you see as the error?
Aug 18 2016
On Friday, 19 August 2016 at 01:10:42 UTC, Engine Machine wrote:I have a template that is suppose to create a type based on a template parameter static if (T == "int") { auto x = New!int(); } else static if (T == "double") { auto x = New!double(); } x = 1.234; This is just an example, I use custom types. The static if's prevent x's scope from being after them, even though it should work perfectly fine. I can't declare x before because I don't know the type.I'm not sure I understand. static if shouldn't introduce a new scope that way. https://dpaste.dzfl.pl/7b63a6e52309 Mind that x might be a pointer.
Aug 18 2016
On Friday, 19 August 2016 at 01:25:10 UTC, Anonymouse wrote:On Friday, 19 August 2016 at 01:10:42 UTC, Engine Machine wrote:Ok, well, I guess the error comes from something else.I have a template that is suppose to create a type based on a template parameter static if (T == "int") { auto x = New!int(); } else static if (T == "double") { auto x = New!double(); } x = 1.234; This is just an example, I use custom types. The static if's prevent x's scope from being after them, even though it should work perfectly fine. I can't declare x before because I don't know the type.I'm not sure I understand. static if shouldn't introduce a new scope that way. https://dpaste.dzfl.pl/7b63a6e52309 Mind that x might be a pointer.
Aug 18 2016
On Friday, 19 August 2016 at 01:53:22 UTC, Engine Machine wrote:On Friday, 19 August 2016 at 01:25:10 UTC, Anonymouse wrote:*x = 1.234 for when T verifies is(T == int*) produces an error. You can put an aditional argument in the function header: void foo(T)(auto ref T t = T.init) { static if (is(T == int)) auto x = new Thing!int; else static if (is(T == double)) auto x = new Thing!double; else static assert(false, "incorrect type"); *x = t; }On Friday, 19 August 2016 at 01:10:42 UTC, Engine MachineOk, well, I guess the error comes from something else.x = 1.234;
Aug 18 2016
On Friday, 19 August 2016 at 02:54:48 UTC, Basile B. wrote:On Friday, 19 August 2016 at 01:53:22 UTC, Engine Machine wrote:It was because I had a added a new type that I didn't account for in the static if chain ;/ The static assert is sort of required. We sort of need a static switch with required default so forgetting this stuff doesn't break later on(in strange ways) when new types are added.On Friday, 19 August 2016 at 01:25:10 UTC, Anonymouse wrote:*x = 1.234 for when T verifies is(T == int*) produces an error. You can put an aditional argument in the function header: void foo(T)(auto ref T t = T.init) { static if (is(T == int)) auto x = new Thing!int; else static if (is(T == double)) auto x = new Thing!double; else static assert(false, "incorrect type"); *x = t; }On Friday, 19 August 2016 at 01:10:42 UTC, Engine MachineOk, well, I guess the error comes from something else.x = 1.234;
Aug 18 2016