In Scala, what does “extends (A => B)” on a case class mean?

前端 未结 5 1511
梦如初夏
梦如初夏 2021-01-02 12:52

In researching how to do Memoization in Scala, I\'ve found some code I didn\'t grok. I\'ve tried to look this particular \"thing\" up, but don\'t know by what to call it; i.

5条回答
  •  遥遥无期
    2021-01-02 13:19

    A => B is short for Function1[A, B], so your Memo extends a function from A to B, most prominently defined through method apply(x: A): B which must be defined.

    Because of the "infix" notation, you need to put parentheses around the type, i.e. (A => B). You could also write

    case class Memo[A, B](f: A => B) extends Function1[A, B] ...
    

    or

    case class Memo[A, B](f: Function1[A, B]) extends Function1[A, B] ...
    

提交回复
热议问题