Overloading generic functions

Monkey Forums/Monkey Programming/Overloading generic functions

Samah(Posted 2013) [#1]
Mark,
If I do this, it says (as expected) that the Int version already exists.
[monkeycode]Class Foo<T>
Function Bar:Void(hello:Int)
End

Function Bar:Void(hello:T)
End
End

Foo<Int>.Bar(1)[/monkeycode]
What I would like is for trans to detect that the Int version exists and assume that I want to provide a custom implementation for that type. It would then use that version rather than generating one from the generic type.
The use case is that I want to do some funky native code for fast array copying, but I can't extern a generic.

Is this something that's possible? I'd add it to trans myself but you said you're reluctant to accept language-change pull requests.

Thanks,
Samah


AdamRedwoods(Posted 2013) [#2]
are you willing to do a conditional?

Class Foo<T>
  Function Bar:Void(hello:T)
    If Int(hello) <> Null
      BarInt(hello)
      Return
    Endif
    ''Bar
  End
End



Samah(Posted 2013) [#3]
Can't do that unfortunately, because if T is an object type, you can't cast to a primitive and it won't compile.
I've spent AGES trying to hack this in without changing trans and I'm out of options.


AdamRedwoods(Posted 2013) [#4]
Ah, i see the problem.
Perhaps auto-boxing?

EDIT: nix'ed that, didn't work either


AdamRedwoods(Posted 2013) [#5]
maybe? works here. need to test in java.
Class Foo<T>
	
	Method Bar:Void(gee:Int)
		Print "Int!"
	End
	
	Method Bar:Void(gee:Object)
		Print "Not int!"
	End
End



Samah(Posted 2013) [#6]
That doesn't use generics though. It's actually going to be expecting arrays, and technically T[] is not the same as Object[] even if T is non-primitive. Monkey is smart enough to cast it, but I don't want to second guess it like that.

Edit: I might do what you suggested anyway and see how it goes. The generics aren't strictly necessary for that functionality anyway.


Nobuyuki(Posted 2013) [#7]
don't know if it's related to the issue but: but it would be nice if we had a TypeOf operator, to test for specific cases :)