Discussion Thread: DIP 1034--Add a Bottom Type (reboot)--Final Review
Steven Schveighoffer
schveiguy at gmail.com
Tue Sep 22 15:46:39 UTC 2020
Continuing discussion from feedback thread:
On 9/22/20 11:08 AM, Dennis wrote:
> On Tuesday, 22 September 2020 at 14:16:19 UTC, Steven Schveighoffer wrote:
>> ```
>> int x;
>> int bar();
>> auto foo() {
>> cast(noreturn) (x = bar()); // same as {x = bar(); assert(0);}
>> }
>> ```
>>
>> This is odd, and I'm not understanding the reason for the cast if you
>> aren't assigning.
>
> This code has no reason, it's just an example of the rewrite that
> happens with `cast(noreturn)`. It could actually be useful in generic
> code, or when you are using a function that is noreturn but does not
> have the correct return type.
> ```
> // someone else's code
> void panic(string msg);
>
> // your code:
> auto f = () => cast(noreturn) panic("error");
> ```
The difference in the second example (which makes sense to me) is that
you are returning the noreturn value.
If the first example said:
```
return cast(noreturn) (x = bar());
```
Then I understand. I thought the code was making an explicit point that
not using the casted result still results in an assert(0).
>
>> Also, earlier you say that you can declare a noreturn variable, but as
>> long as you don't use it, you don't generate the assert 0. Is this
>> example a contradiction of that?
>
> I don't think so, the example does not declare a local variable. I could
> rewrite it to this:
>
> ```
> import std;
> int x;
> int bar();
> auto foo() {
> noreturn y; // no assert here yet
> writeln("this will get printed");
> y = cast(noreturn) (x = bar());
> // here it results in assert(0)
> }
> ```
>
> The reason for this is argued by Johannes Loher here:
> https://forum.dlang.org/post/r8v8tt$14fk$1@digitalmars.com
>
>> ```
>> /* 6 */ !(is(T == function)) || is(noreturn* : T)
>> /* 7 */ !(is(T == delegate)) || is(noreturn* : T)
>> ```
>>
>> What are these trying to say? I think these rules need some
>> clarification or rewriting
>
> In formal logic, "(not p) or q" is equivalent to "if p then q",
OK, I see what you are trying to do. The parentheses make it hard to see
what it's trying to say. A comment or more specific explanation may help.
BTW, is(T == function) only works on actual function types, not function
pointers. I think you meant function pointers.
-Steve
More information about the Digitalmars-d
mailing list