Is there a way to New a generic?
Monkey Forums/Monkey Programming/Is there a way to New a generic?
| ||
Hi guys, I keep getting an error in a generic class I have when it tries to new an object of it's generic type.Class Eagle End Class Jim<T> Method New() the = New T() End Field the:T End Function Main() Local g:Jim<Eagle> = New Jim<Eagle> End Is there a way around this? |
| ||
Pass the instance in, I suppose: |
| ||
Sorry, that example I gave was just an example of the problem. I can't actually pass the instance in in my real program. Essentially my class holds a collection of T. When I call a particular method with an identifier for T, if an object doesn't exist with that identifier it makes one. Something like Method Test(objectId:Int) Local o:T = GetObject(objectId) if o = Null o = new T() InsertIntoCollection(o) endif Do something with T. Of course I can put the test outside in the client code, but I was hoping for my class to handle all that for me. Cheers |
| ||
I guess I'd create a TFactory that implements a known interface and pass that in then. Edit: |
| ||
Hi, No, you can't go 'new T' (or new T[] etc) because as far as the generic code is concerned, 'T' is just 'Object'. |