WurstScript
WurstScript copied to clipboard
typeId, instanceof, typecasting for generics
It is possible to add typecasting feature to generic types?
class Entity
class Unit extends Entity
class Engine
LinkedList<Entity> objects = new LinkedList<Entity>()
function get<T>() returns T
for entity in objects
if entity instanceof T // or object.typeId == T.typeId
return entity castTo T
return null
function add(Entity entity)
objects.add(entity)
I'm want implement something like that but now it's impossible
Currently that is not possible, because all information about generics is removed after type-checking. The necessary information is not available at runtime.
The same restrictions apply to languages such as Java. Changing the implementation of generics is possible, but would take some time. So I am not sure, if I can implement this any time soon.