template/mixin magic for to! auto inferring type from variable
Paul Backus
snarwin at gmail.com
Fri Feb 2 21:01:53 UTC 2024
On Friday, 2 February 2024 at 07:43:09 UTC, Chris Katko wrote:
> Is there some way to do:
> ```D
> string[3] data; //strings from some file input, some are ints,
> uints, etc.
>
> auto into!(T)(T value){return to!???(value); } // ???
>
> uint time = into!(data[1]); // We already know this is uint
> int priority = into!(data[2]);
> ```
>
> instead of:
> ```D
> uint time = to!uint(data[1]); // specifying type twice
> int priority = to!int(data[2])
> ```
No, D only does bottom-up type inference, not top down.
If you want to avoid repeating the type, use `auto` on the left
side:
```d
auto time = to!uint(data[1]);
auto priority = to!int(data[2]);
```
More information about the Digitalmars-d-learn
mailing list