www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why is this not allowed?

reply ryuukk_ <ryuukk.dev gmail.com> writes:
Why is this allowed
```D

struct EntityDef
{
     struct
     {
         int hp;
     }
}
```


But not this fucking thing?

```D
struct EntityDef
{
     struct
     {
         int hp;
     } stats;
}
```


Let me name my shit


No, i don't want to do:


```D
struct EntityDef
{
     struct Stats
     {
         int hp;
     } Stats stats;
}
```

Repeating the same name 3 times, i should go back to the stone 
age too no?

C and all other C like languages allow me to be concise

Why is it a D thing to be backward?
Oct 04
next sibling parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
I don't know why it isn't supported.

Its very useful with bindings to C.

The parser should be able to swap it to a named instance with a 
generated name.
Oct 04
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Saturday, 5 October 2024 at 06:43:00 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 I don't know why it isn't supported.

 Its very useful with bindings to C.

 The parser should be able to swap it to a named instance with a 
 generated name.
Let's PR and let's merge
Oct 05
parent reply Sergey <kornburn yandex.ru> writes:
On Saturday, 5 October 2024 at 10:35:30 UTC, ryuukk_ wrote:
 On Saturday, 5 October 2024 at 06:43:00 UTC, Richard (Rikki) 
 Andrew Cattermole wrote:
 I don't know why it isn't supported.

 Its very useful with bindings to C.

 The parser should be able to swap it to a named instance with 
 a generated name.
Let's PR and let's merge
btw not new topic.. https://forum.dlang.org/thread/pblaqxrrjypswtmtnjhd forum.dlang.org?page=1 But I think you will need a DIP for that
Oct 05
parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Saturday, 5 October 2024 at 17:41:13 UTC, Sergey wrote:
 On Saturday, 5 October 2024 at 10:35:30 UTC, ryuukk_ wrote:
 On Saturday, 5 October 2024 at 06:43:00 UTC, Richard (Rikki) 
 Andrew Cattermole wrote:
 I don't know why it isn't supported.

 Its very useful with bindings to C.

 The parser should be able to swap it to a named instance with 
 a generated name.
Let's PR and let's merge
btw not new topic.. https://forum.dlang.org/thread/pblaqxrrjypswtmtnjhd forum.dlang.org?page=1 But I think you will need a DIP for that
There is no need for a DIP to be able to name something Whoever knows how to do it, just submit a PR, we'll discuss there Notice, what happened to those people in that linked thread? Yeah right..
Oct 05
prev sibling next sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:
 No, i don't want to do:


 ```C
 struct EntityDef
 {
     struct Stats
     {
         int hp;
     } stats;
 }
 ```

 Repeating the same name 3 times, i should go back to the stone 
 age too no?

 C and all other C like languages allow me to be concise

 Why is it a D thing to be backward?
