Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
February 10, 2006 Implicit conversion bug? | ||||
---|---|---|---|---|
| ||||
I can't tell if the following code is a bug, or if I need to throw in a cast somewhere. # import std.string, std.utf, std.stdarg; # void main(char[][] args) # { # void func(...) # { # for (int i = 0; i < _arguments.length; i++) { # if (_arguments[i] == typeid(wchar)) { # char[] str = toUTF8(toString(va_arg!(wchar)(_argptr))); # } # } # } # } Produces this: bug.d(9): function std.string.toString called with argument types: (wchar) matches both: std.string.toString(char) and: std.string.toString(creal) I'm guessing it's matching by implicit conversions, but that's just a little confusing??? -Kramer |
February 10, 2006 Re: Implicit conversion bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kramer | On Fri, 10 Feb 2006 06:28:05 +0000 (UTC), Kramer wrote: > I can't tell if the following code is a bug, or if I need to throw in a cast somewhere. > > # import std.string, std.utf, std.stdarg; > > # void main(char[][] args) > # { > # void func(...) > # { > # for (int i = 0; i < _arguments.length; i++) { > # if (_arguments[i] == typeid(wchar)) { > # char[] str = toUTF8(toString(va_arg!(wchar)(_argptr))); > # } > # } > # } > # } > > Produces this: > bug.d(9): function std.string.toString called with argument types: > (wchar) > matches both: > std.string.toString(char) > and: > std.string.toString(creal) > > I'm guessing it's matching by implicit conversions, but that's just a little confusing??? > > -Kramer You could add these two functions to your code (or Phobos; hint, hint) char[] toString(wchar c) { wchar[] result; result.length = 1; result[0] = c; return std.utf.toUTF8(result); } char[] toString(dchar c) { dchar[] result; result.length = 1; result[0] = c; return std.utf.toUTF8(result); } -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 10/02/2006 5:36:19 PM |
February 10, 2006 Re: Implicit conversion bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | In article <ah2r7az1frya.3xeee3qsuwd7$.dlg@40tude.net>, Derek Parnell says... > >On Fri, 10 Feb 2006 06:28:05 +0000 (UTC), Kramer wrote: > >> I can't tell if the following code is a bug, or if I need to throw in a cast somewhere. >> >> # import std.string, std.utf, std.stdarg; >> >> # void main(char[][] args) >> # { >> # void func(...) >> # { >> # for (int i = 0; i < _arguments.length; i++) { >> # if (_arguments[i] == typeid(wchar)) { >> # char[] str = toUTF8(toString(va_arg!(wchar)(_argptr))); >> # } >> # } >> # } >> # } >> >> Produces this: >> bug.d(9): function std.string.toString called with argument types: >> (wchar) >> matches both: >> std.string.toString(char) >> and: >> std.string.toString(creal) >> >> I'm guessing it's matching by implicit conversions, but that's just a little confusing??? >> >> -Kramer > >You could add these two functions to your code (or Phobos; hint, hint) > >char[] toString(wchar c) >{ > wchar[] result; > result.length = 1; > result[0] = c; > return std.utf.toUTF8(result); >} > >char[] toString(dchar c) >{ > dchar[] result; > result.length = 1; > result[0] = c; > return std.utf.toUTF8(result); >} > > >-- >Derek >(skype: derek.j.parnell) >Melbourne, Australia >"Down with mediocracy!" >10/02/2006 5:36:19 PM Cool, thanks. That helps... and those would be nice in the standard lib. -Kramer |
February 10, 2006 Re: Implicit conversion bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | In article <ah2r7az1frya.3xeee3qsuwd7$.dlg@40tude.net>, Derek Parnell says... > >On Fri, 10 Feb 2006 06:28:05 +0000 (UTC), Kramer wrote: > >> I can't tell if the following code is a bug, or if I need to throw in a cast somewhere. >> >> # import std.string, std.utf, std.stdarg; >> >> # void main(char[][] args) >> # { >> # void func(...) >> # { >> # for (int i = 0; i < _arguments.length; i++) { >> # if (_arguments[i] == typeid(wchar)) { >> # char[] str = toUTF8(toString(va_arg!(wchar)(_argptr))); >> # } >> # } >> # } >> # } >> >> Produces this: >> bug.d(9): function std.string.toString called with argument types: >> (wchar) >> matches both: >> std.string.toString(char) >> and: >> std.string.toString(creal) >> >> I'm guessing it's matching by implicit conversions, but that's just a little confusing??? >> >> -Kramer > >You could add these two functions to your code (or Phobos; hint, hint) > >char[] toString(wchar c) >{ > wchar[] result; > result.length = 1; > result[0] = c; > return std.utf.toUTF8(result); >} > >char[] toString(dchar c) >{ > dchar[] result; > result.length = 1; > result[0] = c; > return std.utf.toUTF8(result); >} > > >-- >Derek >(skype: derek.j.parnell) >Melbourne, Australia >"Down with mediocracy!" >10/02/2006 5:36:19 PM Not sure if this is supposed to be allowed for nested functions (couldn't find anything in the docs), but when both of the nested functions are there, it won't compile. Comment one or the other and it compiles clean. # import std.string, std.utf; # void main(char[][] args) { # char[] func(char[] str) { # char[] toString(wchar c) { # wchar[] result; # result.length = 1; # result[0] = c; # return std.utf.toUTF8(result); # } # char[] toString(dchar c) { # dchar[] result; # result.length = 1; # result[0] = c; # return std.utf.toUTF8(result); # } # return str; # } # } produces this: bug.d(12): declaration toString is already defined -Kramer |
February 10, 2006 Re: Implicit conversion bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | In article <ah2r7az1frya.3xeee3qsuwd7$.dlg@40tude.net>, Derek Parnell says... > >On Fri, 10 Feb 2006 06:28:05 +0000 (UTC), Kramer wrote: > >> I can't tell if the following code is a bug, or if I need to throw in a cast somewhere. >> >> # import std.string, std.utf, std.stdarg; >> >> # void main(char[][] args) >> # { >> # void func(...) >> # { >> # for (int i = 0; i < _arguments.length; i++) { >> # if (_arguments[i] == typeid(wchar)) { >> # char[] str = toUTF8(toString(va_arg!(wchar)(_argptr))); >> # } >> # } >> # } >> # } >> >> Produces this: >> bug.d(9): function std.string.toString called with argument types: >> (wchar) >> matches both: >> std.string.toString(char) >> and: >> std.string.toString(creal) >> >> I'm guessing it's matching by implicit conversions, but that's just a little confusing??? >> >> -Kramer > >You could add these two functions to your code (or Phobos; hint, hint) > >char[] toString(wchar c) >{ > wchar[] result; > result.length = 1; > result[0] = c; > return std.utf.toUTF8(result); >} > >char[] toString(dchar c) >{ > dchar[] result; > result.length = 1; > result[0] = c; > return std.utf.toUTF8(result); >} > > >-- >Derek >(skype: derek.j.parnell) >Melbourne, Australia >"Down with mediocracy!" >10/02/2006 5:36:19 PM This also compiles. You don't have to put in a try block to catch the utf exception, but I'm not sure if this is any *better* necessarily because of the cast. # char[] toString(wchar c) { # char[] result = new char[1]; # result[0] = cast(char)c; # return result[0..1]; # } -Kramer |
February 10, 2006 Re: Implicit conversion bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kramer | On Sat, 11 Feb 2006 07:29:01 +1100, Kramer <Kramer_member@pathlink.com> wrote: > This also compiles. You don't have to put in a try block to catch the utf > exception, but I'm not sure if this is any *better* necessarily because of the > cast. > > # char[] toString(wchar c) { > # char[] result = new char[1]; > # result[0] = cast(char)c; > # return result[0..1]; > # } It might compile but its wrong to do it this way. A wchar is two-bytes long. The resulting char[] might one or two bytes long depending on the content of 'c'. -- Derek Parnell Melbourne, Australia |
February 13, 2006 Re: Implicit conversion bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | In article <op.s4r4zzwc6b8z09@ginger.vic.bigpond.net.au>, Derek Parnell says... > >On Sat, 11 Feb 2006 07:29:01 +1100, Kramer <Kramer_member@pathlink.com> wrote: > > >> This also compiles. You don't have to put in a try block to catch the >> utf >> exception, but I'm not sure if this is any *better* necessarily because >> of the >> cast. >> >> # char[] toString(wchar c) { >> # char[] result = new char[1]; >> # result[0] = cast(char)c; >> # return result[0..1]; >> # } > >It might compile but its wrong to do it this way. A wchar is two-bytes long. The resulting char[] might one or two bytes long depending on the content of 'c'. > >-- >Derek Parnell >Melbourne, Australia Thanks, I'll be sure not to do it this way. I'm going from memeory here, but I believe I had the -w flag thrown. Would've been nice to see a narrowing warning as I know others have asked for as well. -Kramer |
February 24, 2006 Re: Implicit conversion bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kramer Attachments: | Kramer schrieb am 2006-02-10: [snip] > Not sure if this is supposed to be allowed for nested functions (couldn't find anything in the docs), but when both of the nested functions are there, it won't compile. Comment one or the other and it compiles clean. > > # import std.string, std.utf; > > # void main(char[][] args) { > # char[] func(char[] str) { > # char[] toString(wchar c) { > # wchar[] result; > # result.length = 1; > # result[0] = c; > # return std.utf.toUTF8(result); > # } > > # char[] toString(dchar c) { > # dchar[] result; > # result.length = 1; > # result[0] = c; > # return std.utf.toUTF8(result); > # } > > # return str; > # } > # } > > produces this: > bug.d(12): declaration toString is already defined http://digitalmars.com/d/function.html: | Unlike module level declarations, declarations within function scope | are processed in order. Thomas |
Copyright © 1999-2021 by the D Language Foundation