Express "Class argument may not be null" ?
Johan via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Aug 9 10:05:07 PDT 2017
On Wednesday, 9 August 2017 at 12:47:49 UTC, Steven Schveighoffer
wrote:
> On 8/8/17 3:59 PM, Johan Engelen wrote:
>>
>> In C++, it is clear that the _caller_ is doing the
>> dereferencing, and the dereference is also explicit.
>
> In fact it's not doing any dereferencing. It's just under the
> hood passing a pointer.
Come on Steven, I thought you knew I am a compiler dev.
The caller _is_ doing dereferencing. That's what the '*' means
semantically and it matters. [1]
>>> However, the in contract does actually enforce the
>>> requirement.
>>
>> And adds null pointer checks even when clearly not needed.
>
> Clearly not needed? I thought the point was to ensure the
> reference is not null?
I meant code like this: `k->something(); foo(*k);` or `auto k =
new Klass(); foo(*k);`.
Anyway, the point was to express the intent nicely.
I wasn't expecting having to explain the merits of passing by
reference.
- Johan
[1] Going off-topic.
The alternative is dereferencing inside the callee (which is
apparently the only option D semantically provides for classes).
I showed the impact of this semantic difference in my DConf 2017
talk; here are links to examples:
https://godbolt.org/g/rgvHTC (C++)
https://godbolt.org/g/NCTeXo (D)
It's a pity we are not able to pass classes "by reference" (per
C++ parlance), for reasoning and readability, and performance. I
still have to come up with a rationale for not having an explicit
"*" for classes for when I am going to teach D.
More information about the Digitalmars-d-learn
mailing list