Scala syntactic sugar

2019-08-12 16:42发布

问题:

Here is my code. I want to write the expression 7? & 10? so that it compiles.

object Test {
  trait A {
    def &(other: A) = ???
  }
  case class B(i: Int) extends A
  implicit class C(i: Int) {
    def ? : A= B(i)
  }
  val u = 7?
  val v = 10?
  val w = u & v // Ok, it compiles
  val z = 7? & 10?   //';' expected but 10 found
  val g = 7.? & 10?  //';' expected but 10 found
  val h = (7?) & 10? //Type mismatch. found Int(10). Required Test.A
  val i = 7? & (10?) //Test.A does not take parameters (on the ?)
}

Why can't I write 7? & 10? Is it possible to write this expression in 6 characters by writing it too differently?

回答1:

Compile with -Xfuture to use dotted notation on your int:

apm@mara:~/tmp$ scala -Xfuture 
Welcome to Scala version 2.10.1 (OpenJDK 64-Bit Server VM, Java 1.7.0_21).
[your stuff here]

scala> 7.? & 10.?
scala.NotImplementedError: an implementation is missing


回答2:

As Alexiv points out, the problem is that you cannot place a binary operator after using postfix syntax for the unary operator. There are multiple problems with postfix syntax, which is why Scala 2.10 will warn you unless you enable a special language feature.

So in theory 7.? & 10.? should work. But now you run into a problem of ambiguity between integer and floating point literals. From the Scala Language Specification §1.3.2 "Floating Point Literals":

If a floating point literal in a program is followed by a token starting with a letter, there must be at least one intervening whitespace character between the two tokens.

So the rules are defined the opposite way here, i.e. Scala seems to assume a floating point number first and integer second. 7.? would be interpreted as a method on a Double, whereas 7.x is a method on Int, and if you want that on Double, you would have to write 7. x.

No how to do the opposite? Place a space before the period:

7 .? & 10 .?

... which of course makes it as long as using parentheses

(7?) & (10?)

So you don't get your 6 characters, but then why do need it that short?



回答3:

Scala compiler interprets 7 ? & this line like a simple higher-order method call: 7.?(&), cause it's a valid scala code, scalac won't be looking for implicit resolution and throw you an syntactic error.