Skip to content

Generic function creates anonymous class that uses function's type parameter, compiles but fails at runtime #2144

Closed
@scabug

Description

@scabug

The method called approach3 in the attached example compiles but fails with NoSuchMethodException at runtime.

The problem concerns this construct:

def convert[A](a: A) = new { def frob(a2: A) = ... }
convert("foo").frob("bar")

Scala throws a NoSuchMethodException naming frob(String). Scala is trying to locate the frob method using the actual argument type, which is String. But the parameter type of frob is the erasure of A, which is Object.

Scala knows that frob is effectively a generic method, because it does correctly check the type of its argument at compile time. This line of code will not compile:

convert("foo").frob(1)

But it seems like the fact that it is a generic method gets lost somewhere within the call-by-reflection code.

I'm reporting this as a bug because approach1 and approach2, which are logically equivalent, work, probably because they don't require Scala to use reflection internally. If approach3 doesn't work by design, then I think that the Scala compiler should flag it as an error.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions