- 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"; }
.
- NativeScript for Angular Mobile Development
- Learning ArcGIS Pro
- 基于Swift語言的iOS App 商業實戰教程
- Spring Boot企業級項目開發實戰
- 編程數學
- 西門子S7-200 SMART PLC編程從入門到實踐
- 移動增值應用開發技術導論
- 區塊鏈項目開發指南
- TypeScript 2.x By Example
- Practical Maya Programming with Python
- PHP+MySQL Web應用開發教程
- 少兒編程輕松學(全2冊)
- 游戲設計的底層邏輯
- R High Performance Programming
- Web 2.0策略指南