4 ms·
On the other hand, from my limited experience in F# (but I guess OCaml would be similar) I found that, when you let the type inference algorithm do its thing, i
by lou1306 8y ago
On the other hand, from my limited experience in F# (but I guess OCaml would be similar) I found that, when you let the type inference algorithm do its thing, it can help you realize that your function is more general than you thought. For instance, you write a function for lists of strings and in the end it can actually work on any list.
Maybe it's trivial, but I found it helped me thinking about refactoring/reusing my code in contexts I didn't expect.
- weberc2 8y agoYou can still annotate the expression with the general type once you know what that type is. :)
- hinkley 8y agoIs it that hard to figure out that your method is generic? And what is this “end” you speak of? You don’t know what next year’s requirements are. Don’t confuse the implementation with the contract. That’s a variant of duck typing. If you need to do stringy things to the data later don’t advertise genericity. People will start using your function and then you’ll have to make a new method because they own the contract now.
- lou1306 8y ago> If you need to do stringy things to the data later don’t advertise genericity. [...] People will start using your function and then you’ll have to make a new method because they own the contract now. I agree that my point doesn't really apply to functions that are part of some kind of API. Still, type inference is not duck typing: the former happens at compile time, unlike the latter. > Is it that hard to figure out that your method is generic? Well, it is not if you decide it to be generic from the start. What I was saying is that sometimes you start with "concrete" implementation, but the type inference helps you thinking about refactoring it to maximize reuse. Something like "hmmm, right now this method has signature 'a -> int -> 'b... I wonder if I can turn it into 'a -> ('b -> 'c) -> 'c and remove this other method that looks pretty similar..."