[Screencast] Elixir: Object Orientation and charming syntax on top of Erlang
blog.plataformatec.com.br
blog.plataformatec.com.br
2) Yes, adding a syntax to easily change instance variable values is one of the most asked features. The tricky part is, we cannot do the following:
@foo = "bar"
Because that is pattern matching and in Erlang/Elixir, we cannot modify objects in place. The expression above needs to return a new object.I was thinking about something like @{'foo : "bar"}, but I haven't decided yet. Suggestions are welcome. :D
I still haven't decided if Elixir will have open class or if I will add refinements or classboxes.
On other hand, module_eval is what allows frameworks like Rails to dynamically add code to a class, so you can do stuff like:
class Post
has_many :comments
end
And it will define a comments method and others, that allows you to easily manipulate the Post-Comment association.This isn't monkey patching. Ruby's classes are open (they can be modified on the fly), so you can monkey patch modules with new functionality. In a language like C, if you wanted to extend a library, you might modify it and recompile.
Ruby's module_eval and class_eval are meta-programming tools that let you programmatically add methods to classes (and lots of other stuff), this can be combined with monkey patching, if you so desire.