3 ms·
Is lack of ad-hoc polymorphism a real issue for such projects? Everyone who used JS before are accustomed to using overridden methods, or just duck typing, whic
by ungzd 8y ago
Is lack of ad-hoc polymorphism a real issue for such projects? Everyone who used JS before are accustomed to using overridden methods, or just duck typing, which are form of ad hoc polymorphism too, but Reason/Ocaml lacks it. How does this feel? Or it's easy to code avoiding ad-hoc polymorphism after forming habit for it? Do you use things like function dictionaries to simulate ad-hoc polymorphism?
- thangngoc89 8y agoActually I like the lacks of it. I know exactly what an operator does without digging deep into the source code. Everything is explicit
- a0 8y agoIn principle it seems like a significant limitation but in practice it is rarely an issue. ReasonML does support parametric polymorphism so it is still possible to write generic code. Some language features conveniently help to avoid boilerplate. For example it’s possible to have a `Float` module with arithmetic operators and “open” it like this: `Float.(10.0 + 0.5 / 3.0)`. There’s also a work in progress project called Modular Implicits that will introduce ad-hoc polymorphism.
- ohadrau 8y agoEven then, I feel like working with Floats vs. Ints in a web browser is almost never an issue. I'd just open Float if I need it because I can't think of any scenario where I'd use both in one module
- int_19h 8y agoI'm not familiar with Reason, but OCaml does have virtual methods and dynamic dispatch, and therefore ad-hoc polymorphism. http://dev.realworldocaml.org/classes.html#virtual-classes-and-methods http://dev.realworldocaml.org/classes.html#virtual-classes-a... Now, you can't downcast or cross-cast or do a runtime typecheck easily, but that doesn't mean it's impossible - you just have to use roundabout ways like variants to do so (which also means that you can control which class hierarchies it is available for, and which ones it's not). A class hierarchy combined with an polymorphic variant type to allow runtime type queries can be made as powerful as a runtime typecheck/cast in Java - if more verbose - for those rare cases where you actually need it. http://dev.realworldocaml.org/variants.html#polymorphic-variants http://dev.realworldocaml.org/variants.html#polymorphic-vari... Of course, in practice, most cases where you'd use a downcast or a typecheck in a language like Java, map more naturally to regular variants and matching on them in OCaml.