Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
January 28, 2010 default opAssign(string) behaviour | ||||
---|---|---|---|---|
| ||||
Personally, I use (D1)std2.conv a lot to get values from strings and thus would love the following default behaviour for all types: int i = "0"; // i = 0 i = cast( int ) "0"; // i = 48 ( If I read the utf8 table correctly ) What keeps this from being the case? |
January 28, 2010 Re: default opAssign(string) behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | On Thu, 28 Jan 2010 02:32:20 +0100, strtr <strtr@spam.com> wrote: > Personally, I use (D1)std2.conv a lot to get values from strings and thus would love the following default behaviour for all types: > > int i = "0"; // i = 0 > i = cast( int ) "0"; // i = 48 ( If I read the utf8 table correctly ) > > What keeps this from being the case? Implicit casting between unrelated types is considered bad. Most strings are not convertible to an int or whatever typeof(lhs). -- Simen |
January 28, 2010 Re: default opAssign(string) behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | On 01/28/2010 02:32 AM, strtr wrote:
> Personally, I use (D1)std2.conv a lot to get values from strings and thus would love the following default behaviour for all types:
>
> int i = "0"; // i = 0
> i = cast( int ) "0"; // i = 48 ( If I read the utf8 table correctly )
>
> What keeps this from being the case?
Strong typing, mostly. It's messy.
to!int("0") is seriously pretty, though. :)
|
January 28, 2010 Re: default opAssign(string) behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | On Wed, 27 Jan 2010 20:32:20 -0500, strtr <strtr@spam.com> wrote: > Personally, I use (D1)std2.conv a lot to get values from strings and thus would love the following default behaviour for all types: > > int i = "0"; // i = 0 Um... why? How is int i = 0; more difficult to use/understand than int i = "0"; If you want to assign from a variable, then i = to!(int)x; works. It's not that hard to do. The issue with the compiler trying to understand what you are doing results in ambiguities in other places. What does this mean? int i = "1" + "2"; // i == 3 or 12? These kinds of things are avoided, all by having a simple requirement that you use the to!() function. > i = cast( int ) "0"; // i = 48 ( If I read the utf8 table correctly ) i = '0'; or i = cast(int) '0'; works. Not sure of the first, but the second definitely. -Steve |
January 28, 2010 Re: default opAssign(string) behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | On Thu, 28 Jan 2010 09:02:12 +0100, Simen kjaeraas <simen.kjaras@gmail.com> wrote: > On Thu, 28 Jan 2010 02:32:20 +0100, strtr <strtr@spam.com> wrote: > >> Personally, I use (D1)std2.conv a lot to get values from strings and thus would love the following default behaviour for all types: >> >> int i = "0"; // i = 0 >> i = cast( int ) "0"; // i = 48 ( If I read the utf8 table correctly ) > Also, casting the string "0" to an int is gonna engender you the pointer to the first character of that string, not its value. So more likely, cast( int )"0" will produce something more akin to 4383140. -- Simen |
Copyright © 1999-2021 by the D Language Foundation