Thread overview
Static CT Factory
Aug 19, 2016
Engine Machine
Aug 19, 2016
Adam D. Ruppe
Aug 19, 2016
Engine Machine
Aug 19, 2016
Anonymouse
Aug 19, 2016
Engine Machine
Aug 19, 2016
Basile B.
Aug 19, 2016
Engine Machine
August 19, 2016
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.

August 19, 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?
August 19, 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.
August 19, 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:
>> 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.

Ok, well, I guess the error comes from something else.
August 19, 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 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?

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.




August 19, 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:
>> On Friday, 19 August 2016 at 01:10:42 UTC, Engine Machine
>>> x = 1.234;
> Ok, well, I guess the error comes from something else.

*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;
}
August 19, 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:
>> On Friday, 19 August 2016 at 01:25:10 UTC, Anonymouse wrote:
>>> On Friday, 19 August 2016 at 01:10:42 UTC, Engine Machine
>>>> x = 1.234;
>> Ok, well, I guess the error comes from something else.
>
> *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;
> }

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.