www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - default opAssign(string) behaviour

reply strtr <strtr spam.com> writes:
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?
Jan 27 2010
next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
Jan 28 2010
parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
Jan 28 2010
prev sibling next sibling parent =?UTF-8?B?UGVsbGUgTcOlbnNzb24=?= <pelle.mansson gmail.com> writes:
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. :)
Jan 28 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
Jan 28 2010
prev sibling parent strtr <strtr spam.com> writes:
Thanks, now it looks obviously messy to me as well :)
Jan 28 2010