5

I have a user object with a few properties that I can access using dot notation.

For example, user.fullName outputs a String like Firstname Lastname.

How do I access these properties within a println statement that uses string interpolation?

I've tried the following:

println(s"user's full name is $user.fullName")

However, it doesn't seem to work with dot notation and only parses the entire $user object, interpreting the remaining fullName section as a string rather than a property. This incorrectly outputs:

>> user's full name is User(...).fullName

The following is what I'm after:

>> user's full name is Firstname Lastname

Help appreciated!

Machavity
  • 30,841
  • 27
  • 92
  • 100
dbau
  • 16,009
  • 2
  • 21
  • 31
  • Just a suggestion ;): maybe you should rephrase the question because the way string interpolation works and accessing members with dot notation is not println specific. – yǝsʞǝla Oct 26 '13 at 15:56

1 Answers1

13

Solved - looks like curly braces help interpret the entire variable, including properties that are accessed through dot notation.

The following code works:

println(s"user's full name is ${user.fullName}")

This outputs the following as expected:

>> user's full name is Firstname Lastname

dbau
  • 16,009
  • 2
  • 21
  • 31