Wildcard type parameters or similar

I have a parameterized type

class Attribute< ValueType : AttributeValue >  { ... }

I’d like to define a function

function get( a  : X ) AttributeValue { ... }

whose argument can be an Attribute object of any type, but nothing else. What can I write for X?

In Java you can use wildcards to to this. Expressed in a mix of Haxe and Java, what I want is

     function get( a  : Attribute< ? : AttributeValue> ) AttributeValue { ... }

where Attribute< ? : AttributeValue> is the least super type of { t | exists V. t = Attribute< V > and V : AttributeValue }.

As a fallback, I suppose I could write

     function get( a  : Any ) AttributeValue {
           Assert.assert(   <<a is an instance of Attribute>> )
           ... }

and do the type checking at run-time.

Is there a way I can do run-time type checking? Is there an expression that is true iff and only if a is an instance of Attribute?

Try this:

function get<T:AttributeValue>(a:Attribute<T>):AttributeValue {

or this:

function get(a:Attribute<Dynamic>):AttributeValue {

Thanks. I have a method just like the first one, except its result type is T. This is great for clients that know the exact type of an attribute.

I’ll explain a bit more.

I want to be able to give certain clients a list of attributes of various sorts and then the client can use get to get the value associated with each of those attributes. So a client might look like this

     for(  attr in x.getAttributes() ) {
        var value : AttributeValue  = x.get( attr ) ; 

Some possibilities

  1. getAttributes() returns an Iterator< Attribute< AttributeValue > > and get takes an Attribute< AttributeValue >.

  2. getAttributes() returns an Iterator< Attribute< Any > > and get takes an Attribute< AttributAnyeValue >.

  3. getAttributes() returns an Iterator< Attribute< Dynamic > > and get takes an Attribute< Dynamic >.

  4. getAttributes() returns an Iterator< AttributeI > and get takes an AttributeI where AttributeI is an interface that all instances of Attribute implement and no other class implements AttributeI.

1 would make sense if type parameters were co-variant. But, since they aren’t. And since, whenever Attribute objects are constructed, they are constructed a strict subtype of AttributeValue as the type argument, the set of objects that have type Attribute< AttributeValue > is empty. Does it make sense to have an iterator that claims to produce objects of a type to which no objects will ever belong?

2 has the same problem, I think. Also the compiler won’t allow it, so it’s moot.

3 works, but it gives up on more type checking than I’d like. E.g. the client could take any object returned by the iterator and assign it to any variable of type Attribute<T> regardless of what T is.

I think 4 is ok. But it introduces another named type, which seems overly complex.

In case anyone comes across this thread, I’ll mention that I ended up choosing solution 4.