www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Implicit conversion bug?

reply Kramer <Kramer_member pathlink.com> writes:
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
Feb 09 2006
parent reply Derek Parnell <derek psych.ward> writes:
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
Feb 09 2006
next sibling parent Kramer <Kramer_member pathlink.com> writes:
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
Feb 09 2006
prev sibling next sibling parent reply Kramer <Kramer_member pathlink.com> writes:
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
Feb 10 2006
parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- iD8DBQFD/rwv3w+/yD4P9tIRAszCAJwOFpdqxKPksoI6+DGDSfoDnn8uUwCglw1u avPQyRLGKx+/zG8wmEv38Io= =i3GT -----END PGP SIGNATURE-----
Feb 23 2006
prev sibling parent reply Kramer <Kramer_member pathlink.com> writes:
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
Feb 10 2006
parent reply "Derek Parnell" <derek psych.ward> writes:
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
Feb 10 2006
parent Kramer <Kramer_member pathlink.com> writes:
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
Feb 13 2006