Skip to content

Type inference when casting with asInstanceOf #11700

@dwijnand

Description

@dwijnand

It would be useful if asInstanceOf inferred from the expected type.

The workaround is:

def cast[A](x: Any): A = x.asInstanceOf[A] // NB can infer Nothing and throw ClassCastException!

Some notes from @adriaanm:

currently it's typed like a selection foo.AIO -- could try to special case that in typedSelect to type check as cast(foo)
right now, the type param for asInstanceOf would not be in the context's undetParams when type checking its target
(from memory -- didn't look at the code)

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)inferout of scope

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions