2

My function must take a list of values as argument, convert each one to string and add them to a list. The values can be either String, Int or Boolean.

How to define a function with the flexibility to handle the different types? Here is my initial function but as expected when i give int as argument, i get an type mismatch error

def test (s: String*): List[String] = {
  s.toList
}
1

1 Answer 1

4
def test(s: Any*): List[String] = s.map(_.toString).toList
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Exactly what i wanted. i've been able to trim and uppercase each string as needed.
If it's solved, can you mark the answer as solving please ? :-)
@C4stor No, I can't. ;) (Not sure whether the first commenter is notified in such cases)
Yes he is notified :-) I should have commented the question though, sorry !

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.