[fpc-pascal] Better usage of "with"

Ryan Joseph ryan at thealchemistguild.com
Thu Jun 14 09:32:51 CEST 2018


I was just watching a video from Jonathan Blow (the developer of a really popular game named Braid) on a new language he’s working on to get around all the cruft and slowness in c++ for making games. One the problems he identifies is poor memory layout using object oriented inheritance and arrays of structs.

His proposal was to make it easier to build linked lists in structs that can access tightly packed arrays of continuous memory for fields that are iterated often and susceptible to cache misses. The solution he came up with is really brilliant in my opinion and even though he used the keyword “using” it’s basically like “with” in Pascal.

I never use “with” normally but this is an extremely interesting idea and I wonder if it would be compatible with how “with” works in FPC now. Is any of this functionality possible using “with"? Curious what anyone thinks about this, providing my explanation makes any sense.

type
	TEntity = record
		position: TVec2;
	end;
	TEntityPtr = ^TEntity;

type
	TDoor = record
		with entity: TEntityPtr; // import entity namespace for entire TDoor scope
		state: boolean;
	end;

var
	door: TDoor;
begin
	door.entity := GetAvailableEntity; 	// pop an entity from storage
	door.position := V2(1, 1); 		// entity is “with” so we get access to its members (door.entity^.position)
	
// works with function parameters also.
// this is almost like a class helper or at very least mimics “self” in methods.

procedure OpenDoor(with var door: TDoor);
begin
	state := true; // with imports door namespace into entire function scope
end;


Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list