Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
September 08, 2013 Limited type matching? | ||||
---|---|---|---|---|
| ||||
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]? |
September 09, 2013 Re: Limited type matching? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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...
|
September 09, 2013 Re: Limited type matching? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On 9/9/13, Namespace <rswhite4@googlemail.com> wrote:
> Ok I fill a bug for that...
Please do, that code should compile.
|
September 09, 2013 Re: Limited type matching? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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.
|
September 09, 2013 Re: Limited type matching? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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.
|
September 10, 2013 Re: Limited type matching? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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
|
September 10, 2013 Re: Limited type matching? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | > 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.
|
September 10, 2013 Re: Limited type matching? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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
|
September 10, 2013 Re: Limited type matching? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | 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 |
Copyright © 1999-2021 by the D Language Foundation