Skip to content

Instantly share code, notes, and snippets.

@ramntry
Created March 31, 2014 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramntry/9892748 to your computer and use it in GitHub Desktop.
Save ramntry/9892748 to your computer and use it in GitHub Desktop.
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))
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