Skip to content

Instantly share code, notes, and snippets.

@deanwampler
Last active March 6, 2021 21:45
Show Gist options
  • Save deanwampler/b6940fcce289983d0c5563718b65be93 to your computer and use it in GitHub Desktop.
Save deanwampler/b6940fcce289983d0c5563718b65be93 to your computer and use it in GitHub Desktop.
// Adapted from https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/script/scala/progscala3/typesystem/poly/PolymorphicFunctions.scala
scala> val toMapF1g = [K,V] => (key: K) => (value: V) => Map(key -> value) // good
| val toMapF2g: [K,V] => K => V => Map[K,V] =
| [K,V] => (key: K) => (value: V) => Map(key -> value) // good
val toMapF1g: PolyFunction{apply: [K, V](key: K): V => Map[K, V]} = <function1>
val toMapF2g: PolyFunction{apply: [K, V](x$1: K): V => Map[K, V]} = <function1>
scala> toMapF1g("one")(1.1)
| toMapF2g("one")(1.1)
| toMapF1g(2L)("two")
| toMapF2g(2L)("two")
val res0: Map[String, Double] = Map(one -> 1.1)
val res1: Map[String, Double] = Map(one -> 1.1)
val res2: Map[Long, String] = Map(2 -> two)
val res3: Map[Long, String] = Map(2 -> two)
scala> val toMapF1b = [K] => [V] => (key: K) => (value: V) => Map(key -> value) // bad
| val toMapF2b: [K] => [V] => K => V => Map[K,V] =
| [K] => [V] => (key: K) => (value: V) => Map(key -> value) // bad
|
1 |val toMapF1b = [K] => [V] => (key: K) => (value: V) => Map(key -> value) // bad
| ^
|Implementation restriction: polymorphic function literals must have a value parameter
2 |val toMapF2b: [K] => [V] => K => V => Map[K,V] =
| ^
|Implementation restriction: polymorphic function types must have a value parameter
3 | [K] => [V] => (key: K) => (value: V) => Map(key -> value) // bad
| ^
|Implementation restriction: polymorphic function literals must have a value parameter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment