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
import collection._ | |
import collection.generic._ | |
trait Bind[Z[_]] { | |
def bind[A, B](a: Z[A], f: A => Z[B]): Z[B] | |
} | |
def TraversableBind[M[X] <: Traversable[X]] = new Bind[M] { | |
def bind[A, B](r: M[A], f: A => M[B]): M[B] = { | |
implicit object cbf extends CanBuildFrom[Traversable[A], B, M[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 Base { | |
val abstractVal: String | |
final val useAbstractVal = abstractVal | |
} | |
{ | |
class C1 extends Base { | |
val abstractVal = "C1.abstractVal" | |
} | |
assert(new C1().useAbstractVal eq null) |
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
import collection._ | |
import collection.generic._ | |
trait Bind[Z[_]] { | |
def bind[A, B](a: Z[A], f: A => Z[B]): Z[B] | |
} | |
implicit def TraversableBind[M[X] <: Traversable[X]] = new Bind[M] { | |
def bind[A, B](r: M[A], f: A => M[B]): M[B] = { | |
implicit object cbf extends CanBuildFrom[Traversable[A], B, M[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 NamedElementsProduct extends Product { | |
def namedElements: Iterator[(String, Any)] | |
} | |
trait HaskellLikeToString extends NamedElementsProduct { | |
override def toString: String = namedElements.map((p) => p._1 + "=" + p._2).mkString(productPrefix + "(", ",", ")") | |
} | |
case class A(a: Int, b: Int) extends NamedElementsProduct with HaskellLikeToString { | |
def namedElements = List(("a", a), ("b", b)).iterator |
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 T { | |
val t = "T" | |
} | |
class U | |
class V | |
object T { | |
implicit def UToT[UU <% U](u: UU) = new T | |
} |
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 A | |
class A2 extends A | |
class B | |
trait M[X] | |
// | |
// Upper Type Bound | |
// | |
def upperTypeBound[AA <: A](x: AA): A = x |
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 test1 { | |
implicit def s2s(s: String): String = "s2s" | |
assert(implicitly[String => String].apply("foo") == "foo") | |
// Predef.conforms chosen here because it returns the required type (A => A), | |
// so no need to try to eta transform the method s2s. | |
} | |
object test2 { | |
locally { | |
// s2s is a def inside a block. it seems to be automatically eta transformed to (String => 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
type HasTree = { type Tree } | |
def r[HT <: HasTree](x: HT)(y: x.Tree) = null | |
//r: [HT <: HasTree](x$0:HT)(x$0:<param 1.0>#Tree)Null | |
trait T { type Tree } | |
//defined trait T | |
r(new T{ type Tree = Int})(1) | |
/*Exception in thread "main" java.lang.AssertionError: assertion failed: <param 1.0>.type Tree does no longer exist, phase = namer | |
at scala.tools.nsc.symtab.Types$adaptToNewRunMap$.adaptToNewRun(Types.scala:3529) |
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 HtmlBuilder { | |
var html: NodeSeq = NodeSeq.Empty | |
def b(f: (HtmlBuilder) => Unit): Unit = appendNodes(<b>{val b1 = new HtmlBuilder; f(b1); b1.html}</b>) | |
def a(href: String)(f: (HtmlBuilder) => Unit): Unit = appendNodes(<a href={href}>{val b1 = new HtmlBuilder; f(b1); b1.html}</a>) | |
def text(text: String): Unit = appendNodes(Text(text)) | |
def appendNodes(nodes: xml.Node*) = html = html ++ nodes | |
} | |
implicit val builder: HtmlBuilder = new HtmlBuilder | |
import builder._ |
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
import xml.{Text, Node, NodeSeq} | |
class HtmlBuilder { | |
var html: NodeSeq = NodeSeq.Empty | |
def b(f: (HtmlBuilder) => Seq[Node]): Node = <b>{f(this).toSeq}</b> | |
def a(href: String)(f: (HtmlBuilder) => Seq[Node]): Node = <a href={href}>{f(this).toSeq}</a> | |
def text(text: String): Text = Text(text) | |
} | |
val builder: HtmlBuilder = new HtmlBuilder |