In the coding scheme, fields/members/methods may be at the beginning, middle, or end of the structure, or may not be identifiers to the right of the anonymous structs. In structures, a lot (I wish it was in bitfield) has been taken from C. Especially not using typedef and not having extra semicolons make D stand out even with these. As for anonymous structures, they have to be like this in order to be used with unions. I think everything is as it should be, and even more: Please include the relevant comment line (the // characters next to the anonymous struct) in the code and be amazed by the change :) ```d struct Foo {   int bar;    //struct {/*   Baz baz;   struct Baz  {       auto opAssign(int value)         => baz = value;//*/       int baz;   } } void main() {    Foo foo;    foo.bar = 7;    foo.baz = 42;        imported!"std.stdio".writeln(foo); /*    with opAssign()     Anonymous    Foo(7, Baz(42)) or  Foo(7, 42)    */ } ``` Thank you to the creators and maintainers of the D. SDB 79
Oct 05
next sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Saturday, 5 October 2024 at 16:40:46 UTC, Salih Dincer wrote:
 On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:
 No, i don't want to do:


 ```C
 struct EntityDef
 {
     struct Stats
     {
         int hp;
     } stats;
 }
 ```

 Repeating the same name 3 times, i should go back to the stone 
 age too no?

 C and all other C like languages allow me to be concise

 Why is it a D thing to be backward?
In the coding scheme, fields/members/methods may be at the beginning, middle, or end of the structure, or may not be identifiers to the right of the anonymous structs. In structures, a lot (I wish it was in bitfield) has been taken from C. Especially not using typedef and not having extra semicolons make D stand out even with these. As for anonymous structures, they have to be like this in order to be used with unions. I think everything is as it should be, and even more: Please include the relevant comment line (the // characters next to the anonymous struct) in the code and be amazed by the change :) ```d struct Foo {   int bar;    //struct {/*   Baz baz;   struct Baz  {       auto opAssign(int value)         => baz = value;//*/       int baz;   } } void main() {    Foo foo;    foo.bar = 7;    foo.baz = 42;        imported!"std.stdio".writeln(foo); /*    with opAssign()     Anonymous    Foo(7, Baz(42)) or  Foo(7, 42)    */ } ``` Thank you to the creators and maintainers of the D. SDB 79
I'll pretend you didn't say anything, because your solution is just bad
Oct 05
prev sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Saturday, 5 October 2024 at 16:40:46 UTC, Salih Dincer wrote:
 On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:
 No, i don't want to do:


 ```C
 struct EntityDef
 {
     struct Stats
     {
         int hp;
     } stats;
 }
 ```

 Repeating the same name 3 times, i should go back to the stone 
 age too no?

 C and all other C like languages allow me to be concise

 Why is it a D thing to be backward?
In the coding scheme, fields/members/methods may be at the beginning, middle, or end of the structure, or may not be identifiers to the right of the anonymous structs. In structures, a lot (I wish it was in bitfield) has been taken from C. Especially not using typedef and not having extra semicolons make D stand out even with these. As for anonymous structures, they have to be like this in order to be used with unions. I think everything is as it should be, and even more: Please include the relevant comment line (the // characters next to the anonymous struct) in the code and be amazed by the change :) ```d struct Foo {   int bar;    //struct {/*   Baz baz;   struct Baz  {       auto opAssign(int value)         => baz = value;//*/       int baz;   } } void main() {    Foo foo;    foo.bar = 7;    foo.baz = 42;        imported!"std.stdio".writeln(foo); /*    with opAssign()     Anonymous    Foo(7, Baz(42)) or  Foo(7, 42)    */ } ``` Thank you to the creators and maintainers of the D. SDB 79
I literally don't understand what your code does I'm not sure this is related to my suggestion
Oct 05
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:
 Why is this allowed
 ```D

 struct EntityDef
 {
     struct
     {
         int hp;
     }
 }
 ```


 But not this fucking thing?

 ```D
 struct EntityDef
 {
     struct
     {
         int hp;
     } stats;
 }
 ```


 Let me name my shit


 No, i don't want to do:


 ```D
 struct EntityDef
 {
     struct Stats
     {
         int hp;
     } Stats stats;
 }
 ```

 Repeating the same name 3 times, i should go back to the stone 
 age too no?

 C and all other C like languages allow me to be concise

 Why is it a D thing to be backward?
It’s the semicolon. As soon as the closing brace, the declaration is over. You would have to invent new syntax. -Steve
Oct 05
next sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Saturday, 5 October 2024 at 17:26:59 UTC, Steven Schveighoffer 
wrote:
 On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:
 Why is this allowed
 ```D

 struct EntityDef
 {
     struct
     {
         int hp;
     }
 }
 ```


 But not this fucking thing?

 ```D
 struct EntityDef
 {
     struct
     {
         int hp;
     } stats;
 }
 ```


 Let me name my shit


 No, i don't want to do:


 ```D
 struct EntityDef
 {
     struct Stats
     {
         int hp;
     } Stats stats;
 }
 ```

 Repeating the same name 3 times, i should go back to the stone 
 age too no?

 C and all other C like languages allow me to be concise

 Why is it a D thing to be backward?
It’s the semicolon. As soon as the closing brace, the declaration is over. You would have to invent new syntax. -Steve
There is no new syntax to invent instead of writing this error: ``` onlineapp.d(8): Error: no identifier for declarator `stats` ``` you generate a random identifier and assign it ```D extern(D) static Identifier generateAnonymousId(const(char)[] name) ``` https://github.com/dlang/dmd/blob/master/compiler/src/dmd/identifier.d#L165 I'm very far from being bright, but i know simple logic
Oct 05
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On Sunday, 6 October 2024 at 05:41:10 UTC, ryuukk_ wrote:
 There is no new syntax to invent

 instead of writing this error:
 ```
 onlineapp.d(8): Error: no identifier for declarator `stats`
 ```

 you generate a random identifier and assign it

 ```D
     extern(D) static Identifier 
 generateAnonymousId(const(char)[] name)
 ```

 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/identifier.d#L165

 I'm very far from being bright, but i know simple logic
The problem is not the ability to generate an id, the problem is that the declaration is over. Anything after the closing brace is a *new declaration*. Whitespace is not significant in D, so you have to invent a new syntax to to mean the declaration continues. D sees this: ```d struct { int x; int y; } something; ``` as this: ```d struct { int x; int y; } // DECLARATION OF ANONYMOUS STRUCT OVER. something; // this is something different, unrelated to the struct above. ``` One declaration defines an anonymous struct, and one that is a new declaration `something;`. Or if you are in a function, `something;` is treated as a statement. You can't change existing syntax, as that would break code, so you need new syntax. C allows this because structs are *required* to end with a semicolon. So the compiler knows the thing after the brace is part of the same declaration. -Steve
Oct 07
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Monday, 7 October 2024 at 18:06:30 UTC, Steven Schveighoffer 
wrote:
 On Sunday, 6 October 2024 at 05:41:10 UTC, ryuukk_ wrote:
 There is no new syntax to invent

 instead of writing this error:
 ```
 onlineapp.d(8): Error: no identifier for declarator `stats`
 ```

 you generate a random identifier and assign it

 ```D
     extern(D) static Identifier 
 generateAnonymousId(const(char)[] name)
 ```

 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/identifier.d#L165

 I'm very far from being bright, but i know simple logic
The problem is not the ability to generate an id, the problem is that the declaration is over. Anything after the closing brace is a *new declaration*. Whitespace is not significant in D, so you have to invent a new syntax to to mean the declaration continues. D sees this: ```d struct { int x; int y; } something; ``` as this: ```d struct { int x; int y; } // DECLARATION OF ANONYMOUS STRUCT OVER. something; // this is something different, unrelated to the struct above. ``` One declaration defines an anonymous struct, and one that is a new declaration `something;`. Or if you are in a function, `something;` is treated as a statement. You can't change existing syntax, as that would break code, so you need new syntax. C allows this because structs are *required* to end with a semicolon. So the compiler knows the thing after the brace is part of the same declaration. -Steve
Nonono, you overthink it Store last anonamous struct When you see a field without a type, you assign it with that anonymous struct D people love to complicate everything Keep things simple and solve people's problem Stop trying to write essays about simple stuff ALl the languages have solved this, D will now have move constructor lol, yet can't do basics well
Oct 08
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On Tuesday, 8 October 2024 at 15:10:54 UTC, ryuukk_ wrote:
 Nonono, you overthink it

 Store last anonamous struct

 When you see a field without a type, you assign it with that 
 anonymous struct
It doesn't work this way. The parser would have to lookahead to see if the next 2 tokens are an identifier and semicolon. And then close out the declaration in that case. It could possibly be unambiguous, but I suspect there may be ambiguities with current grammar. D grammar is *mostly* free of lookahead, but there are exceptions. It keeps things cleaner. I'm not saying I don't want this kind of feature, I'm saying I think you need new syntax. What's wrong with new syntax? This in itself is new syntax (to D). I've asked for struct lambdas in the past, in the context of passing a model to a template. This would be similar. -Steve
Oct 08
prev sibling parent reply Nick Treleaven <nick geany.org> writes:
On Saturday, 5 October 2024 at 17:26:59 UTC, Steven Schveighoffer 
wrote:
 It’s the semicolon. As soon as the closing brace, the 
 declaration is over. You would have to invent new syntax.
Maybe type tuple syntax will support this: ```d struct EntityDef { (int hp) stats; } EntityDef ed; int x = ed.stats.hp; ```
Oct 06
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Sunday, 6 October 2024 at 10:19:55 UTC, Nick Treleaven wrote:
 On Saturday, 5 October 2024 at 17:26:59 UTC, Steven 
 Schveighoffer wrote:
 It’s the semicolon. As soon as the closing brace, the 
 declaration is over. You would have to invent new syntax.
Maybe type tuple syntax will support this: ```d struct EntityDef { (int hp) stats; } EntityDef ed; int x = ed.stats.hp; ```
Let's focus on what exist today, i'm not looking for working on my game in 10 years I'm working on it TODAY, therefore i need a today's solution
Oct 07
parent reply Nick Treleaven <nick geany.org> writes:
On Monday, 7 October 2024 at 08:05:59 UTC, ryuukk_ wrote:
 I'm working on it TODAY, therefore i need a today's solution
```d import std.typecons; struct EntityDef { Tuple!(int, "hp") stats; } void main() { EntityDef ed; int x = ed.stats.hp; } ```
Oct 07
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Monday, 7 October 2024 at 09:22:20 UTC, Nick Treleaven wrote:
 On Monday, 7 October 2024 at 08:05:59 UTC, ryuukk_ wrote:
 I'm working on it TODAY, therefore i need a today's solution
```d import std.typecons; struct EntityDef { Tuple!(int, "hp") stats; } void main() { EntityDef ed; int x = ed.stats.hp; } ```
Okay, i notice a pattern with Dlang We went from trying to do that: ```D struct EntityDef { struct { int hp; } stats; } ``` to getting suggested to do that instead: ```D struct Foo { int bar; //struct {/* Baz baz; struct Baz { auto opAssign(int value) => baz = value;//*/ int baz; } } void main() { Foo foo; foo.bar = 7; foo.baz = 42; imported!"std.stdio".writeln(foo); /* with opAssign() Anonymous Foo(7, Baz(42)) or Foo(7, 42) */ } ``` to getting suggested to now do that: ```D import std.typecons; struct EntityDef { Tuple!(int, "hp") stats; } void main() { EntityDef ed; int x = ed.stats.hp; } ``` Wich ends up improting a 11k LOC module: https://github.com/dlang/phobos/blob/master/std/typecons.d#L11092 Wich traverse god knows how many templates Only just because i wanteed to do: ```D struct EntityDef { struct { int hp; } stats; } ``` Why wanting to complicate everything? I know i'm not a professional developper, i do this as a hobby, so i must be missing something But something tells me that i'm not missing anything, i can't pin point it just yet!
Oct 07
parent Salih Dincer <salihdb hotmail.com> writes:
On Monday, 7 October 2024 at 17:20:00 UTC, ryuukk_ wrote:
 We went from trying to do that:

 ```d
 struct EntityDef
 {
     struct
     {
         int hp;
     } stats;
 }
 ```


 to getting suggested to do that instead:

 ```d
 struct Foo
 {
     int bar;

     //struct {/*
     Baz baz;
     struct Baz
     {
         auto opAssign(int value)
           => baz = value;//*/
         int baz;
     }
 }

 void main()
 {
     Foo foo;
     foo.bar = 7;
     foo.baz = 42;

     imported!"std.stdio".writeln(foo); /*
     with opAssign()      Anonymous
     Foo(7, Baz(42))  or  Foo(7, 42)    */
 }
 ```
I understand you, but you misunderstood me. You probably brought up a problem that can't be solved quickly (or ever). I wanted to acknowledge D's praise and bring a different perspective. Would this solution work for you? ```d template Foo() { int hp; } struct EntityDef { mixin Foo stats; } void main() { auto num = EntityDef(41); assert(num.stats.hp == 41); with(num) { stats.hp = 42; assert(stats.hp == 42); } } ``` SDB 79
Oct 08
prev sibling next sibling parent reply Sergey <kornburn yandex.ru> writes:
On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:
 all other C like languages allow me to be concise
Which one btw? Except C++
Oct 07
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Monday, 7 October 2024 at 19:34:00 UTC, Sergey wrote:
 On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:
 all other C like languages allow me to be concise
Which one btw? Except C++
Don't lie brother go: ```Go package main import "fmt" type EntityDef struct { stats struct { hp int } } func main() { def := EntityDef{} def.stats.hp = 42 fmt.Println(def) } ``` zig: ```Zig const std = import("std"); const EntityDef = struct { stats: struct { hp: i32, }, }; pub fn main() void { const def = EntityDef { .stats = .{ .hp = 42 } }; std.debug.print("{}\n", .{def}); } ```
Oct 08
parent Sergey <kornburn yandex.ru> writes:
On Tuesday, 8 October 2024 at 07:07:44 UTC, ryuukk_ wrote:
 On Monday, 7 October 2024 at 19:34:00 UTC, Sergey wrote:
 On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:
 all other C like languages allow me to be concise
Which one btw? Except C++
Don't lie brother
Thanks. when I googled "nested unnamed structs" it showed me some rejected DIPs
Oct 08
prev sibling parent reply germandiago <germandiago gmail.com> writes:
On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:
 Why is this allowed
 ```D

 struct EntityDef
 {
     struct
     {
         int hp;
     }
 }
 ```


 But not this fucking thing?

 ```D
 struct EntityDef
 {
     struct
     {
         int hp;
     } stats;
 }
 ```


 Let me name my shit


 No, i don't want to do:


 ```D
 struct EntityDef
 {
     struct Stats
     {
         int hp;
     } Stats stats;
 }
 ```

 Repeating the same name 3 times, i should go back to the stone 
 age too no?

 C and all other C like languages allow me to be concise

 Why is it a D thing to be backward?
The problem is clear. What I do not agree with is your tone. I am noone here, but I think it is not good to tolerate this level. It sets a bad precedent. Just my 2 cents.
Oct 08
next sibling parent Lance Bachmeier <no spam.net> writes:
On Tuesday, 8 October 2024 at 18:24:50 UTC, germandiago wrote:

 What I do not agree with is your tone. I am noone here, but I 
 think it is not good to tolerate this level. It sets a bad 
 precedent.

 Just my 2 cents.
This has come up many times before. The powers that be have been clear that this type of post is pefectly acceptable and anyone that doesn't like it should leave.
Oct 08
prev sibling parent reply Boaz Ampleman <ba 1234.de> writes:
On Tuesday, 8 October 2024 at 18:24:50 UTC, germandiago wrote:
 On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:
 Why is this allowed
 ```D

 struct EntityDef
 {
     struct
     {
         int hp;
     }
 }
 ```


 But not this fucking thing?

 ```D
 struct EntityDef
 {
     struct
     {
         int hp;
     } stats;
 }
 ```


 Let me name my shit


 No, i don't want to do:


 ```D
 struct EntityDef
 {
     struct Stats
     {
         int hp;
     } Stats stats;
 }
 ```

 Repeating the same name 3 times, i should go back to the stone 
 age too no?

 C and all other C like languages allow me to be concise

 Why is it a D thing to be backward?
The problem is clear. What I do not agree with is your tone. I am noone here, but I think it is not good to tolerate this level. It sets a bad precedent. Just my 2 cents.
I'm not a big fan of using declarations as type either: ```d bool doWeWantThat( struct{ int notsure;} p) { return false; } ``` in the same way, named fields in tuples is an aberration to me, because "why not use a struct instead ?"
Oct 08
parent Nick Treleaven <nick geany.org> writes:
On Wednesday, 9 October 2024 at 01:16:29 UTC, Boaz Ampleman wrote:
 I'm not a big fan of using declarations as type either:

 ```d
 bool doWeWantThat( struct{ int notsure;} p)
 {
     return false;
 }
 ```
Here `(int notsure) p` would read much better. And we want tuple syntax in the language anyway so we can do destructuring.
 in the same way, named fields in tuples is an aberration to me, 
 because "why not use a struct instead ?"
When it's a singleton type used in a place you can't declare a struct. Naming tuple fields makes for clearer code rather than having to remember an index in code reviews to decipher what's happening.
Oct 09