官术网_书友最值得收藏!

  • 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:

  1. Import std.conv.
  2. 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.

  3. 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.

Tip

There's also a function called std.conv.text() that works in the same way as writeln, but returns the string instead of printing it out. You can use this to convert several variables to string at once, for example, string s = text(10, " is ten!");.

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"; }.

主站蜘蛛池模板: 河源市| 迁安市| 枣庄市| 海阳市| 新民市| 泽州县| 合水县| 海城市| 左云县| 乐平市| 温宿县| 西盟| 隆尧县| 浙江省| 西藏| 科尔| 锦屏县| 温泉县| 高雄市| 桦甸市| 淳安县| 南丰县| 锡林浩特市| 惠来县| 宜君县| 贺州市| 蛟河市| 洛宁县| 于田县| 万载县| 黄平县| 衡阳县| 峨眉山市| 蓬溪县| 营山县| 黄山市| 嘉禾县| 宁城县| 梧州市| 保靖县| 黑水县|