Im a beginner at a scala and am looking for the best/idiomatic way to do what I intend to do here.
This is what I want to do
def someMethod(obj:MyObj):List[String] = {
List[String]() +:
{if (somecondition is satisfied) .. " element"} +:
{ if (another condition) .. " something else " }
}
That is the method checks some properties of the input parameter object and adds elements to the List (that is to be returned) . If none of the conditions are satisfied , it should return an empty List.
- Of course the code doesn't compile . But somehow, it seems intuitive to me that List[T] + Unit should return List[T] . Why am I wrong ?
And 2. Please tell me the right way to do this Scala . If I were iterating over a list of conditions, I could have used comprehensions.