This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sup | |
class Sub(val element: Int) extends Sup | |
def test1[B >: Sub](a: Sub, b: B): B = if (a.element < 0) b else a | |
// || | |
// || difference | |
// || | |
// \/ | |
def test2[B >: Sub](a: Sub)(b: B): B = if (a.element < 0) b else a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Fn[A] { | |
def apply[B >: A](t: B): B | |
} | |
class Sup | |
class Sub extends Sup | |
def test[S >: Sub](a: Sub): Fn[S] = | |
new Fn[S] { | |
override def apply[B >: S](b: B): B = if (true) a else b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Fn[A,B] { | |
// AA is contravariant; BB is covariant | |
// Similar to Function1[-AA, +BB] | |
def apply[AA >: A, BB <: B](t: AA): BB | |
} | |
class Sup | |
class Sub extends Sup | |
def test[S >: Sub](a: Sub): Fn[S,S] = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Example { | |
trait HttpParser[A] { | |
// For the sake of simplicity, parse doesn't take any params. | |
// Returning an Option is to reflect the fact that parse may | |
// fail. | |
def parse: Option[A] | |
} | |
trait HttpRenderer[B] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Example { | |
trait HttpParser[A] { | |
def parse: Option[A] | |
} | |
trait HttpRenderer[B] { | |
def render(b: B): String | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dependency comes first when currying: OO-style constructors. | |
function foo(dependency) { | |
return function (a) { | |
return dependency(a); | |
} | |
} | |
// Dependency comes last when currying: Reader Monad. | |
function foo(a) { | |
return function (dependency) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace acme; | |
function array_key_exists($key, $a) { | |
if ($a instanceof \ArrayAccess) { | |
return $a->offsetExists($key); | |
} else { | |
return \array_key_exists($key, $a); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace acme; | |
function array_key_exists($key, $a) { | |
if ($a instanceof \ArrayAccess) { | |
return $a->offsetExists($key); | |
} else { | |
return \array_key_exists($key, $a); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait A { | |
val count: Int | |
val elements: Array[String] = new Array(count) | |
} | |
class C extends A { | |
override val count = 10 | |
} | |
val c = new C |
OlderNewer