0

I want the variable value to be processed by string interpolation.

val temp = "1 to 10 by 2"
println(s"$temp")

output expected:

inexact Range 1 to 10 by 2

but getting

1 to 10 by 2

is there any way to get this way done?

EDIT The normal case for using StringContext is:

$> s"${1 to 10 by 2}"
inexact Range 1 to 10 by 2

This return the Range from 1 to 10 with the step value of 2.

And String context won't work on variable, so can there be a way I can do like

$> val temp = "1 to 10 by 2"
$> s"${$temp}"   //hypothetical

such that the interpreter will evaluate this as

s"${$temp}"  => s"${1 to 10 by 2}" => Range from 1 to 10 by step of 2 = {1,3,5,7,9}
3
  • Is there any logic in there? Do you need to detect if the "range is inexact"? If so, please describe that logic (and supply more examples). If not, s"inexact Range $temp". Commented Aug 12, 2020 at 10:33
  • @Thilo: I added the edits, plz have a look. Commented Aug 12, 2020 at 11:40
  • 2
    So your question is very misleading. You want to compute arbitrary Scala code that comes from a file. That is basically the job for the REPL. You need to compile it and then run it. Or maybe not arbitrary code but rather a small grammar of just ranges? Then you need to write a small parser for that grammar. - In any case, this question should be closed since it was not clear and open a new one with all the details of what you really need to do and showing your attempt of solving it. Commented Aug 12, 2020 at 12:45

1 Answer 1

1

By setting a string value to temp you are doing just that - creating a flat String. If you want this to be actual code, then you need to drop the quotes:

val temp = 1 to 10 by 2

Then you can print the results:

println(s"$temp")

This will print the following output string:

inexact Range 1 to 10 by 2

This is the toString(...) output of a variable representing a Range. If you want to print the actual results of the 1 to 10 by 2 computation, you need to do something like this:

val resultsAsString = temp.mkString(",")
println(resultsAsString)
> 1,3,5,7,9

or even this (watch out: here the curly brackets { } are used not for string interpolation but simply as normal string characters):

println(s"{$resultsAsString}")
> {1,3,5,7,9}

Edit If what you want is to actually interpret/compile Scala code on the fly (not recommended though - for security reasons, among others), then you may be interested in this: https://ammonite.io/ - Ammonite, Scala scripting

In any case, to interpret your code from a String, you may try using this: https://docs.scala-lang.org/overviews/repl/embedding.html

See these lines:

val scripter = new ScriptEngineManager().getEngineByName("scala")
scripter.eval("""println("hello, world")""")
Sign up to request clarification or add additional context in comments.

1 Comment

Michal Januszewski , no, the value for val temp will be given from text file. What i want is val temp = "???" and it should process it based on whatever value it can be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.