Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
April 23, 2015 Return data from different types of conditional operation | ||||
---|---|---|---|---|
| ||||
Hi, Why the program can not return different types of data from the conditional operator? ----- import std.stdio; auto foo() { if (true) { return 0; } else return "true"; } void main() { writeln(foo); } |
April 23, 2015 Re: Return data from different types of conditional operation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Thursday, 23 April 2015 at 09:48:21 UTC, Dennis Ritchie wrote:
> Hi,
> Why the program can not return different types of data from the conditional operator?
>
> -----
> import std.stdio;
>
> auto foo() {
>
> if (true) {
> return 0;
> } else
> return "true";
> }
>
> void main() {
>
> writeln(foo);
> }
Because 0 is an int and "true" is a string.
They're totally different types, and in a statically typed language like D, that just wont work.
|
April 23, 2015 Re: Return data from different types of conditional operation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Thursday, 23 April 2015 at 09:48:21 UTC, Dennis Ritchie wrote:
> Hi,
> Why the program can not return different types of data from the conditional operator?
>
> -----
> import std.stdio;
>
> auto foo() {
>
> if (true) {
> return 0;
> } else
> return "true";
> }
>
> void main() {
>
> writeln(foo);
> }
import std.variant, std.stdio;
auto foo()
{
if (true)
return Variant(0);
else
return Variant("Hello");
}
void main()
{
foo.writeln;
}
|
April 23, 2015 Re: Return data from different types of conditional operation | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Thursday, 23 April 2015 at 10:06:45 UTC, John Colvin wrote:
> On Thursday, 23 April 2015 at 09:48:21 UTC, Dennis Ritchie wrote:
>> Hi,
>> Why the program can not return different types of data from the conditional operator?
>>
>> -----
>> import std.stdio;
>>
>> auto foo() {
>>
>> if (true) {
>> return 0;
>> } else
>> return "true";
>> }
>>
>> void main() {
>>
>> writeln(foo);
>> }
>
> import std.variant, std.stdio;
>
> auto foo()
> {
> if (true)
> return Variant(0);
> else
> return Variant("Hello");
> }
>
> void main()
> {
> foo.writeln;
> }
If 'true' is known at compile time, it works:
auto foo() {
static if (true) {
return 0;
} else
return "true";
}
|
April 23, 2015 Re: Return data from different types of conditional operation | ||||
---|---|---|---|---|
| ||||
Posted in reply to rumbu | Thank you all. I did not know before, that this behavior is characteristic of dynamically typed programming languages. |
April 23, 2015 Re: Return data from different types of conditional operation | ||||
---|---|---|---|---|
| ||||
Posted in reply to rumbu | On Thursday, 23 April 2015 at 10:26:09 UTC, rumbu wrote:
> On Thursday, 23 April 2015 at 10:06:45 UTC, John Colvin wrote:
>> On Thursday, 23 April 2015 at 09:48:21 UTC, Dennis Ritchie wrote:
>>> Hi,
>>> Why the program can not return different types of data from the conditional operator?
>>>
>>> -----
>>> import std.stdio;
>>>
>>> auto foo() {
>>>
>>> if (true) {
>>> return 0;
>>> } else
>>> return "true";
>>> }
>>>
>>> void main() {
>>>
>>> writeln(foo);
>>> }
>>
>> import std.variant, std.stdio;
>>
>> auto foo()
>> {
>> if (true)
>> return Variant(0);
>> else
>> return Variant("Hello");
>> }
>>
>> void main()
>> {
>> foo.writeln;
>> }
>
> If 'true' is known at compile time, it works:
>
> auto foo() {
>
> static if (true) {
> return 0;
> } else
> return "true";
> }
Yes, but
auto foo() {
static if (true) {
return 0;
} else
this(statment) is [not.parsed];
}
so it's not just working around a problem of returned type inference.
|
Copyright © 1999-2021 by the D Language Foundation