Attempts to perform a value-preserving conversion of the given value
from type S to type D. If the conversion cannot be performed in any
context, a compile-time error will be issued describing the types
involved. If the conversion fails at run-time because the destination
type could not represent the value being converted, a
ConversionException will be thrown.
For example, to convert the string "123" into an equivalent integer
value, you would use:
autov = to!(int)("123");
You may also specify a default value which should be returned in the
event that the conversion cannot take place:
autov = to!(int)("abc", 456);
The function will attempt to preserve the input value as exactly as
possible, given the limitations of the destination format. For
instance, converting a floating-point value to an integer will cause it
to round the value to the nearest integer value.
Below is a complete list of conversions between built-in types and
strings. Capitalised names indicate classes of types. Conversions
between types in the same class are also possible.
Conversions between arrays and associative arrays are also supported,
and are done element-by-element.
You can add support for value conversions to your types by defining
appropriate static and instance member functions. Given a type
the_type, any of the following members of a type T may be used:
The "toString_" method corresponding to the destination string type will be
tried first. If this method does not exist, then the function will
look for another "toString_" method from which it will convert the result.
Failing this, it will try "toString" and convert the result to the
appropriate encoding.
The rules for converting to a user-defined type are much the same,
except it makes use of the "fromUtf8", "fromUtf16", "fromUtf32" and
"fromString" static methods.
Note: This module contains imports to other Tango modules that needs
semantic analysis to be discovered. If your build tool doesn't do this
properly, causing compile or link time problems, import the relevant
module explicitly.
Attempts to perform a value-preserving conversion of the given value from type S to type D. If the conversion cannot be performed in any context, a compile-time error will be issued describing the types involved. If the conversion fails at run-time because the destination type could not represent the value being converted, a ConversionException will be thrown.
For example, to convert the string "123" into an equivalent integer value, you would use:
You may also specify a default value which should be returned in the event that the conversion cannot take place:
The function will attempt to preserve the input value as exactly as possible, given the limitations of the destination format. For instance, converting a floating-point value to an integer will cause it to round the value to the nearest integer value.
Below is a complete list of conversions between built-in types and strings. Capitalised names indicate classes of types. Conversions between types in the same class are also possible.
Conversions between arrays and associative arrays are also supported, and are done element-by-element.
You can add support for value conversions to your types by defining appropriate static and instance member functions. Given a type the_type, any of the following members of a type T may be used:
You may also use "camel case" names:
Arrays and associative arrays can also be explicitly supported:
If you have more complex requirements, you can also use the generic to and from templated members:
These templates will have the_type explicitly passed to them in the template instantiation.
Finally, strings are given special support. The following members will be checked for:
The "toString_" method corresponding to the destination string type will be tried first. If this method does not exist, then the function will look for another "toString_" method from which it will convert the result. Failing this, it will try "toString" and convert the result to the appropriate encoding.
The rules for converting to a user-defined type are much the same, except it makes use of the "fromUtf8", "fromUtf16", "fromUtf32" and "fromString" static methods.
Note: This module contains imports to other Tango modules that needs semantic analysis to be discovered. If your build tool doesn't do this properly, causing compile or link time problems, import the relevant module explicitly.