0

I write a scala script string_split.sc:

val s = "  some  interesting stuff ...   "

// this does not work
val x = s.toLowerCase()
    .replaceAll("[^\\w']", " ")
    .split(" ")
    .filter(w => w.trim.length >=1)
    .groupBy(identity)
    .mapValues(_.size)

when load it in sbt console with :load:

scala> :load src/main/scala/string_split.sc

src/main/scala/string_split.sc:1: error: illegal start of definition
.replaceAll("[^\\w']", " ")
^
src/main/scala/string_split.sc:1: error: illegal start of definition
.split(" ")
^
src/main/scala/string_split.sc:1: error: illegal start of definition
.filter(w => w.trim.length >=1)
^
src/main/scala/string_split.sc:1: error: illegal start of definition
.groupBy(identity)
^
src/main/scala/string_split.sc:1: error: illegal start of definition
.mapValues(_.size)

sbt console can not parsing the line start with '.'

The code is fine to load in scala REPL directly. compile it also fine. ':paste' it to sbt console also fine.

Only the sbt console has problem to load this. Is it a bug in sbt console? Is there work around?

my build.sbt is simple:

name := "Simple SBT"

version := "1.0"

scalaVersion := "2.11.12"
Ken Cloud
  • 853
  • 7
  • 6
  • Have you seen this? https://stackoverflow.com/questions/13898373/how-to-load-a-scala-file-in-the-sbt-console Wrap everything in an `object` and then load it. Which version of `SBT` you are using? – Nader Ghanbari Feb 02 '18 at 03:05
  • 1
    Possible duplicate of [How to load a scala file in the sbt console?](https://stackoverflow.com/questions/13898373/how-to-load-a-scala-file-in-the-sbt-console) – Nader Ghanbari Feb 02 '18 at 03:11
  • The question is not about how to load scala file. Only sbt console show syntax error when :load the script file. It is fine if compile it . BTW, I write this code snipet as scala script with .sc and file extention – Ken Cloud Feb 02 '18 at 05:32
  • Your question is a subset of that question, or maybe just slightly different. If you read the answer to https://stackoverflow.com/questions/13898373/how-to-load-a-scala-file-in-the-sbt-console carefully you'll find the answer to your question as well. – Nader Ghanbari Feb 03 '18 at 13:32
  • @NaderHadjiGhanbari, Thank you for your patient to explain to me. It do works! Thanks. – Ken Cloud Feb 07 '18 at 03:40

1 Answers1

1

Scala (SBT) Console parses statements line by line. Therefore, when it parses the line val x = s.toLowerCase(), it detects it as a complete statement and assigns the value of s.toLowerCase to val x.

Therefore, when it proceeds to the next line, the statement starts with a dot, making it an illegal statement. This happens for all the statements that start with a dot. To rectify this, change your statement to end with a dot symbol instead as follows:

val x = s.toLowerCase.
          replaceAll("[^\\w']", " ").
          split(" ").
          filter(w => w.trim.length >=1).
          groupBy(identity).
          mapValues(_.size)

This will work the way you expect it to.

Patrick W
  • 1,485
  • 4
  • 19
  • 27
  • Scala REPL, or scala console also works with the '.' at the beginning of line. Only SBT console does not work. Just wonder what special about SBT console. – Ken Cloud Feb 02 '18 at 05:18
  • and, it works in SBT console if use ':paste' as well. Only ':load' in SBT console does not work. – Ken Cloud Feb 02 '18 at 05:22