Question about: ("1.1").to!int;

matheus matheus at gmail.com
Wed Oct 21 22:50:27 UTC 2020


Hi,

import std.stdio, std.conv;
void main(string[ ] args) {
     auto a = (1).to!int;     // this works
     auto b = ("1").to!int;   // this works
     auto c = (1.1).to!int;   // this works and c = 1
     auto d = ("1.1").to!int; // Doesn't work
}

The forth line gives me:

std.conv.ConvException@/usr/include/dlang/dmd/std/conv.d(1898): 
Unexpected '.' when converting from type string to type int
----------------
??:? pure @safe int std.conv.toImpl!(int, 
immutable(char)[]).toImpl(immutable(char)[]) [0x55de76d9b4d7]
??:? pure @safe int 
std.conv.to!(int).to!(immutable(char)[]).to(immutable(char)[]) 
[0x55de76d99a17]
??:? _Dmain [0x55de76d9986e]

Question:

Since (1.1).to!int = 1, shouldn't the string value ("1.1").to!int 
at least try to convert to float/double and then to int?

Because for what I see "to!int" converts from: int,real but in 
case of string only when it is a integer representation?

Matheus.


More information about the Digitalmars-d-learn mailing list