-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Structurally typed Selectable compile error: Sequence argument type annotation *
cannot be used here
#20044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Same error when using dynamic. See this code. package structSelect
class RecordX(elems: (String, Any)*) extends Selectable:
private val fields = elems.toList
val name: String = "A"
val age: Int = 1
def selectDynamic(name: String): Any = fields.find(_._1 == name).get._2
def applyDynamic(name: String)(args: Any*): Any = ???
/*transparent inline*/ def makeRecX() =
// Ok
// RecordX("name" -> "Hemma", "age" -> 6).asInstanceOf[ RecordX ]
// Fails
RecordX("name" -> "Hemma", "age" -> 6).asInstanceOf[
Selectable {
val name: String;
val age: Int;
def selectDynamic(name: String): Any;
def applyDynamic(name: String)(args: Any*): Any
}
]
import scala.language.dynamics
class RecordY(elems: (String, Any)*) extends Dynamic:
private val fields = elems.toList
val name: String = "A"
val age: Int = 1
def selectDynamic(name: String): Any = fields.find(_._1 == name).get._2
def applyDynamic(name: String)(args: Any*): Any = ???
/*transparent inline*/ def makeRecY() =
// Ok
// RecordX("name" -> "Hemma", "age" -> 6).asInstanceOf[ RecordX ]
// Fails
RecordY("name" -> "Hemma", "age" -> 6).asInstanceOf[
Dynamic {
val name: String;
val age: Int;
def selectDynamic(name: String): Any;
def applyDynamic(name: String)(args: Any*): Any
}
]
@main
def mainRecordCheck(): Unit =
val person5 = makeRecX()
println(person5.name)
val person6 = makeRecY()
println(person6.name) |
The problem here doesn't really seem to be related to varargs. If we compile the initial example with person5.applyDynamic("applyDynamic")(
["selectDynamic",["name" : Any]* : Any]*).$asInstanceOf[Any].
$asInstanceOf[Any].$asInstanceOf[(person5.name : String)] That's because the return type of person5.name to person5.selectDynamic("name").asInstanceOf[String] it might further try desugaring it into person5.applyDynamic("selectDynamic")("name").asInstanceOf[Any].asInstanceOf[String] because |
@prolativ Thank you for the explanation. At first glance what you say seems to make sense. But why would the compiler try further de-sugaring? Is their another condition where this must be so? I am assuming the marker trait Reason I ask these questions is to know whether this issue has any chance of going forward or if I should close this. TIA |
Right, actually the compiler tries to go one step further with desugaring and gets into something like person5.applyDynamic("applyDynamic")("selectDynamic", "name").asInstanceOf[Any].asInstanceOf[Any].asInstanceOf[String] or actually person5.applyDynamic("applyDynamic")(
["selectDynamic",["name" : Any]* : Any]*).$asInstanceOf[Any].
$asInstanceOf[Any].$asInstanceOf[(person5.name : String)] as the compiler sees it at this point. In general |
@prolativ Thank you once again. Had not considered the complexities regarding the |
Let's track this under #18009, closing this issue as duplicate. |
Compiler version
Version
3.4.1-RC2
Minimized code
In
makeRecX()
, if we return aRecordX
, it works. Current code fails.Example above in scastie
Output
Expectation
I expected that to work. Although this error seems to be similar to this issue, it occurs in the simpler case when accessing a member (
selectDynamic
and notapplyDynamic
), hence this report.The text was updated successfully, but these errors were encountered: