Unexpectedly nice case of auto return type

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Dec 3 13:45:06 UTC 2019

On Tuesday, December 3, 2019 3:23:20 AM MST Basile B. via Digitalmars-d-
learn wrote:
> On Tuesday, 3 December 2019 at 10:19:02 UTC, Jonathan M Davis
> wrote:
> > On Tuesday, December 3, 2019 3:03:22 AM MST Basile B. via
> >
> > Digitalmars-d- learn wrote:
> >> [...]
> >
> > There isn't much point in giving the type of null an explicit
> > name given that it doesn't come up very often, and typeof(null)
> > is quite explicit about what the type is. Also, anyone doing
> > much generic programming in D is going to be well versed in
> > typeof. They might not know about typeof(null) explicitly, but
> > they should recognize what it means when they see it, and if
> > someone were trying to get the type of null, it would be the
> > obvious thing to try anyway. And typeof(null) isn't even the
> > prime case where typeof gets used on something other than an
> > object. From what I've seen, typeof(return) gets used far more.
> >
> > [...]
> you're right but I see two cases:
> - transpiling
> - header generation

I don't see why either of those would be a problem. For a transpiler,
typeof(null) and an explicit type name for typeof(null) would be the same
thing, and it would have to be translated to something that would work in
the other language either way. As for header generation, do you mean .di
files? They'd just use typeof(null) explicitly, whereas if you're talking
about something like having extern(C) functions in D and declaring a
corresponding .h file, typeof(null) wouldn't work anyway, because there is
no equivalent in C. In either case, whether you represent the type as
typeof(null) or by an explicit name in the D source code is irrelevant. It
would mean the same thing regardless. Having an explicit name wouldn't
really be any different from declaring an alias like

alias TypeOfNull = typeof(null);

The type is the same either way and would be treated the same by the
compiler or by any tool that needed to translate it to another language.
It's what the type is that matters, not how its represented in the D source
code. The only real difference would be what the programmer would see when
interacting with the D source code. It would be like arguing over whether
the root class object should be called Object or Root. It would be the same
thing either way, just with a different name.

- Jonathan M Davis

More information about the Digitalmars-d-learn mailing list