wurstscript / WurstScript

Programming language and toolkit to create Warcraft III Maps
https://wurstlang.org
Apache License 2.0
224 stars 30 forks source link

typeId, instanceof, typecasting for generics #361

Open AlexYukikaze opened 9 years ago

AlexYukikaze commented 9 years ago

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

peq commented 9 years ago

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.