Skip to content

Variadic in Constructor Causes ClassCastException #10774

Open
@maz

Description

@maz

This occurs in Scala 2.12.4 on java 1.8.0_112-b16.

The code below

abstract class TheMoreSuper(val theSeq: Seq[String])

abstract class TheSuper(many: String*) extends TheMoreSuper(many)

class TheSubclass(val theOne: String) extends TheSuper(theOne)

val ts = new TheSubclass("hi")
println(ts.theOne)

throws the following exception:

Caused by: java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot be cast to java.lang.String
	at Playground$TheSubclass.theOne(main.scala:7)
	at Playground.<init>(main.scala:10)
	at Main$.<init>(main.scala:13)
	at Main$.<clinit>(main.scala)
	... 14 more

Here's the crashing example on scastie. and a workaround.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions