Created
August 17, 2010 13:44
-
-
Save sadache/529983 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I really do not like using the syntax that removes points | |
scala> List("one","two") foldLeft ("") (_+_) | |
<console>:6: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2)) | |
List("one","two") foldLeft ("") (_+_) | |
^ | |
// Int ??? | |
scala> List("one","two") foldLeft ("") ((_+_):(String,String) => String) | |
<console>:6: error: type mismatch; | |
found : (String, String) => String | |
required: Int | |
List("one","two") foldLeft ("") ((_+_):(String,String) => String) | |
^ | |
scala> List("one","two").foldLeft ("") (_+_) | |
res2: java.lang.String = onetwo |
I love ML currying, but this is not it and often it gets unpredictable to me. I guess there is a problem of precedence before the type inferencer kicks in. It is very confusing since it is hard to grasp even forgetting about this very case. I don't seem to be getting the mental model and that's why I tent to use it very rarely and only in places where it is clear and obvious.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've been trying to get my head around the type inferer's behaviour around currying too. I got this from a Scheme-friend:
which I could translate to
but I felt a bit ashamed for all the dots so as the ML fan boy I am I tried to remove them:
That error makes me want to add the parameter type to l and x in the anonymous function:
That obviously didn't work. It seems like the same error you got. In my example I at least mention Int so I didn't think more about it. But in your code Int is never even mention so that makes me think Int is just some kind of hacky last resort somewhere in the type inferer?
Anyway, I tried around a whole lot and finally I noticed that a parenthesis around the first function call did the trick just like in Mirkos example:
or maybe this one is the nicest:
Here I also typed Nil. The whole thing feels weird but I guess that the type inferer is doing really complex stuff. No simple Hindley–Milner here ;)
Mirko, did the underscores get lost somewhere? I need to do it like this:
I actually really LOVE point-free currying in ML but I guess the Scala one is somewhat limited and obviously makes the type inferer behave a bit weird sometimes.