Skip to content

cant define nn in usercode with -Yexplicit-nulls #7882

Closed
@bishabosha

Description

@bishabosha

minimized code

compile with -Yexplicit-nulls

def nonnull[T](x: T|Null): x.type & T =
    if (x == null) throw new NullPointerException("tried to cast away nullability, but value is null")
    else x.asInstanceOf[x.type & T]

errors with

-- Error: test.scala:3:24 ------------------------------------------------------
3 |    else x.asInstanceOf[x.type & T]
  |                        ^^^^^^
  |                        (x : T | Null) & T is not stable
1 error found

this makes it hard to define other APIs in user code that eliminate Null, such as Option.apply

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions