2

I am a newbie in scala. I am trying to understand below lines of code:-

val word = "f"
val file = "resources/abc.dat"

val func: (String) => String = word match {
     case "f" => first
     case "s" => second
     case "t" => third
     case _ => default
}

def first(file: String) : String = {
     println("First" + file)
     "first"
}

def second(file: String) : String = {
     println("Second" + file)
     "second"
}

def default(file: String) : String = {
     println("Default" + file)
     "default"
}

What I understand so far is in func, word is matched with cases and particular function is called.But I don't understand how arguments are passed to each function call.

Any pointers will be of great help to me.

Thanks.

1 Answer 1

4

Nope, you are NOT calling function in your pattern matching, you are just RETURNING a function , indeed the type of func is (String) => String, which you can abbreviate in String=>String.

You could then call it like func("ABCDE").

See this for a brief explanation on Scala types

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for clarification.I am able to understand the flow now.

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.