0

I was trying to run a simple program in scala involving implicit class in scala. The expected output of the program is "CZF" i.e. incrementing each character by 1. But, when i am executing it on Eclipse IDE, its not returning any result nor the error.

object ObjectImplitclass extends App{
  implicit class StringIncImplicitClass(s: String){
    def increment = s.map(c => (c+1).toChar)
    val result = "BYE".increment
    print(result)
}
}

When i tried the following chunk of code on terminal:

implicit class StringIncImplicitClass(s: String){
def increment = s.map(c => (c+1).toChar)
val result = "BYE".increment

It returned me "CZF". I am new to the scala syntax, can anyone help me as to why i am not able to see the result on IDE.

Sri2110
  • 335
  • 2
  • 19

1 Answers1

3

In REPL you evaluated "BYE".increment right after you defined an implicit class, so result is shown immediately.

In IDE you written:

object ObjectImplitclass extends App{
  implicit class StringIncImplicitClass(s: String){
    def increment = s.map(c => (c+1).toChar)
    val result = "BYE".increment
    print(result)
  }
}

meaning you defined implicit class... but you never used in on anything. If this code is exactly what you have in IDE, it should have been

object ObjectImplitclass extends App {
  implicit class StringIncImplicitClass(s: String) {
    def increment = s.map(c => (c+1).toChar)
  }
  val result = "BYE".increment
  print(result)
}

See the differences in brackets position.

Mateusz Kubuszok
  • 24,995
  • 4
  • 42
  • 64