0

How can you return a generic function from another generic function? This doesn't work

def gen1[A](a: A) = [B](b: B) => ???

But i don't want to define the second generic in the first function like this

def gen1[A, B](a: A) = (b: B) => ???

Is it possible?

Error here:

illegal start of simple expression
[error]   def gen1[A](a: A) = [B](b: B) => ???
[error]                       ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

What I'm trying to do is the equivalent of dotty's:

def gen1 = [A] => (a: A) => [B] => (b: B) => (a, b)

But can't find documentation for it in scala 2

7
  • 1
    Have you tried? What error do you encounter? Try to specify the return type. Commented Mar 20, 2021 at 10:27
  • 1
    "But i don't want to define the second generic in the first function" why? Sounds a little bit like a xy problem to me. Commented Mar 20, 2021 at 10:31
  • Well what I'm asking for is "How to define a curried function that has a generic in the returned function without having to declare all the generics in the main function". I can find documentation on how to do it in dotty, but not scala 2. Don't really know what u are talking about with "xy problem" Commented Mar 20, 2021 at 10:47
  • 2
    The reason you can find it in dotty, but on in scala 2 is obviously because it exists in dotty, but does not in scala 2 :) Because scala 2 is not dotty. The definitive sign of an xy problem is a statement like "I know I can do X but I don't want to do it, because I want to do Y". You are fixated on what you think is a solution, that does not really solve anything. Having said that, you can do what you "want" in scala 2, I just have no idea why you would want to do it: def foo() = { def bar[B](b: B) = ??? ; bar _ } Commented Mar 20, 2021 at 11:43
  • And why would you need to know why I have to do it? It's like saying that If I ask how to screw a screw I need to explain what I'm building. Makes no sense. Also your solution doesn't give the same result as dotty's one. scastie.scala-lang.org/QdcL9FZkRSKeZk1OXlyrvg This will require Nothing as parameter in the returned function. Commented Mar 20, 2021 at 12:26

1 Answer 1

2

Polymorphic functions don't exist in Scala 2. What you can do to get a similar effect is define a custom class with a polymorphic apply method.

class Gen1PartiallyApplied[A](a: A){
  def apply[B](b: B) = ???
}
def gen1[A](a: A) = new Gen1PartiallyApplied(a)
gen1[Int](42)[String]("foo")
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I was looking for. No need to specify "WHY", either provide the alternative to Polymorphic functions or say that's not possible in scala 2 THat's it, don't know why people were running in circles. Thank you sir
@rockson sorry people wanted to help you instead of just saying "is not possible" (which was actually said).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.