3 ms·
The issue to me is one of name spacing. Where is the XML#to_s defined? Certainly not in one very large to_s function that takes in large swaths of various types
by 3pt14159 11y ago
The issue to me is one of name spacing. Where is the XML#to_s defined? Certainly not in one very large to_s function that takes in large swaths of various types. Of course it should be namespaced around XML, the only issue is whether it's module based or object based.
I think object based is better. Why? Because I can take the object and introspect the methods. Any number of functions can take in an XML object, but there are very few methods that the XML object needs to have. In my repl if I do:
my_xml.methods - Object.new.methods
I get a list of what I'm expected to do with this object. If I do something along the lines of:
functions.collect { |f| f.first_argument_can_be? XML }
Then I'm pulling in not just the normal functions, but also anything that consumes _any_ instance of the XML instance, including domain specific ones.
Now, you could argue that all method-like functions should be namespaced to their own module, but now humans have to be trusted to not define these functions outside of that module and they have to be trusted to not define anything else in the module. For what purpose?
- Retra 11y agoIt's not a binary choice. You could easily have everything be module namespaced and then then allow module functions to be selectively promoted to method invocation. But really, that's no different from requiring all functions to be methods and then selectively 'demoting' some to be static or class functions instead.
- true_religion 11y agoElixir is a functional language, and has the option of modules and protocols so you can define XML#to_string in two ways. Either as a protocol so you could do to_string(this_xml) Or as a function within the module so you could do XML.to_string(this_xml) If you use the module function defintion you can import it into your scope via import XML then you can write to_string(this_xml) as if it were a protocol.