У меня сложнее понять, когда я могу или не могу опустить скобки и/или периоды, и как это чередуется с _.Именованные параметры vs _, точечная нотация против операции infix, фигурные и круглые скобки при использовании функций более высокого порядка в Scala
Конкретный случай, который я имел с этим был
val x: X = ???
val xss: List[List[X]] = ???
xss map x :: _ //this doesn't compile
xss map _.::(x) //this is the same as the above (and thus doesn't compile)
выше два кажутся идентичными xss.map(_).::(x)
xss map (x :: _) //this works as expected
xss map {x :: _} //this does the same thing as the above
тем временем, следующий также не:
xss.map xs => x :: xs //';' expected but '=>' found.
xss.map x :: _ //missing arguments for method map in class List; follow this method with `_' if you want to treat it as a partially applied function
//so when I try following the method with _, I get my favourite:
xss.map _ x :: _ //Cannot construct a collection of type That with elements of type B based on a collection of type List[List[Main.X]]
//as opposed to
xss map _ x :: _ //missing parameter type for expanded function ((x$1) => xss.map(x$1).x(($colon$colon: (() => <empty>))))
Right теперь я часто играю «переключать символы, пока они не компилируются», которые, как я считаю, являются субоптимальная стратегия программирования. Как это все работает?
Вы видели http://stackoverflow.com/questions/2173373/scala-foreach-strange-behaviour? – sschaef
Пока нет, спасибо. Ищу. – Martijn
Это должно помочь с проблемой того, где оставить круглые скобки/точки: http://stackoverflow.com/questions/1181533/what-are-the-precise-rules-for-when-you-can-omit-parenthesis- dots-braces-f – sschaef