Explicit Nulls: Option-like extensions for working with nullable T | Null

My bad, very nice that it works !

With that in mind, here is an alternate proposal that addresses one of the original issues:

def generate(model: String, prompt: String?) =
  // model has type String here
  // prompt has type Option[String] here
  ???

generate("mymodel") // inside: prompt = None
generate("mymodel", prompt = "Nullable type are") // inside: prompt = Some("Nullable type are")

// And maybe

val optionalParam = Option.when(usePrompt)("Only present when usePrompt is true")
generate("mymodel", prompt = ?optionalParam)

There might be issues with the above syntax, this is just a quick sketch

In particular, the above avoids the issue of (A | Null) | Null:

def foo[T](optional: T?) =
  // optional will have type Option[T] here
  // => T can be Option[A] or A | Null without issue
  ???

foo(Some(1)) // T =:= Option[Int], in body: optional: Option[Option[T]]
foo(?Some(1)) // T =:= Int, in body: optional: Option[T]

This might even be possible through SIP-XX - Unpack Case Classes into Parameter Lists and Argument Lists
Has it has similar ideas, but I’m not yet familiar with the particulars of the proposal

2 Likes