April 27, 2013 Re: 1 matches bool, 2 matches long | ||||
---|---|---|---|---|
| ||||
Posted in reply to Luís Marques | On Saturday, 27 April 2013 at 05:54:48 UTC, Luís Marques wrote: > Is this what some of you are asking for? > > bool a = true; // ok > bool b = false; // ok > bool c = 1; // error, no implicit conversion > bool c = getInt(); // error? ok? Last 2 are error. > int x = 42; > if(x) { ... } // ok (doesn't this imply c = getInt() ok too? > if(42) { ... } // ok if insert a cast already, so all are ok. |
April 27, 2013 Re: 1 matches bool, 2 matches long | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Friday, 26 April 2013 at 18:22:10 UTC, Walter Bright wrote:
> On 4/26/2013 6:51 AM, deadalnix wrote:
>> The last time I experienced that feature was with a char getting casted to bool
>> implicitly and then appended to a string, causing super weird behavior after
>> when using the resulting (corrupted) string.
>
>
> void main()
> {
> bool b = 'c';
> }
>
> dmd -c foo
> foo.d(4): Error: cannot implicitly convert expression ('c') of type char to bool
import std.stdio;
void main()
{
bool c0 = '\1'; //compiled OK
bool c1 = '\0'; //compiled OK
writeln(c0,' ',c1); //output: true false
}
|
April 27, 2013 Re: 1 matches bool, 2 matches long | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright Attachments:
| First, I can guess that why Walter disagree *fixing* this problem. http://dlang.org/overview.html > Major Design Goals of D > 9. Where D code looks the same as C code, have it either behave the same or issue an error. Based on the design goal, we should not change the behavior toward foo(1) matching to long version. It will change the code behavior that looks like C. But, we can raise an "ambiguous error" for the case. I think we need to add a "special rule" for more natural overload resolution in D. // The expected behavior we can do at the most extern(C) int printf(const char*, ...); void foo(bool) { printf("bool\n"); } void foo(long) { printf("long\n"); } void main() { foo(0); // Error: function foo called with argument types: (int) matches both foo(bool) and foo(long) foo(1); // Error: function foo called with argument types: (int) matches both foo(bool) and foo(long) foo(2); // OK, matches to long } Kenji Hara 2013/4/27 Walter Bright <newshound2@digitalmars.com> > On 4/26/2013 1:59 PM, Andrej Mitrovic wrote: > >> On 4/26/13, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote: >> >>> An even better example: >>> >> >> import std.stdio; >> >> void foo(bool x) { writeln("1"); } >> void foo(long x) { writeln("2"); } >> >> void main() >> { >> foo(1); // "1" >> foo(false ? 2 : 1); // "2" >> } >> >> Kill it with fire. >> > > How about this one: > > import std.stdio; > > void foo(short x) { writeln("1"); } > > void foo(long x) { writeln("2"); } > > void main() > { > foo(30000); // "1" > foo(false ? 40000 : 30000); // "2" > } > |
April 27, 2013 Re: 1 matches bool, 2 matches long | ||||
---|---|---|---|---|
| ||||
On 4/27/13, kenji hara <k.hara.pg@gmail.com> wrote:
> // The expected behavior we can do at the most
> extern(C) int printf(const char*, ...);
> void foo(bool) { printf("bool\n"); }
> void foo(long) { printf("long\n"); }
> void main()
> {
> foo(0); // Error: function foo called with argument types: (int) matches
> both foo(bool) and foo(long)
> foo(1); // Error: function foo called with argument types: (int) matches
> both foo(bool) and foo(long)
> foo(2); // OK, matches to long
> }
That's even more stupid. We need *less* special cases, not more. We already have true/false, they're keywords, any other integral literal (*literal*, not expression) should not implicitly convert to bool.
|
April 27, 2013 Re: 1 matches bool, 2 matches long | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic:
> That's even more stupid.
Please be gentle. That from Kenji is one of the few usable ideas of this thread.
Bye,
bearophile
|
April 27, 2013 Re: 1 matches bool, 2 matches long | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Saturday, 27 April 2013 at 12:20:00 UTC, bearophile wrote:
> Andrej Mitrovic:
>
>> That's even more stupid.
>
> Please be gentle. That from Kenji is one of the few usable ideas of this thread.
>
> Bye,
> bearophile
No, that is even worse.
|
April 27, 2013 Re: 1 matches bool, 2 matches long | ||||
---|---|---|---|---|
| ||||
Posted in reply to kenji hara | On Saturday, 27 April 2013 at 11:41:30 UTC, kenji hara wrote:
> First, I can guess that why Walter disagree *fixing* this problem.
>
> http://dlang.org/overview.html
>> Major Design Goals of D
>> 9. Where D code looks the same as C code, have it either behave the same
> or issue an error.
>
C doesn't have a bool type, so how can D behave the same?
|
April 27, 2013 Re: 1 matches bool, 2 matches long | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas Mina Attachments:
| OK. I misunderstood. C does not allow function overloading, so same problem is not there. In C++, // test.cpp #include <stdio.h> void foo(bool) { printf("bool\n"); } void foo(long) { printf("long\n"); } int main(int argc, char **argv) { foo(false); // matches bool version foo(true); // matches bool version foo(0); // ambiguous foo(1); // ambiguous foo(2); // ambiguous return 0; } The behavior is same with GCC 4.7.2 (using msys) and dmc. Walter, now I changed my opinion. It seems not correct that being regarded bool type as one of the integer. How about? Kenji Hara 2013/4/27 Minas Mina <minas_mina1990@hotmail.co.uk> > On Saturday, 27 April 2013 at 11:41:30 UTC, kenji hara wrote: > >> First, I can guess that why Walter disagree *fixing* this problem. >> >> http://dlang.org/overview.html >> >>> Major Design Goals of D >>> 9. Where D code looks the same as C code, have it either behave the same >>> >> or issue an error. >> >> > C doesn't have a bool type, so how can D behave the same? > |
April 27, 2013 Re: 1 matches bool, 2 matches long | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On Saturday, 27 April 2013 at 10:17:58 UTC, deadalnix wrote:
> On Friday, 26 April 2013 at 21:37:14 UTC, Walter Bright wrote:
>> On 4/26/2013 1:59 PM, Andrej Mitrovic wrote:
>>> On 4/26/13, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
>>>> An even better example:
>>>
>>> import std.stdio;
>>>
>>> void foo(bool x) { writeln("1"); }
>>> void foo(long x) { writeln("2"); }
>>>
>>> void main()
>>> {
>>> foo(1); // "1"
>>> foo(false ? 2 : 1); // "2"
>>> }
>>>
>>> Kill it with fire.
>>
>> How about this one:
>>
>> import std.stdio;
>>
>> void foo(short x) { writeln("1"); }
>> void foo(long x) { writeln("2"); }
>>
>> void main()
>> {
>> foo(30000); // "1"
>> foo(false ? 40000 : 30000); // "2"
>> }
>
> VRP should be used as a fallback mechanism. Yes, kill it with fire.
I agree 100% VRP is nice but it should not be used implicitly to resolve ambiguous cases, that definitely goes against least surprise and all that.
|
April 27, 2013 Re: 1 matches bool, 2 matches long | ||||
---|---|---|---|---|
| ||||
Posted in reply to kenji hara | One of the main problem that I saw here, is this behavior (DMD 2.062): import std.stdio; void foo(bool b) { writeln("bool"); } void foo(long l) { writeln("long"); } void main() { int num = 0; foo(num); foo(0); foo(2); } output: long bool long As num = 0, for me both: "foo(num)" and "foo(0)" should give the same output ("long"), regardless that num is integer implicit declaration or not. |
Copyright © 1999-2021 by the D Language Foundation