- D Cookbook
- Adam D. Ruppe
- 496字
- 2021-07-16 11:50:45
Performing type conversions
D is a strongly typed language, which means converting between types often has to be done explicitly. The language's built-in cast
operator only works when the types are already mostly compatible, for example int
to short
, or in cases where the user has defined an opCast
function. This doesn't cover the very common need of converting strings to integers and vice versa. That's where Phobos' std.conv
module is helpful.
How to do it…
Now it's time to perform type conversions by executing the following steps:
- Import
std.conv
. - Use the
to
function as follows:import std.conv; auto converted = to!desired_type(variable_to_convert);
This is pretty simple, and it works for a lot of types.
- To convert a string to an integer, use the following line of code:
auto a = to!int("123"); // a == 123
That's all you have to do!
How it works…
The std.conv.to
function strives to be a one-stop shop for type conversions. The to
function isn't just one function. It is actually a family of functions from a template, one body that is parameterized on a list of compile-time arguments. That's why it has the !
operator in the name. In D, you pass two sets of arguments to a function template: a compile-time argument list and a normal runtime argument list. The syntax is as follows:
function_name!(compile, time, arguments)(run, time, arguments);
If you're familiar with C++, this is similar to the function_name<type, list, here>(arguments, here);
function of that language. D chose !()
over <>
because it has less lexing ambiguity. Compile-time arguments are similar to runtime arguments, so they look similar too.
Note
If there is only one compile-time argument and it is syntactically simple (made up of only one token, for example, int
, string
, or object
, but not const(int)
or mymodule.Something
), the parentheses around the compile-time argument are not necessary. The to!int
function is equivalent to to!(int)
.
The compiler can often figure out compile-time arguments automatically if a runtime list is provided. This is called implicit function template instantiation (IFTI). In fact, this is how std.stdio.writeln
works; you can pass it any arguments you want, and it automatically converts them to strings and prints them out. Try the following line of code:
writeln!(int)(10); // works!
The reason writeln
converts variables to string is that like to
, it is a template function. D's templates let you write functions that generically handle a variety of types at once with convenient syntax. Phobos uses them extensively.
There's more…
To enable to
on custom types, either implement a constructor that takes the target type or implement T opCast(T:Target)()
. Converting a type to string can also be done with a member function: string toString() const { return "string representation"; }
.
- 圖解Java數據結構與算法(微課視頻版)
- PyTorch Artificial Intelligence Fundamentals
- 實戰低代碼
- Building Mapping Applications with QGIS
- TypeScript項目開發實戰
- Learning ArcGIS for Desktop
- Java程序設計入門
- Raspberry Pi Home Automation with Arduino(Second Edition)
- 好好學Java:從零基礎到項目實戰
- OpenResty完全開發指南:構建百萬級別并發的Web應用
- Mastering Concurrency Programming with Java 9(Second Edition)
- AutoCAD基礎教程
- Java Web開發基礎與案例教程
- Oracle SOA Suite 12c Administrator's Guide
- Building Microservices with Go