Skip to content

Commit d0c4817

Browse files
committed
Patch self type of Function1 and PartialFunction
1 parent af897ca commit d0c4817

File tree

3 files changed

+494
-0
lines changed

3 files changed

+494
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
// GENERATED CODE: DO NOT EDIT. See scala.Function0 for timestamp.
14+
15+
package scala
16+
17+
18+
object Function1 {
19+
20+
implicit final class UnliftOps[A, B] private[Function1](private val f: A => Option[B]) extends AnyVal {
21+
/** Converts an optional function to a partial function.
22+
*
23+
* @example Unlike [[Function.unlift]], this [[UnliftOps.unlift]] method can be used in extractors.
24+
* {{{
25+
* val of: Int => Option[String] = { i =>
26+
* if (i == 2) {
27+
* Some("matched by an optional function")
28+
* } else {
29+
* None
30+
* }
31+
* }
32+
*
33+
* util.Random.nextInt(4) match {
34+
* case of.unlift(m) => // Convert an optional function to a pattern
35+
* println(m)
36+
* case _ =>
37+
* println("Not matched")
38+
* }
39+
* }}}
40+
*/
41+
def unlift: PartialFunction[A, B] = Function.unlift(f)
42+
}
43+
44+
}
45+
46+
/** A function of 1 parameter.
47+
*
48+
* In the following example, the definition of `succ` is
49+
* shorthand, conceptually, for the anonymous class definition
50+
* `anonfun1`, although the implementation details of how the
51+
* function value is constructed may differ:
52+
*
53+
* {{{
54+
* object Main extends App {
55+
* val succ = (x: Int) => x + 1
56+
* val anonfun1 = new Function1[Int, Int] {
57+
* def apply(x: Int): Int = x + 1
58+
* }
59+
* assert(succ(0) == anonfun1(0))
60+
* }
61+
* }}}
62+
*
63+
* Note that the difference between `Function1` and [[scala.PartialFunction]]
64+
* is that the latter can specify inputs which it will not handle.
65+
*/
66+
@annotation.implicitNotFound(msg = "No implicit view available from ${T1} => ${R}.")
67+
trait Function1[@specialized(Specializable.Arg) -T1, @specialized(Specializable.Return) +R] extends AnyRef { // FIXME: self =>
68+
/** Apply the body of this function to the argument.
69+
* @return the result of function application.
70+
*/
71+
def apply(v1: T1): R
72+
73+
/** Composes two instances of Function1 in a new Function1, with this function applied last.
74+
*
75+
* @tparam A the type to which function `g` can be applied
76+
* @param g a function A => T1
77+
* @return a new function `f` such that `f(x) == apply(g(x))`
78+
*/
79+
@annotation.unspecialized def compose[A](g: A => T1): A => R = { x => apply(g(x)) }
80+
81+
/** Composes two instances of Function1 in a new Function1, with this function applied first.
82+
*
83+
* @tparam A the result type of function `g`
84+
* @param g a function R => A
85+
* @return a new function `f` such that `f(x) == g(apply(x))`
86+
*/
87+
@annotation.unspecialized def andThen[A](g: R => A): T1 => A = { x => g(apply(x)) }
88+
89+
override def toString(): String = "<function1>"
90+
}

0 commit comments

Comments
 (0)