Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSimon Pike
1,415 PointsWhat is an unused l-value?
The question asks me to : Create a variable named result and assign it the tuple returned from function greeting. (Note: pass the string "Tom" to the greeting function.)
I have copied my code into Xcode, and it seems to run OK, but does not compile in this test browser environment.
When I go to the Preview pane, it tells me that my expression resolves to an unused l-value.
What's that? and how can I resolve the issue?
func greeting(person: String) -> (greeting: String, language: String) {
let language = ("English")
let greeting = ("Hello \(person)", "speak \(language)")
return greeting
}
var result = greeting("Tom")
1 Answer
Richard Lu
20,185 PointsHi Simon,
You have a very interesting solution (assigning it to a constant before returning it). The reason you may be getting the error is because the challenge expects a tuple value returned (with the 2nd value of the tuple with just the language). Here's the problem in your snippet of code
let greeting = ("Hello \(person)", "speak \(language)") // this returns a tuple of the values ("Hello Tom", "speak English")
To fix this problem, remove the string "speak ". So you have:
let greeting = ("Hello \(person)", "\(language)")
Here's a more common way of solving this problem (reason why your solution is so unique):
func greeting(person: String) -> (greeting: String, language: String) {
let language = "English"
let greeting = "Hello \(person)" // this is just the greeting string, instead of a tuple
return (greeting, language) // the return value is creating a tuple
}
var result = greeting("Tom")
Happy coding! :)
Simon Pike
1,415 PointsSimon Pike
1,415 PointsThank you very much Richard! i have spent most of the afternoon trying variations on this!