std.conv.to

harakim harakim at gmail.com
Fri Jun 17 12:31:45 UTC 2022


I can generically convert a string to a type using to!type. I 
have a read function that does that. I have simplified the 
example below:

```d
	int readNumber()
	{
		return read!int(val => to!int(val), "number");
	}

	string readTime()
	{
		return read!string(val => toTime(val), "time");
	}

	private T read(T)(T function(string) transform, string typeName)
	{
	    string input = readln();
             return transform(input);
	}
```
However, I want to be able to convert my own custom types as 
well. How do I do that? Is there an operator overload I need to 
override for that? Do I have to static if on the template type 
and call the object?


More information about the Digitalmars-d-learn mailing list