digitalmars.D.learn - Aliasing member's members
- Kayomn (37/37) Feb 26 2018 I've been experimenting with D's Better C mode, and I have a
- TheFlyingFiddle (10/47) Feb 26 2018 Don't think you can alias member variables directly.
I've been experimenting with D's Better C mode, and I have a question regarding something that I started thinking about after watching one of Jonathon Blow's talks on data-oriented programming - more specifically the aspect of fake "inheritance" I have the following code. My question is if it's possible to use alias in a similar way to Jonathon's own language Jai and its using keyword, referencing the internal Vector2 as Player.pos instead of Player.entity.position: import core.stdc.stdio : printf; uint idCounter = 0; struct Vector2 { double x,y; } struct Entity { uint id; Vector2 position; } struct Player { Entity entity; alias pos = Entity.position; } Player createPlayer(Vector2 position) { Player player; player.entity.id = idCounter++; player.entity.position = position; return player; } int main(string[] args) { Player player = createPlayer(Vector2(50.0,50.0)); printf( "[Player]\nid: %d\nPosition: %lf x %lf\n", player.entity.id, player.pos.x, player.pos.y ); return 0; }
Feb 26 2018
On Monday, 26 February 2018 at 20:50:35 UTC, Kayomn wrote:I've been experimenting with D's Better C mode, and I have a question regarding something that I started thinking about after watching one of Jonathon Blow's talks on data-oriented programming - more specifically the aspect of fake "inheritance" I have the following code. My question is if it's possible to use alias in a similar way to Jonathon's own language Jai and its using keyword, referencing the internal Vector2 as Player.pos instead of Player.entity.position: import core.stdc.stdio : printf; uint idCounter = 0; struct Vector2 { double x,y; } struct Entity { uint id; Vector2 position; } struct Player { Entity entity; alias pos = Entity.position; } Player createPlayer(Vector2 position) { Player player; player.entity.id = idCounter++; player.entity.position = position; return player; } int main(string[] args) { Player player = createPlayer(Vector2(50.0,50.0)); printf( "[Player]\nid: %d\nPosition: %lf x %lf\n", player.entity.id, player.pos.x, player.pos.y ); return 0; }Don't think you can alias member variables directly. You could do this though: struct Player { Entity entity; ref auto pos() inout { return entity.position; } } Which will give you most of what you want. Although if you want to take the address of pos you have to use auto addr = &player.pos();
Feb 26 2018
On Monday, 26 February 2018 at 21:04:51 UTC, TheFlyingFiddle wrote:On Monday, 26 February 2018 at 20:50:35 UTC, Kayomn wrote:Damn, was hoping to keep my structs as plain old data-structures. Thanks for the info, guess I won't be doing this then.[...]Don't think you can alias member variables directly. You could do this though: struct Player { Entity entity; ref auto pos() inout { return entity.position; } } Which will give you most of what you want. Although if you want to take the address of pos you have to use auto addr = &player.pos();
Feb 26 2018
Kayomn wrote:On Monday, 26 February 2018 at 21:04:51 UTC, TheFlyingFiddle wrote:write `pos` as free function then, and use UFCS. there is no real difference. ;-)On Monday, 26 February 2018 at 20:50:35 UTC, Kayomn wrote:Damn, was hoping to keep my structs as plain old data-structures. Thanks for the info, guess I won't be doing this then.[...]Don't think you can alias member variables directly. You could do this though: struct Player { Entity entity; ref auto pos() inout { return entity.position; } } Which will give you most of what you want. Although if you want to take the address of pos you have to use auto addr = &player.pos();
Feb 26 2018