Extending Function with an implicit parameter

69 views
Skip to first unread message

Joe Barnes

unread,
May 14, 2013, 5:02:45 PM5/14/13
to scala...@googlegroups.com
I'm attempting to extend the Function class with an implicit parameter, but I've not managed to appease the compiler.  This is the code that I want to work:

class MyFun extends ((Int, Int) => Int) {
  def apply(a:Int)(implicit b:Int):Int = a + b
}

And it doesn't compile:

[error] MyFun.scala:21: class MyFun needs to be abstract, since method apply in trait Function2 of type (v1: Int, v2: Int)Int is not defined
[error] class MyFun extends ((Int, Int) => Int) {
[error]       ^

I've tried a few variants without success:

class MyFun extends ((Int, implicit Int) => Int) {
  def apply(a:Int)(implicit b:Int):Int = a + b
}

[error] MyFun.scala:21: identifier expected but 'implicit' found.
[error] class MyFun extends ((Int, implicit Int) => Int) {
[error]                            ^
[error] MyFun.scala:40: ')' expected but eof found.

class MyFun extends ((Int)(implicit Int) => Int) {
  def apply(a:Int)(implicit b:Int):Int = a + b
}

[error] MyFun.scala:21: ')' expected but '(' found.
[error] class MyFun extends ((Int)(implicit Int) => Int) {
[error]                           ^
[error] MyFun.scala:21: ';' expected but '=>' found.
[error] class MyFun extends ((Int)(implicit Int) => Int) {
[error]                                          ^


Does anyone know how to do this?

Thanks,
Joe

Brian Maso

unread,
May 14, 2013, 5:42:08 PM5/14/13
to Joe Barnes, scala-user (ggroups)
I don't believe functions can have either type parameters or implicit parameters.

Brian Maso


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Best regards,
Brian Maso
(949) 395-8551
Follow me: @bmaso
br...@blumenfeld-maso.com

yves

unread,
May 14, 2013, 5:55:00 PM5/14/13
to scala...@googlegroups.com
I think you can't,because basically you're trying to change the base signature of the apply method.
implicit is just syntaxic sugar that lets you not bother with someparameters ; but the parameteris still there nevertheless.

Yves

Joe Barnes

unread,
May 14, 2013, 9:38:58 PM5/14/13
to scala...@googlegroups.com, Joe Barnes
Functions can certainly have those... I suppose you mean objects/classes which extend a FunctionN class?

Joe

Joe Barnes

unread,
May 14, 2013, 9:39:39 PM5/14/13
to scala...@googlegroups.com
Right... but that's what makes it seem to me that it should be possible.

Joe

yves

unread,
May 15, 2013, 1:09:11 AM5/15/13
to scala...@googlegroups.com
I'm doubtfull.

def apply(x:Int)(y:Int) is not equivalent to def apply(x:Int,y:Int)even though they share a lot in common.

For once,I think for example that the signature used to resolve overload is Int for the first,Int,Int forthe second.

Now,I see nothing that would forbid you from writting:

class F extends ((x:X,y:Y)=>Z) = {
  def apply(x:X,y:Y) = ...
  def apply(x:X)(implicit y:Y) = apply(x,y)
}

It's not exactly what you want,but might be close enough.

Yves

Rex Kerr

unread,
May 15, 2013, 10:40:24 AM5/15/13
to yves, scala-user
Better to test than surmise.  With Ints:

<console>:9: error: double definition:
method apply$mcIII$sp:(v1: Int, v2: Int)Int and
method apply$mcIII$sp:(v1: Int, v2: Int)Int at line 8

So, no, you can't have two JVM-identical methods, even if Scala thinks they are different.

  --Rex


--

Sonnenschein

unread,
May 15, 2013, 2:40:34 PM5/15/13
to scala...@googlegroups.com
Hi Joe,

maybe something like the following would suffice for you:

class MyFun(implicit c:Int = 0) extends ((Int, Int) => Int) {
  def apply(a:Int, b:Int): Int = a + b
  def apply(a:Int): Int = a + c
}

implicit val b = 2
val f = new MyFun

println(f(2,2)) // 4
println(f(3))   // 5


Peter
Reply all
Reply to author
Forward
0 new messages