Structural types can lie when combined with values of erased types
See original GitHub issueCompiler version
3.0.0-M3
Minimized code
This is an example from the documentation that has been slightly modified.
Note the type of val age: 12
.
We (dynamically) assign the value 42
and then access it from an instance whose type has been refined.
class Record(elems: (String, Any)*) extends Selectable:
private val fields = elems.toMap
def selectDynamic(name: String): Any = fields(name)
type Person = Record { val name: String; val age: 12 }
val person = Record("name" -> "Emma", "age" -> 42).asInstanceOf[Person]
println(s"${person.name} is ${person.age} years old.")
Output
Emma is 42 years old
Expectation
An exception should be thrown ?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Java generics type erasure: when and what happens?
Type erasure applies to the use of generics. There's definitely metadata in the class file to say whether or not a method/type is...
Read more >Background: how we got the generics we have
Erasure is the technique of mapping richer types at one level to less rich types at a lower level (ideally, after performing sound...
Read more >Overcoming type erasure in Scala - Medium
Scala has a really strong type system. Existential types, structural types, nested types, path-dependant types, abstract and concrete type ...
Read more >Generics and Type-erasure
As another example consider that overloading a method with same generic class fails in Java: public void doSomething(List<One>); public void ...
Read more >Type Erasure in Java Explained - Baeldung
Type erasure can be explained as the process of enforcing type constraints only at compile time and discarding the element type information ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For the record, I think the only safe way of using Dotty’s new record/selectDynamic abstraction (i.e., without having to use
asInstanceOf
, which is unsafe and error-prone) is through carefully-written whitebox macros. Similar to the old scala-records library for Scala 2.It’s a bit unfortunate that despite the whole
Selectable
reworking, the situation has not improved and is still fundamentally the same.@sjrd I did not expect the compiler to tell me it’s incorrect, I would have expected, as mentioned in the
Expectation
section, the runtime to do so. While I still think this situation is unfortunate I understand it’s a limitation of the JVM so I will close the issue.