Re: Linker Error with Template Function
On Saturday, 1 October 2022 at 21:18:05 UTC, Ali Çehreli wrote: On 10/1/22 11:15, Kyle Ingraham wrote: > storing structs as > `void*` in a wrapper struct with information about their module and > identifier saved elsewhere. Perhaps unrelated but that part reminded me of the following discussion: https://forum.dlang.org/post/tfbn10$19nv$1...@digitalmars.com Ali Thanks for this Ali. I found it useful to see another way to solve this problem. Seems like it would be great to have a `Type` type that can store a type at compile-time for use at run-time.
Re: Linker Error with Template Function
On 10/1/22 11:15, Kyle Ingraham wrote: > storing structs as > `void*` in a wrapper struct with information about their module and > identifier saved elsewhere. Perhaps unrelated but that part reminded me of the following discussion: https://forum.dlang.org/post/tfbn10$19nv$1...@digitalmars.com Ali
Re: Linker Error with Template Function
On Tuesday, 13 September 2022 at 08:43:45 UTC, Nick Treleaven wrote: On Tuesday, 13 September 2022 at 03:00:17 UTC, Kyle Ingraham wrote: Any suggestions for being able to call one function for any instance given but maintain flexible return types? Not sure if it helps, but you can define final methods in an interface, which can call virtual interface methods: ```d interface PathConverter { string getValue(); final T toD(T)() { import std.conv : to; return to!T(getValue()); } } ``` Not tested as AFK. Thanks for the suggestion Nick. I solved this by storing structs as `void*` in a wrapper struct with information about their module and identifier saved elsewhere. I use that information to setup casts to the appropriate type then call `toD`. That way I can call the same method for functions that return different types and store disparate structs to the same wrapper struct. The wrapper struct gets used in function signatures.
Re: Linker Error with Template Function
On Tuesday, 13 September 2022 at 03:00:17 UTC, Kyle Ingraham wrote: Any suggestions for being able to call one function for any instance given but maintain flexible return types? Not sure if it helps, but you can define final methods in an interface, which can call virtual interface methods: ```d interface PathConverter { string getValue(); final T toD(T)() { import std.conv : to; return to!T(getValue()); } } ``` Not tested as AFK.
Re: Linker Error with Template Function
On Tuesday, 13 September 2022 at 01:46:14 UTC, Paul Backus wrote: On Tuesday, 13 September 2022 at 00:57:58 UTC, Kyle Ingraham wrote: I am writing a library where I would like to be able to store instances of a type of class to an associative array for later usage. Each class stored has to implement a function as part of the required interface. The argument given is always the same type but the return value should be flexible. I solved this with an interface: ```d interface PathConverter { T toD(T)(const string value) @safe; } ``` https://dlang.org/spec/template.html#limitations Templates cannot be used to add non-static fields or virtual functions to classes or interfaces. You *should* get an error from the compiler for trying to do this, instead of just a linker error somewhere else down the line, but either way it's not going to work. You'll have to find another solution. Thanks for the spec help Paul. I must've skirted around the compiler somehow. This is a minimal example that triggers the linker error: ```d interface PathConverter { T toD(T)(string value); } class NumberConverter(T) : PathConverter { T toD(T)(string value) { import std.conv : to; return to!T(value); } } alias IntConverter = NumberConverter!int; void main() { PathConverter[string] allConverters; allConverters["int"] = new IntConverter; int converted = allConverters["int"].toD!int("9"); } ``` Any suggestions for being able to call one function for any instance given but maintain flexible return types?
Re: Linker Error with Template Function
On Tuesday, 13 September 2022 at 00:57:58 UTC, Kyle Ingraham wrote: I am writing a library where I would like to be able to store instances of a type of class to an associative array for later usage. Each class stored has to implement a function as part of the required interface. The argument given is always the same type but the return value should be flexible. I solved this with an interface: ```d interface PathConverter { T toD(T)(const string value) @safe; } ``` https://dlang.org/spec/template.html#limitations Templates cannot be used to add non-static fields or virtual functions to classes or interfaces. You *should* get an error from the compiler for trying to do this, instead of just a linker error somewhere else down the line, but either way it's not going to work. You'll have to find another solution.