Adding a partial specialization of std.conv.to for Typedef!string
monkyyy
crazymonkyyy at gmail.com
Sun Sep 1 15:21:09 UTC 2024
On Sunday, 1 September 2024 at 11:01:13 UTC, kdevel wrote:
> In order to make this work
>
> ```d
> import std.typecons;
>
> alias vstring = Typedef!string;
>
> void main ()
> {
> import std.stdio;
> import std.conv;
>
> auto v = 3.to!vstring; // ain't work out of the box
> writeln (v);
>
> auto w = 3.to!string;
> writeln (w);
>
> long l = 3.to!long;
> writeln (l);
> }
> ```
>
> I ended up adding
>
> ```d
> V to (V, T) (T t)
> {
> static import std.conv;
> return std.conv.to!V (t);
> }
>
> V to (V : vstring, T) (T t)
> {
> static import std.conv;
> auto s = std.conv.to!(TypedefType!V) (t);
> return cast (V) s;
> }
> ```
>
> to the code. Is there a way to add only a partial
> specialization of std.conv.to?
Im pretty sure all solutions are imperfect, but id suggest
combineing the overloadset
```d
//myconv.d
import std.conv;
alias to=std.conv.to;
//template to(T) if cond(....){alias to(T)=std.conv.to!T;} if you
need to mess with conditions
V to(V:vstring,T)(T t)=> cast(V) t.to!V;
```
spooky quantum shit will stop probably it from working on
`vstring[]` without trail and error(were its a ~~bug~~ feature of
dlang live editing the ast if it does work)
More information about the Digitalmars-d-learn
mailing list