Created
March 31, 2014 13:54
-
-
Save ramntry/9892748 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
let ident_parser stream = | |
let ident = stream#get_ident () in | |
print_endline ("ident_parser got ident `" ^ ident ^ "'"); | |
ident | |
let const_parser stream = | |
let const = stream#get_const () in | |
print_endline ("const_parser got const `" ^ const ^ "'"); | |
const | |
let def_parser stream = | |
let ident = ident_parser stream in | |
stream#get_equal_sign (); | |
let const = const_parser stream in | |
print_endline ("def_parser got definition `" ^ ident ^ " = " ^ const ^ "'"); | |
(ident, const) | |
let () = | |
ignore ( | |
def_parser (object | |
method get_ident () = "great_answer" | |
method get_const () = "42" | |
method get_equal_sign () = () | |
end)) |
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 IdentParser { | |
def parse[S <: { def getIdent(): String }](stream: S) = { | |
val ident = stream.getIdent() | |
println("IdentParser got ident `" + ident + "'") | |
ident | |
} | |
} | |
object ConstParser { | |
def parse[S <: { def getConst(): String }](stream: S) = { | |
val const = stream.getConst() | |
println("ConstParser got const `" + const + "'") | |
const | |
} | |
} | |
object DefParser { | |
def parse[S <: { | |
def getIdent(): String | |
def getConst(): String | |
def getEqualSign(): Unit }](stream: S) = { | |
val ident = IdentParser.parse(stream) | |
stream.getEqualSign() | |
val const = ConstParser.parse(stream) | |
println("DefParser got definition `" + ident + " = " + const + "'") | |
(ident, const) | |
} | |
} | |
object StructuralSubtyping { | |
def main(args: Array[String]) { | |
DefParser.parse(new { | |
def getIdent() = "greatAnswer" | |
def getConst() = "42" | |
def getEqualSign() = () | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment