www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Limited type matching?

reply "Namespace" <rswhite4 googlemail.com> writes:
Code:

----
import std.stdio;

void foo(short x, short y) { }
void foo(short[2] xy) { }

void main()
{
	foo(1, 2); /// works
	foo([1, 2]); /// works
	
	ushort[2] xy = [1, 2];
	foo(xy); /// fails
	
	ushort x = 1, y = 2;
	foo(x, y); /// works
}
----

What is the problem? If the compiler is able to cast implicit 
from ushort to short, what is the problem of casting ushort[2] to 
short[2]?
Sep 08 2013
next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Sunday, 8 September 2013 at 21:11:53 UTC, Namespace wrote:
 Code:

 ----
 import std.stdio;

 void foo(short x, short y) { }
 void foo(short[2] xy) { }

 void main()
 {
 	foo(1, 2); /// works
 	foo([1, 2]); /// works
 	
 	ushort[2] xy = [1, 2];
 	foo(xy); /// fails
 	
 	ushort x = 1, y = 2;
 	foo(x, y); /// works
 }
 ----

 What is the problem? If the compiler is able to cast implicit 
 from ushort to short, what is the problem of casting ushort[2] 
 to short[2]?
Ok I fill a bug for that...
Sep 09 2013
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/9/13, Namespace <rswhite4 googlemail.com> wrote:
 Ok I fill a bug for that...
Please do, that code should compile.
Sep 09 2013
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/8/13, Namespace <rswhite4 googlemail.com> wrote:
 What is the problem? If the compiler is able to cast implicit
 from ushort to short, what is the problem of casting ushort[2] to
 short[2]?
Oh I didn't even noticed it was a signed/unsigned issue. I'm not sure whether or not it's a bug. But file it anyways and someone will know what to do with it.
Sep 09 2013
next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 9 September 2013 at 11:36:42 UTC, Andrej Mitrovic 
wrote:
 On 9/8/13, Namespace <rswhite4 googlemail.com> wrote:
 What is the problem? If the compiler is able to cast implicit
 from ushort to short, what is the problem of casting ushort[2] 
 to
 short[2]?
Oh I didn't even noticed it was a signed/unsigned issue. I'm not sure whether or not it's a bug. But file it anyways and someone will know what to do with it.
I think the "issue" is that when you write "[1, 2]", that array is still "weakly typed", and the compiler will implicitly cast it to any type that will create a match. Once you've crammed it into a variable though, it becomes explicitly typed, and the casts become no-go. It's kind of like how you can write: wstring s = "hello!"; I dare say that the behavior is expected.
Sep 09 2013
prev sibling parent reply "Kenji Hara" <k.hara.pg gmail.com> writes:
On Monday, 9 September 2013 at 11:36:42 UTC, Andrej Mitrovic 
wrote:
 On 9/8/13, Namespace <rswhite4 googlemail.com> wrote:
 What is the problem? If the compiler is able to cast implicit
 from ushort to short, what is the problem of casting ushort[2] 
 to
 short[2]?
Oh I didn't even noticed it was a signed/unsigned issue. I'm not sure whether or not it's a bug. But file it anyways and someone will know what to do with it.
Currently, all of array types does not allow copy-conversion like ushort to short for their elements. In old D1 age, static array did not have value semantics, so the behavior was expected. In D2, static array had changed to value type, but the behavior was not changed. As far as I know, there was no discussion about that, but at least it is not fundamentally wrong. Kenji Hara
Sep 09 2013
parent reply "Namespace" <rswhite4 googlemail.com> writes:
 Currently, all of array types does not allow copy-conversion 
 like ushort to short for their elements.

 In old D1 age, static array did not have value semantics, so 
 the behavior was expected.
 In D2, static array had changed to value type, but the behavior 
 was not changed.

 As far as I know, there was no discussion about that, but at 
 least it is not fundamentally wrong.

 Kenji Hara
And what is your personal opinion? I think an implicit cast between signed / unsigned would be acceptable even with static arrays. It were only logical.
Sep 10 2013
parent reply "Kenji Hara" <k.hara.pg gmail.com> writes:
On Tuesday, 10 September 2013 at 08:04:48 UTC, Namespace wrote:
 Currently, all of array types does not allow copy-conversion 
 like ushort to short for their elements.

 In old D1 age, static array did not have value semantics, so 
 the behavior was expected.
 In D2, static array had changed to value type, but the 
 behavior was not changed.

 As far as I know, there was no discussion about that, but at 
 least it is not fundamentally wrong.

 Kenji Hara
And what is your personal opinion? I think an implicit cast between signed / unsigned would be acceptable even with static arrays. It were only logical.
Hmm, OK. I've taken a look a little deeper about the current behavior. void main() { void f1(short) {} void f2(short[2]) {} void f3(short[]) {} ushort us = 1; short ss = us; // OK ss = us; // OK f1(us); // OK ushort[2] usa = [1,2]; short[2] ssa = usa; // OK -> [x] ssa = usa; // NG -> [y] f2(usa); // NG -> [y] ushort[] uda = [1,2]; short[] sda = uda; // NG, expected sda = uda; // NG, expected f3(uda); // NG, expected } Surely the current state that, [x] is accepted but [y] is rejected, looks weird to me. It would be a bug. Kenji Hara
Sep 10 2013
parent "Namespace" <rswhite4 googlemail.com> writes:
On Tuesday, 10 September 2013 at 10:53:15 UTC, Kenji Hara wrote:
 On Tuesday, 10 September 2013 at 08:04:48 UTC, Namespace wrote:
 Currently, all of array types does not allow copy-conversion 
 like ushort to short for their elements.

 In old D1 age, static array did not have value semantics, so 
 the behavior was expected.
 In D2, static array had changed to value type, but the 
 behavior was not changed.

 As far as I know, there was no discussion about that, but at 
 least it is not fundamentally wrong.

 Kenji Hara
And what is your personal opinion? I think an implicit cast between signed / unsigned would be acceptable even with static arrays. It were only logical.
Hmm, OK. I've taken a look a little deeper about the current behavior. void main() { void f1(short) {} void f2(short[2]) {} void f3(short[]) {} ushort us = 1; short ss = us; // OK ss = us; // OK f1(us); // OK ushort[2] usa = [1,2]; short[2] ssa = usa; // OK -> [x] ssa = usa; // NG -> [y] f2(usa); // NG -> [y] ushort[] uda = [1,2]; short[] sda = uda; // NG, expected sda = uda; // NG, expected f3(uda); // NG, expected } Surely the current state that, [x] is accepted but [y] is rejected, looks weird to me. It would be a bug. Kenji Hara
Nice to hear. I already filled a bug report: http://d.puremagic.com/issues/show_bug.cgi?id=10999
Sep 10 2013