I'm new to Scala, coming over from Java, and I'm having trouble elegantly returning Map from this function. What's an elegant way to rewrite this function so there's a single exit point?

foo(): Map[TypeDef, DataFrame]
if (someTest) {
    if (someTest2) {
         val a = A()
         val b = B()
         Map(a -> b)
    } else {
} else {

You could write it like:

def foo(): Map[TypeDef, DataFrame] = 
    if (someTest && someTest2) Map(A() -> B()) else Map.empty

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.