Skip to content

Instantly share code, notes, and snippets.

@Dierk
Created March 18, 2015 09:02
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 Dierk/bd72b46fd69ad4cdd959 to your computer and use it in GitHub Desktop.
Save Dierk/bd72b46fd69ad4cdd959 to your computer and use it in GitHub Desktop.
Protype-base "subclassing" with immutable records
module FunctionRecord where
data FRecord = FRecord {
parent :: FRecord,
name :: String,
toString :: (FRecord -> String)
}
prototype = FRecord { parent = prototype, name = undefined, toString = (\r -> r.name) }
dierk = prototype.{ name = "Dierk" }
phil = prototype.{ name = "Phil", toString = (\r -> "Wadler") }
main _ = do
println $ prototype.toString dierk -- Dierk
println $ prototype.toString phil -- Phil (prototype toString)
println $ phil.toString phil -- Wadler (overridden toString)
println $ phil.parent.toString phil -- Phil (parent toString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment