What the abstrac final class mean?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Aug 13 09:24:41 UTC 2019

On Tuesday, August 13, 2019 3:03:49 AM MDT Jacob Carlborg via Digitalmars-d-
learn wrote:
> On 2019-08-12 11:25, a11e99z wrote:
> > its weird that next compiles in some weird form
> >
> > import std;
> > static class A {
> >
> >      static a() { "a".writeln; } // forgot return type
> >
> > }
> Since you have specified an attribute on "a", the compiler can infer the
> return type. In this case it's inferred to "void" since there is no
> return statement.
> It's not really the attribute that makes it possible for the compiler to
> infer the return type, it probably can in most of the cases. It just
> needs to be an attribute to satisfy the parser.

Indeed. Basically, the compiler needs _something_ there which goes with a
function to make it happy. The same goes with variable declarations. Many
people mistakenly think that auto tells the compiler to infer the type when
in reality all it does is provide a placeholder to make the parser happy.
The type is _always_ inferred unless it's explicitly given, which is why
stuff such as enum, const, or ref can be used by themselves without
specifying the full type. However, because _something_ has to be there to
indicate that it's a declaration, auto is in the language so that the
programmer has a way to indicate that it's a variable or function
declaration. static by itself is plenty to indicate that a declaration is
being provided. auto or void could be used, but they're not necessary.

- Jonathan M Davis

More information about the Digitalmars-d-learn mailing list