Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

syntax - What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?

What are the precise rules for when you can omit (omit) parentheses, dots, braces, = (functions), etc.?

For example,

(service.findAllPresentations.get.first.votes.size) must be equalTo(2).
  • service is my object
  • def findAllPresentations: Option[List[Presentation]]
  • votes returns List[Vote]
  • must and be are both functions of specs

Why can't I go:

(service findAllPresentations get first votes size) must be equalTo(2)

?

The compiler error is:

"RestServicesSpecTest.this.service.findAllPresentations of type Option[List[com.sharca.Presentation]] does not take parameters"

Why does it think I'm trying to pass in a parameter? Why must I use dots for every method call?

Why must (service.findAllPresentations get first votes size) be equalTo(2) result in:

"not found: value first"

Yet, the "must be equalTo 2" of (service.findAllPresentations.get.first.votes.size) must be equalTo 2, that is, method chaining works fine? - object chain chain chain param.

I've looked through the Scala book and website and can't really find a comprehensive explanation.

Is it in fact, as Rob H explains in Stack Overflow question Which characters can I omit in Scala?, that the only valid use-case for omitting the '.' is for "operand operator operand" style operations, and not for method chaining?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You seem to have stumbled upon the answer. Anyway, I'll try to make it clear.

You can omit dot when using the prefix, infix and postfix notations -- the so called operator notation. While using the operator notation, and only then, you can omit the parenthesis if there is less than two parameters passed to the method.

Now, the operator notation is a notation for method-call, which means it can't be used in the absence of the object which is being called.

I'll briefly detail the notations.

Prefix:

Only ~, !, + and - can be used in prefix notation. This is the notation you are using when you write !flag or val liability = -debt.

Infix:

That's the notation where the method appears between an object and it's parameters. The arithmetic operators all fit here.

Postfix (also suffix):

That notation is used when the method follows an object and receives no parameters. For example, you can write list tail, and that's postfix notation.

You can chain infix notation calls without problem, as long as no method is curried. For example, I like to use the following style:

(list
 filter (...)
 map (...)
 mkString ", "
)

That's the same thing as:

list filter (...) map (...) mkString ", "

Now, why am I using parenthesis here, if filter and map take a single parameter? It's because I'm passing anonymous functions to them. I can't mix anonymous functions definitions with infix style because I need a boundary for the end of my anonymous function. Also, the parameter definition of the anonymous function might be interpreted as the last parameter to the infix method.

You can use infix with multiple parameters:

string substring (start, end) map (_ toInt) mkString ("<", ", ", ">")

Curried functions are hard to use with infix notation. The folding functions are a clear example of that:

(0 /: list) ((cnt, string) => cnt + string.size)
(list foldLeft 0) ((cnt, string) => cnt + string.size)

You need to use parenthesis outside the infix call. I'm not sure the exact rules at play here.

Now, let's talk about postfix. Postfix can be hard to use, because it can never be used anywhere except the end of an expression. For example, you can't do the following:

 list tail map (...)

Because tail does not appear at the end of the expression. You can't do this either:

 list tail length

You could use infix notation by using parenthesis to mark end of expressions:

 (list tail) map (...)
 (list tail) length

Note that postfix notation is discouraged because it may be unsafe.

I hope this has cleared all the doubts. If not, just drop a comment and I'll see what I can do to improve it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...