Skip to content

Unable to find member method #6247

Closed
Closed
@abeln

Description

@abeln

For the explicit nulls project, we want to have an extension method that strips away nullability:

implicit class NN[T](x:T|Null) {
  def nn: T = x.asInstanceOf[T]
}

This works must of the time, but weirdly fails in the code below:

class Foo {
  val x1: String|Null = null
  x1.nn.length
  val x2: String = x1.nn
  x1.nn.length
}

-- [E008] Member Not Found Error: kk.scala:10:8 --------------------------------
10 |  x1.nn.length
   |  ^^^^^^^^^^^^
   |  value length is not a member of T
   |
   |  where:    T is a type variable with constraint >: String
one error found

Line 10 is the second x1.nn.length. However, both

class Foo {
  val x1: String|Null = null
  x1.nn.length
  // val x2: String = x1.nn <---------- commented out
  x1.nn.length
}

and

class Foo {
  val x1: String|Null = null
  x1.nn.length
  val x2 = x1.nn // <---------- the type of `x2` is inferred
  x1.nn.length
}

typecheck fine.

@liufengyun any hints about what could be going here?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions