digitalmars.D.learn - Why is this not allowed?
- ryuukk_ (35/35) Oct 04 Why is this allowed
- Richard (Rikki) Andrew Cattermole (4/4) Oct 04 I don't know why it isn't supported.
- ryuukk_ (3/7) Oct 05 Let's PR and let's merge
- Salih Dincer (37/51) Oct 05 In the coding scheme, fields/members/methods may be at the
- ryuukk_ (3/61) Oct 05 I'll pretend you didn't say anything, because your solution is
- ryuukk_ (3/61) Oct 05 I literally don't understand what your code does
- Steven Schveighoffer (4/39) Oct 05 It’s the semicolon. As soon as the closing brace, the declaration
- ryuukk_ (14/67) Oct 05 There is no new syntax to invent
- Steven Schveighoffer (32/44) Oct 07 The problem is not the ability to generate an id, the problem is
- ryuukk_ (11/60) Oct 08 Nonono, you overthink it
- Steven Schveighoffer (14/18) Oct 08 It doesn't work this way. The parser would have to lookahead to
- Nick Treleaven (11/13) Oct 06 Maybe type tuple syntax will support this:
- ryuukk_ (4/17) Oct 07 Let's focus on what exist today, i'm not looking for working on
- Nick Treleaven (13/14) Oct 07 ```d
- ryuukk_ (67/81) Oct 07 Okay, i notice a pattern with Dlang
- Salih Dincer (23/57) Oct 08 I understand you, but you misunderstood me. You probably brought
- Sergey (3/4) Oct 07 Which one btw? Except C++
- ryuukk_ (34/38) Oct 08 Don't lie brother
- Sergey (3/11) Oct 08 Thanks. when I googled "nested unnamed structs" it showed me some
- germandiago (5/40) Oct 08 The problem is clear. What I do not agree with is your tone. I am
- Lance Bachmeier (4/8) Oct 08 This has come up many times before. The powers that be have been
- Boaz Ampleman (10/64) Oct 08 I'm not a big fan of using declarations as type either:
- Nick Treleaven (7/16) Oct 09 Here `(int notsure) p` would read much better. And we want tuple
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
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
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
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:btw not new topic.. https://forum.dlang.org/thread/pblaqxrrjypswtmtnjhd forum.dlang.org?page=1 But I think you will need a DIP for thatI 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
On Saturday, 5 October 2024 at 17:41:13 UTC, Sergey wrote:On Saturday, 5 October 2024 at 10:35:30 UTC, ryuukk_ wrote: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..On Saturday, 5 October 2024 at 06:43:00 UTC, Richard (Rikki) Andrew Cattermole wrote:btw not new topic.. https://forum.dlang.org/thread/pblaqxrrjypswtmtnjhd forum.dlang.org?page=1 But I think you will need a DIP for thatI 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
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
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:I'll pretend you didn't say anything, because your solution is just badNo, 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
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:I literally don't understand what your code does I'm not sure this is related to my suggestionNo, 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
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
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: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 logicWhy 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
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 logicThe 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
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: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 wellThere 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 logicThe 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 08
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 structIt 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
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
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: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 solutionIt’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 07
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
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: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!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
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
On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:all other C like languages allow me to be conciseWhich one btw? Except C++
Oct 07
On Monday, 7 October 2024 at 19:34:00 UTC, Sergey wrote:On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote: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}); } ```all other C like languages allow me to be conciseWhich one btw? Except C++
Oct 08
On Tuesday, 8 October 2024 at 07:07:44 UTC, ryuukk_ wrote:On Monday, 7 October 2024 at 19:34:00 UTC, Sergey wrote:Thanks. when I googled "nested unnamed structs" it showed me some rejected DIPsOn Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote:Don't lie brotherall other C like languages allow me to be conciseWhich one btw? Except C++
Oct 08
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
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
On Tuesday, 8 October 2024 at 18:24:50 UTC, germandiago wrote:On Saturday, 5 October 2024 at 06:35:57 UTC, ryuukk_ wrote: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 ?"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
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