www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What the abstrac final class mean?

reply lili <akozhao tencent.com> writes:
Hi:
     Why need defined an abstract final class?
     see  
https://github.com/Rikarin/Trinix/blob/master/Kernel/arch/amd64/gdt.d
Aug 12 2019
next sibling parent reply Alex <sascha.orlov gmail.com> writes:
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...
Aug 12 2019
parent reply a11e99z <black80 bk.ru> writes:
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
Aug 12 2019
parent reply Jacob Carlborg <doob me.com> writes:
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
Aug 13 2019
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Aug 13 2019
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Aug 12 2019