3 ms·
> declare every type everywhere What does that mean? The types are declared exactly once. If you mean C# doesn’t do type inference, well yeah. But, I still
by louthy 9y ago
> declare every type everywhere
What does that mean? The types are declared exactly once.
If you mean C# doesn’t do type inference, well yeah.
But, I still don’t see how this changes my original point, which was merely to demonstrate how to do return type polymorphism in a language other than the sacred Haskell. As the original post gives the implication that it’s not achievable when not working in Haskell.
- moomin 9y agoAh, I see the confusion. Shouldn’t have used “declare” in that context. My point was that you needed to specify the type every time you used it. So the more operations you add the more your code looks like generic soup. I think you’re confused as to what return type polymorphism is, though. It’s the ability to have the compiler infer the type of something from its site of use. So your example doesn’t exhibit it because the types need to be specified. So, the following code works in Haskell x = [1,2,read “3”] y = [“1”,”2”, read ”3”] In the first case read returns an Int, in the second a String. This is a useless toy example but it turns out to be really useful and to make ergonomic a huge number of things that are just painful in the languages we normally use. Everything’s achievable in every language, but what’s convenient changes massively.
- louthy 9y ago> I think you’re confused as to what return type polymorphism is, though. It’s the ability to have the compiler infer the type of something from its site of use. I think you may be confused about what return type polymorphism is tbh. Type inference != polymorphism. Polymorphism is a type-system feature. Type inference is a separate process to ascertain concrete types at compile time. For example, the only type inference that C# really does is `var`: var x = foo(); That is no different to me writing: int x = foo(); (assuming that foo returns an int that is). The compiled version of both of those code snippets are the same. The fact that I specified the `int` directly doesn't make those chunks of code any different. Or, make one less valid. You're right it's absolutely the case that to do this I'd need to specify the types I want to work with, and that's because C# is shit at inferring anything. It doesn't change the fact that the return type is polymorphic for Concat in my example. It's _ad-hoc polymorphic_, and no lack of type-inference will change that. But this still misses the entire point of what I wrote. Which was to counter the point made in your original post: > Once you've got return type polymorphism, you really start to miss it in other languages. Initially I felt I was helping by pointing out that if you're missing it in other (mainstream) languages, then there's absolutely a way of doing it. > The simplest example possible is "mempty" mempty :: a > gets the "default" value of a. Which makes no sense in a language where you need an instance to have polymorphism. And secondly I felt it important to use your example of `mempty` to show how to do it.