Thread overview |
---|
June 27, 2018 anyway to pass the context of an inner type to a template so it can be constructed? | ||||
---|---|---|---|---|
| ||||
This currently fails unless you mark the class as static: auto construct(T)() { return new T; } void main() { class C {} auto s = construct!C; } So wondering if there's anything that can be done to get the above working? Or if there isn't then how could the compiler be enhanced to allow for something like this if possible? === The use case is for a non-nullable type, where I want to guarantee that the value inside will never be null. I can't do it for inner classes though. And I can't allow the user to do something like: void main() { class C {} auto s = construct(new C); } Because I can't guarantee that's not null. Cheers, - Ali |
June 27, 2018 Re: anyway to pass the context of an inner type to a template so it can be constructed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | On Wednesday, 27 June 2018 at 12:02:10 UTC, aliak wrote:
> ===
> The use case is for a non-nullable type, where I want to guarantee that the value inside will never be null. I can't do it for inner classes though. And I can't allow the user to do something like:
>
> void main() {
> class C {}
> auto s = construct(new C);
> }
>
> Because I can't guarantee that's not null.
>
>
> Cheers,
> - Ali
Is there any reason, why you don't want to use a struct? An instance of such is never null, still having access to its context, if it is a function.
|
June 27, 2018 Re: anyway to pass the context of an inner type to a template so it can be constructed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | On Wednesday, 27 June 2018 at 12:02:10 UTC, aliak wrote:
> This currently fails unless you mark the class as static:
>
> auto construct(T)() {
> return new T;
> }
> void main() {
> class C {}
> auto s = construct!C;
> }
>
> So wondering if there's anything that can be done to get the above working?
>
>
> Or if there isn't then how could the compiler be enhanced to allow for something like this if possible?
>
> ===
> The use case is for a non-nullable type, where I want to guarantee that the value inside will never be null. I can't do it for inner classes though. And I can't allow the user to do something like:
>
> void main() {
> class C {}
> auto s = construct(new C);
> }
>
> Because I can't guarantee that's not null.
>
>
> Cheers,
> - Ali
After a bit of experimentation
import std.stdio;
template construct1(T)
{
enum construct1 = "new " ~ T.stringof;
}
template construct2(T)
{
// Attempt 1
// can't do this : /
//alias construct2 = mixin(construct1!T);
// Attempt 2
T construct2()
{
// Error: outer function context of D main is needed to new nested class onlineapp.main.C
//return new T;
// Error: undefined identifier C
//mixin("return new " ~ T.stringof ~ ";");
return null; // ...
}
}
mixin template construct3(string s, T)
{
mixin("auto " ~ s ~ " = new " ~ T.stringof ~ ";");
}
void main() {
class C { int i = 4;}
auto a = mixin(construct1!C);
assert(a.i == 4);
mixin construct3!("b", C);
b.i = 3;
assert(b.i == 3);
// trying to get around using mixin here...
auto c = construct2!C;
}
Can't seem to avoid using mixin in main..
|
June 27, 2018 Re: anyway to pass the context of an inner type to a template so it can be constructed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | On Wednesday, 27 June 2018 at 14:01:03 UTC, Alex wrote:
> On Wednesday, 27 June 2018 at 12:02:10 UTC, aliak wrote:
>> ===
>> The use case is for a non-nullable type, where I want to guarantee that the value inside will never be null. I can't do it for inner classes though. And I can't allow the user to do something like:
>>
>> void main() {
>> class C {}
>> auto s = construct(new C);
>> }
>>
>> Because I can't guarantee that's not null.
>>
>>
>> Cheers,
>> - Ali
>
> Is there any reason, why you don't want to use a struct? An instance of such is never null, still having access to its context, if it is a function.
Sorry, by non-nullable I meant not null. It's that construct produces a wrapper type that has an internal value that I want to guarantee is not null. So whether T is a struct or class is dependent on the user of construct.
- Ali
|
June 27, 2018 Re: anyway to pass the context of an inner type to a template so it can be constructed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timoses | On Wednesday, 27 June 2018 at 19:28:37 UTC, Timoses wrote:
> Can't seem to avoid using mixin in main..
hehe yeah I see, didn't think of trying mixins, worth a shot! It seems like you had fun at least ;)
|
Copyright © 1999-2021 by the D Language Foundation