Bug 18031 – Empty this compiles

Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Mac OS X
Creation time
2017-12-04T18:18:01Z
Last change time
2017-12-04T18:50:40Z
Assigned to
No Owner
Creator
Jonathan Wilbur

Comments

Comment #0 by jonathan — 2017-12-04T18:18:01Z
``` public struct CommandLineInterfaceOption { immutable public string token; immutable public void function (string) callback; this (string token, void function (string) callback); } ``` This compiles, even though `this` is undefined. Is this supposed to happen?
Comment #1 by issues.dlang — 2017-12-04T18:50:40Z
It's perfectly legal to declare functions without bodies. That's what you would typically do in a .di file. The actual function body would then be in the corresponding .d file. Most of the time, there's no reason to bother with .di files, but in order for them to work, it's required that any function that does not absolutely need its body to be present be able to not have its body present (stuff like auto return functions and templated functions require their bodies, but a normal function doesn't). Similarly, in some cases, the body may be in a separate library entirely (though that's pretty much only with extern(C) functions where the D declaration is just so that the D code can use an existing C function). You'll get a linker error as soon as you try to use the function if a function body hasn't been linked in.