Function type inheritance in Scala

44 views
Skip to first unread message

Saanvi S

unread,
Nov 23, 2017, 2:30:31 AM11/23/17
to scala-functional

Hi Scala folk,


"Function types are classes that can be inherited from subclasses."

I know that I can create new classes, and I know that functions are
objects and hence have a type.


I don't yet know how/if scale distinguishes between a type and a
class, but for now I'm not assuming that there is a distinction.


A function type might be something like "(Int,Int)String" (possibly
I'm confused on this point, perhaps its type is Function or something
like that).


The statement conjures up an image of me declaring a function type the
way I would declare a class:
class (X,Y)Z

and then subclassing that function type somehow (not sure what there
would be to inherit, since the entire implementation of the function
would be overridden).


Thanks in Advance

Saanvi S

Vlad Patryshev

unread,
Nov 23, 2017, 2:51:14 AM11/23/17
to scala-fu...@googlegroups.com
Actually, the types of functions are Function1...Function26 (as far as I remember). If you subtype a function, you subtype one of these.

Thanks,
-Vlad

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

Alexey Romanov

unread,
Nov 25, 2017, 2:59:03 PM11/25/17
to scala-fu...@googlegroups.com
In Scala, you can write the type e.g. `(Int, Int) => String`, but it's just a short way to write `Function2[Int, Int, String]` (it's actually a trait, not a class). And yes, you can write

class Foo extends (Int, Int) => String {
  override def apply(x1: Int, x2: Int): String = ...
}



Yours, Alexey Romanov
Reply all
Reply to author
Forward
0 new messages