Method signature for a function with default values
up vote
3
down vote
favorite
In Scala, is there a way to specify that a function should have default parameter values declared?
For example, in the code below, is there a way to specify in the signature of indirectHelloName
that the provided function must provide a default value for the second parameter?
def helloName(name: String, greating: String = "hello"): Unit = {
println(s"$greating $name")
}
def indirectHelloName(name: String, function: (String,String) => Unit): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
function(name) //This would use the default value for the second argument.
}
}
scala
add a comment |
up vote
3
down vote
favorite
In Scala, is there a way to specify that a function should have default parameter values declared?
For example, in the code below, is there a way to specify in the signature of indirectHelloName
that the provided function must provide a default value for the second parameter?
def helloName(name: String, greating: String = "hello"): Unit = {
println(s"$greating $name")
}
def indirectHelloName(name: String, function: (String,String) => Unit): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
function(name) //This would use the default value for the second argument.
}
}
scala
1
A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
– Jörg W Mittag
Nov 9 at 13:49
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
In Scala, is there a way to specify that a function should have default parameter values declared?
For example, in the code below, is there a way to specify in the signature of indirectHelloName
that the provided function must provide a default value for the second parameter?
def helloName(name: String, greating: String = "hello"): Unit = {
println(s"$greating $name")
}
def indirectHelloName(name: String, function: (String,String) => Unit): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
function(name) //This would use the default value for the second argument.
}
}
scala
In Scala, is there a way to specify that a function should have default parameter values declared?
For example, in the code below, is there a way to specify in the signature of indirectHelloName
that the provided function must provide a default value for the second parameter?
def helloName(name: String, greating: String = "hello"): Unit = {
println(s"$greating $name")
}
def indirectHelloName(name: String, function: (String,String) => Unit): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
function(name) //This would use the default value for the second argument.
}
}
scala
scala
edited Nov 7 at 23:12
jwvh
24.7k52038
24.7k52038
asked Nov 7 at 21:58
Mike Rylander
8,2761563112
8,2761563112
1
A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
– Jörg W Mittag
Nov 9 at 13:49
add a comment |
1
A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
– Jörg W Mittag
Nov 9 at 13:49
1
1
A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
– Jörg W Mittag
Nov 9 at 13:49
A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
– Jörg W Mittag
Nov 9 at 13:49
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
In Scala, is there a way to specify that a function should have default parameter values declared?
For example, in the code below, is there a way to specify in the signature of
indirectHelloName
that the provided function must provide a default value for the second parameter?
A function cannot have an optional parameter with default argument, therefore there is no way to specify one:
val f = (a: Int, b: Int) => a + b
//⇒ f: (Int, Int) => Int = $$Lambda$1073/0x000000080070c840@6cd98a05
val g = (a: Int, b: Int = 5) => a + b
// <console>:1: error: ')' expected but '=' found.
// val g = (a: Int, b: Int = 5) => a + b
// ^
val h = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int) = a + b
}
//⇒ h: (Int, Int) => Int = <function2>
val i = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int = 5) = a + b
}
//⇒ i: (Int, Int) => Int{def apply$default$2: Int @scala.annotation.unchecked.uncheckedVariance} = <function2>
i(3, 5)
//⇒ res: Int = 8
i(3)
// <console>:13: error: not enough arguments for method apply: (v1: Int, v2: Int)Int in trait Function2.
// Unspecified value parameter v2.
// h(3)
// ^
So, a method can have default values, but a function cannot? is that correct?
– Mike Rylander
Nov 16 at 16:32
1
Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
– Jörg W Mittag
Nov 16 at 21:51
add a comment |
up vote
3
down vote
One thing you could do is move the default value from the parameter list to inside the method.
def helloName(in :String*) :Unit =
println(in.lift(1).getOrElse("hello") + ", " + in.head)
def indirectHelloName(name: String, function: (String*) => Unit): Unit =
if (name == "Ted") function(name, "Custom Greeting")
else function(name) //use default
usage:
indirectHelloName("Ted", helloName) //Custom Greeting, Ted
indirectHelloName("Tam", helloName) //hello, Tam
add a comment |
up vote
0
down vote
I tried and come up with the below answer
def helloName(name: String, greeting: String = "hello"): Unit = {
println(s"$greeting $name")
}
val helloNameFn = (x: String, y :String) => println(x + y)
type fn = (String, String) => Unit
def indirectHelloName(name: String, function:fn = helloNameFn): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
helloName(name) //This would use the default value for the second argument.
}
}
helloNameFn("123", "123")
Please try and let me know your comments.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
In Scala, is there a way to specify that a function should have default parameter values declared?
For example, in the code below, is there a way to specify in the signature of
indirectHelloName
that the provided function must provide a default value for the second parameter?
A function cannot have an optional parameter with default argument, therefore there is no way to specify one:
val f = (a: Int, b: Int) => a + b
//⇒ f: (Int, Int) => Int = $$Lambda$1073/0x000000080070c840@6cd98a05
val g = (a: Int, b: Int = 5) => a + b
// <console>:1: error: ')' expected but '=' found.
// val g = (a: Int, b: Int = 5) => a + b
// ^
val h = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int) = a + b
}
//⇒ h: (Int, Int) => Int = <function2>
val i = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int = 5) = a + b
}
//⇒ i: (Int, Int) => Int{def apply$default$2: Int @scala.annotation.unchecked.uncheckedVariance} = <function2>
i(3, 5)
//⇒ res: Int = 8
i(3)
// <console>:13: error: not enough arguments for method apply: (v1: Int, v2: Int)Int in trait Function2.
// Unspecified value parameter v2.
// h(3)
// ^
So, a method can have default values, but a function cannot? is that correct?
– Mike Rylander
Nov 16 at 16:32
1
Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
– Jörg W Mittag
Nov 16 at 21:51
add a comment |
up vote
1
down vote
accepted
In Scala, is there a way to specify that a function should have default parameter values declared?
For example, in the code below, is there a way to specify in the signature of
indirectHelloName
that the provided function must provide a default value for the second parameter?
A function cannot have an optional parameter with default argument, therefore there is no way to specify one:
val f = (a: Int, b: Int) => a + b
//⇒ f: (Int, Int) => Int = $$Lambda$1073/0x000000080070c840@6cd98a05
val g = (a: Int, b: Int = 5) => a + b
// <console>:1: error: ')' expected but '=' found.
// val g = (a: Int, b: Int = 5) => a + b
// ^
val h = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int) = a + b
}
//⇒ h: (Int, Int) => Int = <function2>
val i = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int = 5) = a + b
}
//⇒ i: (Int, Int) => Int{def apply$default$2: Int @scala.annotation.unchecked.uncheckedVariance} = <function2>
i(3, 5)
//⇒ res: Int = 8
i(3)
// <console>:13: error: not enough arguments for method apply: (v1: Int, v2: Int)Int in trait Function2.
// Unspecified value parameter v2.
// h(3)
// ^
So, a method can have default values, but a function cannot? is that correct?
– Mike Rylander
Nov 16 at 16:32
1
Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
– Jörg W Mittag
Nov 16 at 21:51
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
In Scala, is there a way to specify that a function should have default parameter values declared?
For example, in the code below, is there a way to specify in the signature of
indirectHelloName
that the provided function must provide a default value for the second parameter?
A function cannot have an optional parameter with default argument, therefore there is no way to specify one:
val f = (a: Int, b: Int) => a + b
//⇒ f: (Int, Int) => Int = $$Lambda$1073/0x000000080070c840@6cd98a05
val g = (a: Int, b: Int = 5) => a + b
// <console>:1: error: ')' expected but '=' found.
// val g = (a: Int, b: Int = 5) => a + b
// ^
val h = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int) = a + b
}
//⇒ h: (Int, Int) => Int = <function2>
val i = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int = 5) = a + b
}
//⇒ i: (Int, Int) => Int{def apply$default$2: Int @scala.annotation.unchecked.uncheckedVariance} = <function2>
i(3, 5)
//⇒ res: Int = 8
i(3)
// <console>:13: error: not enough arguments for method apply: (v1: Int, v2: Int)Int in trait Function2.
// Unspecified value parameter v2.
// h(3)
// ^
In Scala, is there a way to specify that a function should have default parameter values declared?
For example, in the code below, is there a way to specify in the signature of
indirectHelloName
that the provided function must provide a default value for the second parameter?
A function cannot have an optional parameter with default argument, therefore there is no way to specify one:
val f = (a: Int, b: Int) => a + b
//⇒ f: (Int, Int) => Int = $$Lambda$1073/0x000000080070c840@6cd98a05
val g = (a: Int, b: Int = 5) => a + b
// <console>:1: error: ')' expected but '=' found.
// val g = (a: Int, b: Int = 5) => a + b
// ^
val h = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int) = a + b
}
//⇒ h: (Int, Int) => Int = <function2>
val i = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int = 5) = a + b
}
//⇒ i: (Int, Int) => Int{def apply$default$2: Int @scala.annotation.unchecked.uncheckedVariance} = <function2>
i(3, 5)
//⇒ res: Int = 8
i(3)
// <console>:13: error: not enough arguments for method apply: (v1: Int, v2: Int)Int in trait Function2.
// Unspecified value parameter v2.
// h(3)
// ^
answered Nov 10 at 12:01
Jörg W Mittag
287k62352545
287k62352545
So, a method can have default values, but a function cannot? is that correct?
– Mike Rylander
Nov 16 at 16:32
1
Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
– Jörg W Mittag
Nov 16 at 21:51
add a comment |
So, a method can have default values, but a function cannot? is that correct?
– Mike Rylander
Nov 16 at 16:32
1
Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
– Jörg W Mittag
Nov 16 at 21:51
So, a method can have default values, but a function cannot? is that correct?
– Mike Rylander
Nov 16 at 16:32
So, a method can have default values, but a function cannot? is that correct?
– Mike Rylander
Nov 16 at 16:32
1
1
Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
– Jörg W Mittag
Nov 16 at 21:51
Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
– Jörg W Mittag
Nov 16 at 21:51
add a comment |
up vote
3
down vote
One thing you could do is move the default value from the parameter list to inside the method.
def helloName(in :String*) :Unit =
println(in.lift(1).getOrElse("hello") + ", " + in.head)
def indirectHelloName(name: String, function: (String*) => Unit): Unit =
if (name == "Ted") function(name, "Custom Greeting")
else function(name) //use default
usage:
indirectHelloName("Ted", helloName) //Custom Greeting, Ted
indirectHelloName("Tam", helloName) //hello, Tam
add a comment |
up vote
3
down vote
One thing you could do is move the default value from the parameter list to inside the method.
def helloName(in :String*) :Unit =
println(in.lift(1).getOrElse("hello") + ", " + in.head)
def indirectHelloName(name: String, function: (String*) => Unit): Unit =
if (name == "Ted") function(name, "Custom Greeting")
else function(name) //use default
usage:
indirectHelloName("Ted", helloName) //Custom Greeting, Ted
indirectHelloName("Tam", helloName) //hello, Tam
add a comment |
up vote
3
down vote
up vote
3
down vote
One thing you could do is move the default value from the parameter list to inside the method.
def helloName(in :String*) :Unit =
println(in.lift(1).getOrElse("hello") + ", " + in.head)
def indirectHelloName(name: String, function: (String*) => Unit): Unit =
if (name == "Ted") function(name, "Custom Greeting")
else function(name) //use default
usage:
indirectHelloName("Ted", helloName) //Custom Greeting, Ted
indirectHelloName("Tam", helloName) //hello, Tam
One thing you could do is move the default value from the parameter list to inside the method.
def helloName(in :String*) :Unit =
println(in.lift(1).getOrElse("hello") + ", " + in.head)
def indirectHelloName(name: String, function: (String*) => Unit): Unit =
if (name == "Ted") function(name, "Custom Greeting")
else function(name) //use default
usage:
indirectHelloName("Ted", helloName) //Custom Greeting, Ted
indirectHelloName("Tam", helloName) //hello, Tam
answered Nov 7 at 22:39
jwvh
24.7k52038
24.7k52038
add a comment |
add a comment |
up vote
0
down vote
I tried and come up with the below answer
def helloName(name: String, greeting: String = "hello"): Unit = {
println(s"$greeting $name")
}
val helloNameFn = (x: String, y :String) => println(x + y)
type fn = (String, String) => Unit
def indirectHelloName(name: String, function:fn = helloNameFn): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
helloName(name) //This would use the default value for the second argument.
}
}
helloNameFn("123", "123")
Please try and let me know your comments.
add a comment |
up vote
0
down vote
I tried and come up with the below answer
def helloName(name: String, greeting: String = "hello"): Unit = {
println(s"$greeting $name")
}
val helloNameFn = (x: String, y :String) => println(x + y)
type fn = (String, String) => Unit
def indirectHelloName(name: String, function:fn = helloNameFn): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
helloName(name) //This would use the default value for the second argument.
}
}
helloNameFn("123", "123")
Please try and let me know your comments.
add a comment |
up vote
0
down vote
up vote
0
down vote
I tried and come up with the below answer
def helloName(name: String, greeting: String = "hello"): Unit = {
println(s"$greeting $name")
}
val helloNameFn = (x: String, y :String) => println(x + y)
type fn = (String, String) => Unit
def indirectHelloName(name: String, function:fn = helloNameFn): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
helloName(name) //This would use the default value for the second argument.
}
}
helloNameFn("123", "123")
Please try and let me know your comments.
I tried and come up with the below answer
def helloName(name: String, greeting: String = "hello"): Unit = {
println(s"$greeting $name")
}
val helloNameFn = (x: String, y :String) => println(x + y)
type fn = (String, String) => Unit
def indirectHelloName(name: String, function:fn = helloNameFn): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
helloName(name) //This would use the default value for the second argument.
}
}
helloNameFn("123", "123")
Please try and let me know your comments.
answered Nov 7 at 22:20
Rajkumar Natarajan
1,0161133
1,0161133
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198469%2fmethod-signature-for-a-function-with-default-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
– Jörg W Mittag
Nov 9 at 13:49