Search code examples
scalafunctiondefault

Set up default value of the the parameter of the element from the tuple argument


def found( names(String, String) ): Unit {
  // Do something
}

The parameter list of the function found is a string tuple (string, string)

How to set up the default value of the second tuple element as the first one like names(String, String=names._1) Or does this even allowed? if so how can I passing the argument? found( ("FirstName", ???) ) or found("FirstName")


Solution

  • If you have a tuple (String, String) then you have both strings. If you have not both strings then this is not a tuple. The whole parameter can have default value, part of a tuple can't. If default value uses other parameter then parameters should be in different parameter lists.

    Try

    def found(name1: String)(name2: String = name1): Unit = ???
    
    found("FirstName")()
    found("FirstName")("LastName")
    
    def foundTuple(names: (String, String)): Unit = found(names._1)(names._2)
    
    foundTuple(("FirstName", "LastName"))
    foundTuple("FirstName", "LastName")