3 ms·
The problem here is you have a complex inheritance hierarchy, right? So don't do that, use composition instead. It's super flexible. E.g. (OCaml): let crea
by yawaramin 7y ago
The problem here is you have a complex inheritance hierarchy, right? So don't do that, use composition instead. It's super flexible. E.g. (OCaml):
let create_frog () = object
method color = "black"
method size = 10
method speed = 5
method direction = 3.14
method swim = print_endline "Frog swimming"
end
let allow_frog_to_walk frog = object
method walk = print_endline "Frog walking"
method super = frog
end
let allow_frog_to_jump frog = object
method jump = print_endline "Frog jumping"
method super = frog
end
let make_frog_toxic frog = object
method intoxicate animal = print_endline ("Intoxicate" ^ animal)
method super = frog
end
let frog = create_frog ()
let frog = allow_frog_to_walk frog
let frog = allow_frog_to_jump frog
let frog = make_frog_toxic frog
- tom_mellior 7y agoThis is cool, but I think the OP wanted to be able to change a given instance's capabilities rather than allocate new wrappers around it. But it wasn't very clear.
- yawaramin 7y agoYeah so changing capabilities at runtime is essentially dynamic typing, and here I'm suggesting a (IMHO safer–you don't have to deal with mutation and its weird effects) statically-typed alternative to that.