Giving type parameters in functions

I want to implement something like this (took it from here) using a function. This method does not do anything (as in does no computation and returns nothing). A call to this method can either compile or not compile and there lies it’s usefulness

def conformance[A, B <: B] = ()

I am trying to do something like this using a function. I don’t see a way of doing this with an anonymous function.

But can I do it using the Function0 class?

new Function0[A, B <: A ,Unit] {
      def apply() = ()

I know this does not compile and is not valid. But just thinking if there is a way to make this work.

What are you trying to accomplish by using Function0 for this? Do you need to pass the result somewhere? You can’t use <: in the initialization and you only have a single type parameter (the return type) in Function0, so you’d have to create a subtype to do this:

class Conformance[A,B <:A] extends Function0[Unit] {
      def apply() = ()
new Function0Limited[AnyRef, String] //compiles
new Function0Limited[AnyRef, Int] //doesn't compile

If you explain, why you need an anonymous function, there may be a better answer.

Thanks for the reply! Actually I am not using this anywhere. I just wanted to see if method def conformance[A, B <: A] could be written as a function :smile:

You cannot write it as a function. That would be a polymorphic function value, which does not yet exist in Scala.

1 Like