Thread overview
What the abstrac final class mean?
Aug 12, 2019
lili
Aug 12, 2019
Alex
Aug 12, 2019
a11e99z
Aug 13, 2019
Jacob Carlborg
Aug 13, 2019
Jonathan M Davis
Aug 12, 2019
Jonathan M Davis
August 12, 2019
Hi:
    Why need defined an abstract final class?
    see  https://github.com/Rikarin/Trinix/blob/master/Kernel/arch/amd64/gdt.d
August 12, 2019
On Monday, 12 August 2019 at 08:54:56 UTC, lili wrote:
> Hi:
>     Why need defined an abstract final class?
>     see  https://github.com/Rikarin/Trinix/blob/master/Kernel/arch/amd64/gdt.d

From what I saw, all members are static. So, this is a kind of utility class, which is not supposed to be instantiated, nor to be derived from. Maybe, it serves like a namespace, for convenience...
August 12, 2019
On Monday, 12 August 2019 at 09:16:19 UTC, Alex wrote:
> On Monday, 12 August 2019 at 08:54:56 UTC, lili wrote:
>> Hi:
>>     Why need defined an abstract final class?
>>     see  https://github.com/Rikarin/Trinix/blob/master/Kernel/arch/amd64/gdt.d
>
> From what I saw, all members are static. So, this is a kind of utility class, which is not supposed to be instantiated, nor to be derived from. Maybe, it serves like a namespace, for convenience...

its weird that next compiles in some weird form

import std;
static class A {
    static a() { "a".writeln; } // forgot return type
}
void main() {
    A.a();

    A b, c; // 2 instances with diff addr
    "%s %s".writefln( &b, &c );
}

// and for static struct DMD gives to A size 1b, LDC - 8b
August 12, 2019
On Monday, August 12, 2019 2:54:56 AM MDT lili via Digitalmars-d-learn wrote:
> Hi:
>      Why need defined an abstract final class?
>      see
> https://github.com/Rikarin/Trinix/blob/master/Kernel/arch/amd64/gdt.d

It's one way to effectively create a namespace using a class. Another way would be to declare the class final, @disable its default constructor, and provide no other constructors. Either way, the result is that the class cannot be instantiated and any static functions declared within the class have to use the class' name when referencing them, whereas if they were free functions within a module, a non-static import would allow you to refer to them directly.

For better or worse, std.datetime does it with the class Clock to force the functions for getting the time to all refer to Clock - e.g. Clock.currTime(), whereas if they were directly in the module, it would be possible to do something like currTime().

- Jonathan M Davis



August 13, 2019
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.

-- 
/Jacob Carlborg
August 13, 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