-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create Option factory method for T | Null #15891
Comments
I don’t think it’s a good idea since there is no way to ensure that type Threshold = Int | Null
val defaultThreshold: Threshold = null
val maybeThreshold: Option[Threshold] = Option(defaultThreshold)
assert(maybeThreshold.contains(defaultThreshold)) // crash Here, I think having such an operation on |
I don't think that's a valid counter-argument. You can get this today with In order to show an actual issue with this suggestion, you would have to show unsoundness, i.e., a program that doesn't use any |
you can have this one def opt[T](m: T | Null)(using util.NotGiven[Null <:< T]): Option[T] =
Option(m).asInstanceOf[Option[T]] scala> val maybeThreshold = opt(defaultThreshold)
val maybeThreshold: Option[Int] = None Edit: actually this seems a poor encoding because it seems the compiler does not intelligently infer a new union type here if there is more than one part of the union type that is non-null, e.g. it fails for |
would it be too much to expose |
I think the issue here is that since we have The problem is that we share the standard library with Scala 2.13. On the other hand, this is not a binary affecting change, so maybe we can change this transparenty, by adding a stub or something. In any case I think this needs to be solved behind the scenes. Adding more operators for this specific case will cost us in the long run. |
Yes, correct. It seems ugly to still have to deal with the If changing |
A lunch discussion suggested that an explicit |
There's one in |
I would love a solution to this. I really love Scala, but when I used Kotlin using .? felt so good and easy. I do understand that you don't want to introduce a new operator, but is at least the toOption() anytime coming soon? |
Why not a simply Something like this: extension [T](s: T | Null) def getOrElse(t: T): T= if s != null then s else t For Strings this also could be helpful (I use for exception messages for example): extension (s: String | Null) def getOrEmpty: String = if s != null then s else "" |
I don't see that as an issue because you could still do: val maybeThreshold: Option[Threshold] = Some(defaultThreshold) The behavior of def opt[T](value: T | Null): Option[T] =
value match {
case null => None
case v => Some(v.nn)
}
Option(null)
// val r: Option[Null] = None
opt(null)
// val r: Option[Nothing] = None Which is more correct? Currently, the inferred type in I think untagged union types in Scala 3 are in my top 3 favorite additions, and the number 1 use-case is dealing with |
When using
-Yexplicit-nulls
, it's often desirable to create anOption
from an obtained reference that might benull
:For example, the following would avoid any mention of the dreaded
null
value:Except that there is no such factory method, and the Scala compiler complains that it received a
T | Null
value when trying to create theOption
value. Clearly, if thenn
method is used to get the type right, then an exception is thrown if the reference isnull
.This use of
Option
is idiomatic for handling possibly-null
values, so it seems ironic that it isn't available when using explicit nulls.Would it be possible to add a factory method to the
Option
companion, such as the following?Or am I missing something, such as extension method like
nn
that converts the value to an option?The text was updated successfully, but these errors were encountered: