COMMUNITY

Simple way to get rid of redundant fields when downcasting a typedef?

Is there a way to “downcast” from one type to another, and at the same time get rid of redundant fields?

Let’s say that I have the following:

typedef TName = {
	name:String,
}

typedef TNameAge = {
	> TName,
	age:Int,
}

function main() {
	var nameAgeItem:TNameAge = {name: 'Bob', age: 32}
	var nameItem:TName = nameAgeItem;
	
	trace(nameItem); // traces {name:'Bob', age:32}  <-- age field still present
}

As seen above, the age field is present in the nameItem.

Is there a simple way of doing this type conversion so that the redundant age field isn’t carried over?
Macro?

/ Jonas

Edit: I expect that this requires creating a new object, copying over the relevant fields.

1 Like

Yep, macro can do that Try Haxe !

Try tink.Anon.merge from tink_anon

1 Like

Thank you, Rudy and Kevin!