www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - final struct ?

reply wjoe <invalid example.com> writes:
As I was reading a few source files of a library I found dozens 
of final struct declarations like this:

final struct Foo {

   const pure final nothrow bar() { ... }

}

What's this supposed to express ?

A final class is a class that can't be subclassed - structs can't 
be subclassed, so does that express something else or is the 
final redundant ?
Additionally since it's not possible to subclass a struct it's 
also not possible to override bar() - does 'final bar()' mean 
something else ?

The only relevant bits of my web search for 'dlang: final' were 
results for final class, virtual functions and final switch. Am I 
missing something ?
May 19 2020
parent reply user1234 <user1234 12.de> writes:
On Tuesday, 19 May 2020 at 10:01:34 UTC, wjoe wrote:
 As I was reading a few source files of a library I found dozens 
 of final struct declarations like this:

 final struct Foo {

   const pure final nothrow bar() { ... }

 }

 What's this supposed to express ?

 A final class is a class that can't be subclassed - structs 
 can't be subclassed, so does that express something else or is 
 the final redundant ?
 Additionally since it's not possible to subclass a struct it's 
 also not possible to override bar() - does 'final bar()' mean 
 something else ?

 The only relevant bits of my web search for 'dlang: final' were 
 results for final class, virtual functions and final switch. Am 
 I missing something ?
It has no purpose. In D many attributes are allowed even if they have no meaning. D-Scanner checks this kind of stuff, to some extent, but not the compiler.
May 19 2020
parent reply wjoe <invalid example.com> writes:
On Tuesday, 19 May 2020 at 10:08:37 UTC, user1234 wrote:
 On Tuesday, 19 May 2020 at 10:01:34 UTC, wjoe wrote:
 [...]
It has no purpose. In D many attributes are allowed even if they have no meaning. D-Scanner checks this kind of stuff, to some extent, but not the compiler.
Thank you.
May 19 2020
parent reply user1234 <user123 12.de> writes:
On Tuesday, 19 May 2020 at 10:29:51 UTC, wjoe wrote:
 On Tuesday, 19 May 2020 at 10:08:37 UTC, user1234 wrote:
 On Tuesday, 19 May 2020 at 10:01:34 UTC, wjoe wrote:
 [...]
It has no purpose. In D many attributes are allowed even if they have no meaning. D-Scanner checks this kind of stuff, to some extent, but not the compiler.
Thank you.
A little sample to show you more cases of attributes that have no effect: --- struct Foo { nothrow nogc int field; // why not ? void func() { void nested() const { field++; // mutation is allowed because const is a noop extern int j; // extern decl in a nested func... } nothrow i = 8; // just like auto pure f = 9; // just like auto safe s = 10; // just like auto system t = s; // just like auto } } void main() { } ---
May 19 2020
parent wjoe <invalid example.com> writes:
On Wednesday, 20 May 2020 at 04:40:33 UTC, user1234 wrote:
 On Tuesday, 19 May 2020 at 10:29:51 UTC, wjoe wrote:
 On Tuesday, 19 May 2020 at 10:08:37 UTC, user1234 wrote:
 [...]
Thank you.
A little sample to show you more cases of attributes that have no effect: --- struct Foo { nothrow nogc int field; // why not ? void func() { void nested() const { field++; // mutation is allowed because const is a noop extern int j; // extern decl in a nested func... } nothrow i = 8; // just like auto pure f = 9; // just like auto safe s = 10; // just like auto system t = s; // just like auto } } void main() { } ---
much appreciated :)
May 20 2020