question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Structural types can lie when combined with values of erased types

See original GitHub issue

Compiler 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:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
LPTKcommented, Feb 19, 2021

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.

0reactions
jtocommented, Feb 20, 2021

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found