What is the different between Option[String]
and Option[String] = None
? I think Option type is default to None
if not set, but not 100% sure. Wondering if there will be any potential impact.
For example:
case class User(name:Option[String])
Will it be different from
case class User(name:Option[String]=None)
I think you confuse two different concepts: Default arguments and Default initial values .
Default arguments
In Scala you can provide default arguments for parameters of methods and constructors. And it allows you to skip those arguments:
case class User(name:Option[String]=Some("Sam")) {
def say(greeting: String = "Hello"): String = greeting + " " + name
}
val u = new User
println(u.say())
As you can see, it has nothing to do with Option
and None
. It works for any type (see the method) and yes, you have to specify that default yourself, otherwise it will not compile:
case class User(name:Option[String])
val u = new User
// Doesn't compile.
And no, None
is not the default value for Option[A]
when we talk about default arguments. You can provide None
as well as any other value of type Option[A]
as a parameter of type Option[A]
, but you have to do it explicitly:
case class User(name:Option[String]=Some("Sam"))
case class User(name:Option[String]=None)
So, talking about parameters to methods and constructors , there's no default value for values of type Option[A]
. You must specify that default yourself, otherwise it will not compile.
So, these two lines from your question are indeed different:
case class User(name:Option[String])
case class User(name:Option[String]=None)
Default initial values
Consider this code:
var name: Option[String] = _
println(name)
null
is printed. Because the default value is null
, not None
. And this is the case not only for Option[A]
, but for all AnyRef
types in Scala. The following prints null
either:
var name: String = _
println(name)
Please, refer also to Scala Language Specification. Eg https://www.scala-lang.org/files/archive/spec/2.11/04-basic-declarations-and-definitions.html#variable-declarations-and-definitions
Your assumption is incorrect. There is no standard default for parameters; usually, parameters that are declared must also be provided explicitly when the method or constructor is called.
But a parameter can be declared as optional, and supplied with a default value at its declaration point: When you declare a parameter with its name and type, and then you add the equals sign followed by a static value, then this parameter becomes optional. If left out, the given static value will be inserted automatically.
This does not only work with case classes, but also with other kinds of constructors and methods.
In the first example that you have given, a statement like val user = User()
would not compile, because the name
parameter must be given.
In the second example, val user = User()
would very well compile. At runtime, user.name
would have the value of None
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.