Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected start of definition error in Scala

Tags:

scala

I'm trying to run the Scala code presented in this math.stackexchange post (please see the second answer), but seem to be running into issues in the line starting with implicit def.... The compiler is telling me error: expected start of definition.

Any thoughts? Thanks!

I should add that I'm using http://www.tutorialspoint.com/compile_scala_online.php to run my code.

like image 964
stats134711 Avatar asked Aug 14 '15 16:08

stats134711


2 Answers

Just tried your example on Scala REPL and it works for me as expected.

Move the implicit def to an object:

object MyImplicits {
  /** Pimp `Set[X]` with a few convenient operators */
  implicit def logicalSetOps[X](set: Set[X]) = new {
    def and(other: Set[X]) = set.intersect(other)
    def or(other: Set[X]) = set.union(other)
    def minus(other: Set[X]) = set.filterNot(other.contains)
  }
}

and then do:

import MyImplicits._

That should work for you.

like image 77
Sudheer Aedama Avatar answered Nov 18 '22 16:11

Sudheer Aedama


The code in that example is meant to be pasted either into a worksheet or into the REPL.

It should also work to paste it inside an

object MathApp extends App {
  // paste here
}

Then you can run MathApp as scala or java application.

like image 37
Sascha Kolberg Avatar answered Nov 18 '22 17:11

Sascha Kolberg