Created
November 24, 2015 13:00
-
-
Save akhileshs/96e6083162ec4d47769b to your computer and use it in GitHub Desktop.
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
scala> trait Exp { def eval() : Int } | |
defined trait Exp | |
scala> trait Lit extends Exp { | |
| val x : Int | |
| def eval() = x | |
| } | |
defined trait Lit | |
scala> trait Add extends Exp { | |
| val e1, e2 : Exp | |
| def eval() = e1.eval + e2.eval | |
| } | |
defined trait Add | |
scala> trait Sub extends Exp { | |
| val e1, e2 : Exp | |
| def eval() = e1.eval - e2.eval | |
| } | |
defined trait Sub | |
scala> trait ExpP extends Exp { def print(): String } | |
defined trait ExpP | |
scala> trait LitP extends Lit with ExpP { | |
| def print() = "" + x | |
| } | |
defined trait LitP | |
scala> trait AddP extends Add with ExpP { | |
| val e1, e2 : ExpP //type refined | |
| def print() = "(" + e1.print + "+" + e2.print + ")" | |
| } | |
defined trait AddP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment