Aliasing member's members

Kayomn spam at kayomn.net
Mon Feb 26 20:50:35 UTC 2018


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;
}


More information about the Digitalmars-d-learn mailing list