WurstScript icon indicating copy to clipboard operation
WurstScript copied to clipboard

typeId, instanceof, typecasting for generics

Open AlexPutin opened this issue 10 years ago • 1 comments

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

AlexPutin avatar May 04 '15 18:05 AlexPutin

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.

peq avatar May 05 '15 13:05 peq