Thread overview
Aliasing member's members
Feb 26, 2018
Kayomn
Feb 26, 2018
TheFlyingFiddle
Feb 26, 2018
Kayomn
Feb 26, 2018
ketmar
February 26, 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;
}
February 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();



February 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:
>> [...]
>
> 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();

Damn, was hoping to keep my structs as plain old data-structures. Thanks for the info, guess I won't be doing this then.
February 26, 2018
Kayomn wrote:

> On Monday, 26 February 2018 at 21:04:51 UTC, TheFlyingFiddle wrote:
>> On Monday, 26 February 2018 at 20:50:35 UTC, Kayomn wrote:
>>> [...]
>>
>> 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();
>
> Damn, was hoping to keep my structs as plain old data-structures. Thanks for the info, guess I won't be doing this then.

write `pos` as free function then, and use UFCS. there is no real difference. ;-)