Aliasing member's members
TheFlyingFiddle
none at none.com
Mon Feb 26 21:04:51 UTC 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();
More information about the Digitalmars-d-learn
mailing list