October 24, 2020
On Friday, 23 October 2020 at 16:59:06 UTC, matheus wrote:
> On Friday, 23 October 2020 at 13:57:41 UTC, Joseph Rushton Wakeling wrote:
>> On Wednesday, 21 October 2020 at 22:50:27 UTC, matheus wrote:
> Well since the caller is handling a string, shouldn't the caller verify the content before any conversion?

What if:
"1.1.1".to!int
?

The algorithm will become more complicated?
Will the speed decrease?

October 24, 2020
On Saturday, 24 October 2020 at 04:04:18 UTC, Виталий Фадеев wrote:
> On Friday, 23 October 2020 at 16:59:06 UTC, matheus wrote:
>> On Friday, 23 October 2020 at 13:57:41 UTC, Joseph Rushton Wakeling wrote:
>>> On Wednesday, 21 October 2020 at 22:50:27 UTC, matheus wrote:
>> Well since the caller is handling a string, shouldn't the caller verify the content before any conversion?
>
> What if:
> "1.1.1".to!int
> ?
>
> The algorithm will become more complicated?
> Will the speed decrease?

I don't get...

Anyway this should fail, look "1.1" is one thing, "1.1.1" is another thing as "1.a" is another thing.

Like I said before I think the behavior should be the same in these cases:

(1.1).to!int = 1.
("1.1").to!int = Current is an error and IMO should be 1.

Now for your question:

1.1.1.to!int = Error: found `0.1` when expecting `,`.
"1.1.1".to!int = Should be the same as above.

Matheus.
October 24, 2020
On Saturday, 24 October 2020 at 14:10:02 UTC, matheus wrote:
> On Saturday, 24 October 2020 at 04:04:18 UTC, Виталий Фадеев wrote:
>> On Friday, 23 October 2020 at 16:59:06 UTC, matheus wrote:
>>> On Friday, 23 October 2020 at 13:57:41 UTC, Joseph Rushton Wakeling wrote:
>>>> On Wednesday, 21 October 2020 at 22:50:27 UTC, matheus wrote:
> I don't get...
> (1.1).to!int = 1.
> ("1.1").to!int = Current is an error and IMO should be 1.
> Matheus.

You should write converter from string to int.
Then you will understand.

You converter input:
"1"
"1.1"
"1.1.1"
"1a"
"1.1a"
"a"
".1"
".1.1"

You converter must be fast.

October 25, 2020
On Wednesday, 21 October 2020 at 22:50:27 UTC, matheus wrote:
> 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.

In first case you asked to convert a float (inferred by templace function) to int in the second case, you asked for the function "convert this string contaning a set of digits to an int for me". Doesn't make sense do something else than the asked. D has static if() and a nice way to do type comparasion at compile time. It does optimize things alot.

I haven't read the to source code, but consider it may use the idea like this:

auto to(T, T2)(T2 source)
{
    // you asked for int, doesn't makes sense consider a float here.
    // that would waste time checking for floats when you programmer's requested
    // was clear: an integer.
    static if(is(T == int) && is(T2 == string))
    {
        int v = 0;
        foreach(c; source)
            v = v * 10 + c - '0';
        return v;
    }
    // the string to float point conversion algorithm
    static if(is(T == float) && is(T2 == string))
    {
        /* ... */
    }
    static if(is(T == int) && is(T2 == double))
    {
        return cast(int)source;
    }
    assert(0, "unknow type");
}
1 2
Next ›   Last »