Created
March 18, 2015 09:02
-
-
Save Dierk/bd72b46fd69ad4cdd959 to your computer and use it in GitHub Desktop.
Protype-base "subclassing" with immutable records
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
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