digitalmars.D.learn - default opAssign(string) behaviour
- strtr (4/4) Jan 27 2010 Personally, I use (D1)std2.conv a lot to get values from strings and thu...
- Simen kjaeraas (5/10) Jan 28 2010 Implicit casting between unrelated types is considered bad. Most
- Simen kjaeraas (7/13) Jan 28 2010 Also, casting the string "0" to an int is gonna engender you the pointer
- =?UTF-8?B?UGVsbGUgTcOlbnNzb24=?= (3/7) Jan 28 2010 Strong typing, mostly. It's messy.
- Steven Schveighoffer (15/19) Jan 28 2010 Um... why? How is int i = 0; more difficult to use/understand than int ...
- strtr (1/1) Jan 28 2010 Thanks, now it looks obviously messy to me as well :)
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
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
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: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. -- SimenPersonally, 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 )
Jan 28 2010
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
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 = 0Um... 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