Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8798533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:07:11+00:00 2026-06-14T00:07:11+00:00

String interpolation is available in Scala starting Scala 2.10 This is the basic example

  • 0

String interpolation is available in Scala starting Scala 2.10

This is the basic example

 val name = "World"            //> name  : String = World
 val message = s"Hello $name"  //> message  : String = Hello World

I was wondering if there is a way to do dynamic interpolation, e.g. the following (doesn’t compile, just for illustration purposes)

 val name = "World"            //> name  : String = World
 val template = "Hello $name"  //> template  : String = Hello $name
 //just for illustration:
 val message = s(template)     //> doesn't compile (not found: value s)
  1. Is there a way to “dynamically” evaluate a String like that? (or is it inherently wrong / not possible)

  2. And what is s exactly? it’s not a method def (apparently it is a method on StringContext), and not an object (if it was, it would have thrown a different compile error than not found I think)


  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:07:12+00:00Added an answer on June 14, 2026 at 12:07 am

    s is actually a method on StringContext (or something which can be implicitly converted from StringContext). When you write

    whatever"Here is text $identifier and more text"
    

    the compiler desugars it into

    StringContext("Here is text ", " and more text").whatever(identifier)
    

    By default, StringContext gives you s, f, and raw* methods.

    As you can see, the compiler itself picks out the name and gives it to the method. Since this happens at compile time, you can’t sensibly do it dynamically–the compiler doesn’t have information about variable names at runtime.

    You can use vars, however, so you can swap in values that you want. And the default s method just calls toString (as you’d expect) so you can play games like

    class PrintCounter {
      var i = 0
      override def toString = { val ans = i.toString; i += 1; ans }
    }
    
    val pc = new PrintCounter
    def pr[A](a: A) { println(s"$pc: $a") }
    scala> List("salmon","herring").foreach(pr)
    1: salmon
    2: herring
    

    (0 was already called by the REPL in this example).

    That’s about the best you can do.

    *raw is broken and isn’t slated to be fixed until 2.10.1; only text before a variable is actually raw (no escape processing). So hold off on using that one until 2.10.1 is out, or look at the source code and define your own. By default, there is no escape processing, so defining your own is pretty easy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a short syntax for string interpolation in Scala? Something like: my name
I wanted to ask if there is any type of string interpolation in Scala.
In Ruby you can use string interpolation like so: text = This is visit
I was wondering if there is any way of preserving indentation while doing string
Is there a way to 'prepare' a string for later interpolation (without using custom
Is there any single-line string literal syntax in Ruby that allows string interpolation but
string name = bob; object obj = name; obj = joe; Console.WriteLine(name); I'm a
I am attempting to perform String Interpolation in C#. The input string I am
My original python program used string interpolation to take a number a user enters
There are a lot of similar questions to this one but i think none

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.