Can access class parameters and default arguments if you leave them off:
Can access class parameters and specify arguments in any order if you use their names:
When calling methods and functions, you can use the name of the variables explicitly in the call, like so:
When calling methods and functions, you can use the name of the variables explicitly in the call, like so:
def printName(first:String, last:String) = { println(first + " " + last) } printName("John","Smith") // Prints "John Smith" printName(first = "John",last = "Smith") // Prints "John Smith" printName(last = "Smith",first = "John") // Prints "John Smith"
Note that once you are using parameter names in your calls, the order doesn't matter, so long as all parameters are named. This feature works well with default parameter values:
def printName(first:String = "John", last:String = "Smith") = { println(first + " " + last) } printName(last = "Jones") // Prints "John Jones"
Given classes below:
class WithoutClassParameters() { def addColors(red: Int, green: Int, blue: Int) = { (red, green, blue) } def addColorsWithDefaults(red: Int = 0, green: Int = 0, blue: Int = 0) = { (red, green, blue) } } class WithClassParameters(val defaultRed: Int, val defaultGreen: Int, val defaultBlue: Int) { def addColors(red: Int, green: Int, blue: Int) = { (red + defaultRed, green + defaultGreen, blue + defaultBlue) } def addColorsWithDefaults(red: Int = 0, green: Int = 0, blue: Int = 0) = { (red + defaultRed, green + defaultGreen, blue + defaultBlue) } } class WithClassParametersInClassDefinition(val defaultRed: Int = 0, val defaultGreen: Int = 255, val defaultBlue: Int = 100) { def addColors(red: Int, green: Int, blue: Int) = { (red + defaultRed, green + defaultGreen, blue + defaultBlue) } def addColorsWithDefaults(red: Int = 0, green: Int = 0, blue: Int = 0) = { (red + defaultRed, green + defaultGreen, blue + defaultBlue) } }
Can specify arguments in any order if you use their names:
Can default arguments if you leave them off:
Can default class parameters and have default arguments too:
Default parameters can be functional too:
The parameterless execute method has been deprecated and will be removed in a future version of ScalaTest. Please invoke execute with empty parens instead: execute().
The trap method is no longer needed for demos in the REPL, which now abreviates stack traces, and will be removed in a future version of ScalaTest