question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create Option factory method for T | Null

See original GitHub issue

When using -Yexplicit-nulls, it’s often desirable to create an Option from an obtained reference that might be null:

For example, the following would avoid any mention of the dreaded null value:

Option(someJavaMethodPossiblyReturningNull()) match

    // Function returned null
    case None => // etc.

    // Value was non-null.
    case Some(x) => // etc.

Except that there is no such factory method, and the Scala compiler complains that it received a T | Null value when trying to create the Option value. Clearly, if the nn method is used to get the type right, then an exception is thrown if the reference is null.

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?

object Option:

    def apply[T](value: T | Null): Option[T] = // ...

Or am I missing something, such as extension method like nn that converts the value to an option?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
sjrdcommented, Aug 26, 2022

A lunch discussion suggested that an explicit toOption extension method on T | Null is a likely good candidate.

0reactions
carlos-verdescommented, Oct 12, 2022

Why not a simply getOrElse extension method?

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 ""
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it ok for a factory to return null? | Kim Lindhard
Is it ok for a factory to return null? Super short answer. No, it contradicts the intent of the factory patterns. The long...
Read more >
Is it OK for a factory method to return null? - Stack Overflow
I think it's potentially reasonable for a factory method to return null in some situations, but not if it's a method called CreateCommand...
Read more >
Factory Method - Refactoring.Guru
The Factory Method pattern suggests that you replace direct object construction calls (using the new operator) with calls to a special factory method....
Read more >
Java Constructors vs Static Factory Methods - Baeldung
Learn about static factory methods in Java and why they're sometimes preferred over constructors for instantiating and initializing objects.
Read more >
Is null a valid value for a dependency (from a factory method)?
No, it's not. null means no value and is ignored by Windsor. Be explicit - if the value is optional provide overloaded constructor...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found