This syntax regarding null checking baffles me
kdevel
kdevel at vogtner.de
Sun Jan 10 21:52:29 UTC 2021
On Sunday, 10 January 2021 at 19:21:47 UTC, Paul Backus wrote:
> On Sunday, 10 January 2021 at 17:28:42 UTC, kdevel wrote:
>> On Sunday, 10 January 2021 at 16:44:29 UTC, Paul Backus wrote:
>>
>> [...]
>>
>>> Your opCast has the wrong signature.
>>
>> "Wrong" in what respect?
>>
>
> It does not match the signature required by the language spec:
>
> https://dlang.org/spec/operatoroverloading.html#cast
Sure, but it is called anyway:
```null3.d
import std.stdio: writeln;
class C {
int i;
bool opCast ()
{
__PRETTY_FUNCTION__.writeln;
return false;
}
}
void main ()
{
C c = new C;
cast (bool) c; // print bool null3.C.opCast()
c = null;
cast (bool) c; // Segmentation fault
}
```
$ dmd null3 && ./null3
bool null3.C.opCast()
Segmentation fault
As ag0aep6g pointed out
if (c)
does not invoke the opCast member. Neither one of the signatures
bool opCast ()
T opCast (T: bool) ().
This "conversion to bool" [1] of the class variable seems to be
not
programmatically accessible.
[1] "Class references are converted to bool by checking to see if
the class reference is null or not." from § 20.2.1
More information about the Digitalmars-d
mailing list