2 class issues

ag0aep6g anonymous at example.com
Thu Mar 7 20:11:47 UTC 2019


On 07.03.19 11:38, spir wrote:
> -1- How to enforce that subclasses implement given methods without using 
> "abstract", which seems to make the whole class abstract?

Not, as far as I can tell. You can't force derived classes to override 
an existing implementation. And you can't omit the implementation 
without making the class abstract.

> -2- How to have "constant" (predefined) class instances at the 
> module-level?

Just like so?

const o = new Object; /* works for me */

> The compiler requires a "static this ()".

For what code does it say that?

> What does this 
> actually mean (for a constructor)? What are the consequences, for either 
> my code or client code? (The doc on the topic [2] is rather obscure for 
> me, and I could not find better elsewhere.)
> 
> I'm also bluffed by "Static constructors have empty parameter lists." 
> Does this mean I should manually fill the fields? (not a big deal, but 
> why???) This may give:
>      // Predefined pseudo-pattern "End-of-Text":
>      auto EoT = new Pattern() ;       // ???
>      EoT.name = "EoT" ;
> 
>      // Unique lexeme "end-of-text":
>      auto eot = new Lexeme() ;       // ???
>      eot.patname = "EoT" ;
>      eot.slice = null ;
>      eot.index = uint.max ;
> Then, why have a constructor at all? This would also prevent me from 
> making classes immutable, while conceptually all are immutable... (no 
> reason for a pattern or a lexeme to change)

You're misunderstanding the nature of static constructors.

Static constructors are a special kind of function that runs once at the 
beginning of the program/thread, automatically. They're not constructors 
for static objects. You can't call static constructors from your code. 
`new Foo` calls a (normal) constructor; doesn't matter if you're 
creating a dynamic instance or a static one.


More information about the Digitalmars-d-learn mailing list