1

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?

Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101

3 Answers3

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?

Community
  • 1
  • 1
0__
  • 66,707
  • 21
  • 171
  • 266
  • As a side note, Scala defines `'$'` and `'_'` as upper case letters. You would be able to write `7.$ & 10.$` or even `7.$&10.$`. But who will understand the meaning of such an expression? – 0__ Jul 08 '13 at 14:07
  • Note that you can drop the period from before the second ?, and that with the addition of an implicit method (along with your existing implicit class) - eg. `implicit def toA(i: Int): A = B(i)` you can drop the parentheses around the second argument (ie. make your `val h = (7?) & 10?` line above compile) – Shadowlands Jul 08 '13 at 14:09
  • @0__ you should never use `$` and `_` in production code, cause this symbols are used by the compiler and can cause very hard to find bugs – 4lex1v Jul 08 '13 at 14:11
  • @Shadowlands Well you can also define `implicit class IntOps(i: Int) { def ?&(j: Int) = Incomplete(B(i), j) }; case class Incomplete(a: A, b: Int) { def ? : A = a & B(b) }` and write `7?&10?`. Six characters. Who buys me a beer? – 0__ Jul 08 '13 at 14:11
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
som-snytt
  • 39,429
  • 2
  • 47
  • 129
0

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.

4lex1v
  • 21,367
  • 6
  • 52
  • 86
  • It will still not compile (Scala 2.10.2), because it tries to make up a `Double` literal. – 0__ Jul 08 '13 at 13:35
  • @0__ Yeah, checked it, you r right, but still the problem is in method call interpretation – 4lex1v Jul 08 '13 at 13:39