1

New to Scala and see people are using sign f ahead of a string, here is an example I tried which works. Wondering what is the function of sign f? Does it need to be combined to use with %s? Tried to search some tutorials but failed. Thanks.

object HelloWorld {

   def main(args: Array[String]) {
      var start = "Monday";
      var end = "Friday";
      var palindrome = "Dot saw I was Tod";
      println(f"date >= $start%s and date <= $end%s" + palindrome);
      // output date >= Monday and date <= FridayDot saw I was Tod
   }
}

2 Answers 2

3

http://docs.scala-lang.org/overviews/core/string-interpolation.html

The f Interpolator

Prepending f to any string literal allows the creation of simple formatted strings, similar to printf in other languages. When using the f interpolator, all variable references should be followed by a printf-style format string, like %d.

PS. another somewhat related feature is http://docs.scala-lang.org/overviews/quasiquotes/expression-details

Sign up to request clarification or add additional context in comments.

15 Comments

That is not a sufficiently related feature. The answer is better without any mention of quasiquotes. s"$a $b" is related.
Isn't interpolation a specific application of "hygienic macros" ? On one hand the quasiquotes is a very complex topic that can confuse novices. On another hand its existence is to be known for those who want to learn new things and explore.
As said by @Rex, no knowledge of macro or quasiquote is need to use/define string interpolation.
that is why I put it in notes rather than answer body and said the feature is "somewhat related". You surely don't have to know how it works just to copy textbook examples, but if you would wonder how it could ever work in strongly typed compiled language then it is a route to explore
@LinMa that is the difference between s-prefix ("splicing") and f-prefix("formatted splicing"). The latter lets you specify not only the value itself, but also the format how to print it. Easy example ( dunno if applies to Scala) TDateTime in Delphi. It is double float internally. But when you print it it converts to year-month-day-dayOfWeek-hours-minutes-seconds-milliseconds. Sometimes that is just a bit too much. So specifying format you may say "show the user year and month and hide all the rest"
|
3

See the explanation here. For people coming from C the f interpolator is a printf style formatter. % is to denote the type of data and with a $ you may may refer to a previously defined variable. The % in not mandatory. Its just that you will get a format that is decided by the compiler at compile time. Bit uyou may want to change the output format sometimes.

So if i take an example ,

var start = "Monday";
  var end = "Friday";
  val age = 33
  var palindrome = "Dot saw I was Tod";
  println(f"date >= $start and date <= $end and age<= $age%f" + palindrome);

I could omit the %f and i will see a output of 33 as it will inferred as Int. However i could use %f if i wanted to format it as a float. Also if you use a incompatible formatted you will receive a error at compile time.

9 Comments

Edited answer with explanation. Please accept if satisified.
BTW, Som, does splicing in Scala providing for re-ordering of variables? I mean when localising applications, phrases in different languages need different order of arguments. When Martin initiated public discussion of splicing few years ago i mentioned it but AFAIR this idea did not appeal to anyone else... Maybe it still was implemented later?.. Of course its compiled nature makes it much more complex than with purely interpreting printf...
i dont know the details :( Can you post a separfate question for that ? Someone definitely out there knows about it i am sure
@LinMA - check "Using scalac print options" section at alvinalexander.com/scala/… and use it over some simple program with a simple f-splicer string
@LinMa you "variable value" - actually variable reference - is "$start" before it and "%s" after that is a formatting standard, it tells Scala that it should print the previous "$start" as string type. And %f would tell to print it as float/double type. See the first link in my answer. First there goes variable or expression after $-sign, then there follows formatting specification after %-sign.
|

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.