On Saturday, 9 March 2019 at 18:11:09 UTC, Jacob Shtokolov wrote:

One of the task was to take a string from STDIN and detect its type. There were a few options: Float, Integer, string and "something else" (which, I think, doesn't have any sense under the scope of the task).

Another std-based solution I came up with:

bool isInteger(string str)
{
    if(str.isNumeric)
    {
        try { return str.to!long == str.to!real; }
        catch(ConvException) { return false; }
    }
    else return false;
}

I tried to use std.conv.to and std.conv.parse, but found that they can't really do this. When I call `data.to!int`, the value of "123.45" will be converted to int!

What compiler version are you using? I on the other hand was surprised that I needed the try-catch above, after having already checked isNumeric. The documentation claims that the conversion to int or long would truncate, but my compiler (v2.084.0) throws instead.

Reply via email to