digitalmars.D.learn - Factory using an alias template parameter to set a member of the new
- jkpl (25/25) Feb 09 2017 I'm looking for a better way to do this, if possible:
I'm looking for a better way to do this, if possible: ``` class Tool { string name; } T namedTool(alias Variable, T)() { T result = new T; result.name = Variable.stringof; return result; } void main() { Tool grep; grep = namedTool!(grep,Tool); assert(grep.name == "grep"); } ``` Ideally this would work like this: ``` Tool grep = namedTool!Tool; assert(grep.name == "grep"); ``` Possible ?
Feb 09 2017
On Thursday, 9 February 2017 at 13:30:07 UTC, jkpl wrote:I'm looking for a better way to do this, if possible: ``` class Tool { string name; } T namedTool(alias Variable, T)() { T result = new T; result.name = Variable.stringof; return result; } void main() { Tool grep; grep = namedTool!(grep,Tool); assert(grep.name == "grep"); } ``` Ideally this would work like this: ``` Tool grep = namedTool!Tool; assert(grep.name == "grep"); ``` Possible ?Sorry, but this does not make much sense to me ... You expect "namedTool!Tool" to know that the name of the Tool must be set to "grep". The expression evaluation proceeds from right to left, so at the time of analyzing "namedTool!Tool", the compiler knows nothing about "grep". On the other hand, if you supplied the "grep" variable, the compiler could infer its type, like this: ``` ... auto namedTool(alias Variable)() { alias T = typeof(Variable); T result = new T; result.name = Variable.stringof; return result; } void main() { Tool grep; grep = namedTool!(grep); assert(grep.name == "grep"); } ```
Feb 09 2017
On Thursday, 9 February 2017 at 14:39:41 UTC, angel wrote:On Thursday, 9 February 2017 at 13:30:07 UTC, jkpl wrote:Or actually, maybe this will suite your case better: ``` template namedTool(T, alias Variable) { enum namedTool = T.stringof ~ " " ~ Variable ~ " = new " ~ T.stringof ~ ";" ~ Variable ~ ".name = \"" ~ Variable ~ "\";"; } void main() { mixin(namedTool!(Tool, "grep")); assert(grep.name == "grep"); } ```I'm looking for a better way to do this, if possible: ``` class Tool { string name; } T namedTool(alias Variable, T)() { T result = new T; result.name = Variable.stringof; return result; } void main() { Tool grep; grep = namedTool!(grep,Tool); assert(grep.name == "grep"); } ``` Ideally this would work like this: ``` Tool grep = namedTool!Tool; assert(grep.name == "grep"); ``` Possible ?Sorry, but this does not make much sense to me ... You expect "namedTool!Tool" to know that the name of the Tool must be set to "grep". The expression evaluation proceeds from right to left, so at the time of analyzing "namedTool!Tool", the compiler knows nothing about "grep". On the other hand, if you supplied the "grep" variable, the compiler could infer its type, like this: ``` ... auto namedTool(alias Variable)() { alias T = typeof(Variable); T result = new T; result.name = Variable.stringof; return result; } void main() { Tool grep; grep = namedTool!(grep); assert(grep.name == "grep"); } ```
Feb 09 2017
On Thursday, 9 February 2017 at 15:00:21 UTC, angel wrote:On Thursday, 9 February 2017 at 14:39:41 UTC, angel wrote:Thanks for trying. I know that it doesn't make much sense but it would have been a cool sugar for the tool class. Mixins are not good for me because of completion in the IDE.On Thursday, 9 February 2017 at 13:30:07 UTC, jkpl wrote:Or actually, maybe this will suite your case better: ``` template namedTool(T, alias Variable) { enum namedTool = T.stringof ~ " " ~ Variable ~ " = new " ~ T.stringof ~ ";" ~ Variable ~ ".name = \"" ~ Variable ~ "\";"; } void main() { mixin(namedTool!(Tool, "grep")); assert(grep.name == "grep"); } ```I'm looking for a better way to do this, if possible:
Feb 09 2017