I want a class that has a function (which is contracted by a trait) available statically. Then I want a type parameterized method that takes such a type as its type parameter and accesses the contracted static function. (Yes I know the term static is Java not Scala but you know what I mean.)
The only way I know to have a static function in Scala is by having a companion object extend the trait. However doing that has two problems:
- I don't know how to constrain the type parameter such that it's companion object extends some trait.
- I don't know how I would access the static method being that the class is actually of a different type than its companion object.
This may be completely off but kinda what I want to do is:
MyTrait {
def MyFunction() : Any //some function
}
case class MyClass(i: Int)
object MyClass extends MyTrait {
def MyFunction() = {/*do stuff*/}
}
//need the type as a class not an object because I need it for a higher order function like this
def SomeFunctionHigherOrderFunction[T /*constrain T such that the companion object of T <: MyTrait*/](someFunc : Function1[T, Any]) : Unit {
val someStuff = T.MyFunction()
/*use someStuff in here*/
}
SomeFunctionHigherOrderFunction[T](/*provide a Function1[T, Any]*/);
Any ideas of the correct solution or some better ways of going about this problem?
Thanks in advance!