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.

[scala.js] Dotty does not produce null/undefined property on anonymous class

See original GitHub issue

Compiler version

3.1.0

Minimized code

  • With Scala.js 1.8.0
trait Foo extends js.Object {
  var extra: js.UndefOr[String]
}

val anon1 = new js.Object() {
  var extra = "a"
}
val anon2 = new js.Object() {
  var extra = js.undefined
}
val anon3 = new js.Object() {
  var extra = null
}
val foo1 = new Foo {
   var extra = "a"
}
val foo2 = new Foo {
  var extra: js.UndefOr[String] = js.undefined
}
val foo3 = new Foo {
  var extra: js.UndefOr[String] = null
}
println(js.Object.hasProperty(anon1, "extra"))
println(js.Object.hasProperty(anon2, "extra"))
println(js.Object.hasProperty(anon3, "extra"))
println(js.Object.hasProperty(foo1, "extra"))
println(js.Object.hasProperty(foo2, "extra"))
println(js.Object.hasProperty(foo3, "extra"))

Output

On Scala 2.13.7 and 2.12.15,

true
true
true
true
true
true

However on Scala 3.1.0,

true
false
false
true
true
true

which means null or undefined property on anonymous class is missing.

val foo2 = new Foo {
  var extra: js.UndefOr[String] = js.undefined
}
val foo3 = new Foo {
  var extra: js.UndefOr[String] = null
}

Expectation

All properties should exist even if its value is null or undefined.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sjrdcommented, Jan 21, 2022

This appears to happen only when the type of the var is Unit or Null (whether inferred, or explicitly written). It does not seem to be related to having a supertrait, since I can also reproduce the issue when the trait declares the member but the class implements it with a more specific type that is explicitly Null or Unit.

Also, I can reproduce this with a named class, local or top-level. So it’s also not related to anonymous classes.

The situations where this issue pops up are therefore summarized as:

  • A field of type Unit or Null in a non-native JS class.
0reactions
xerialcommented, Sep 19, 2022

@sjrd I also hit this issue when trying to implement an object comparison method like deepEquals(jsObjectA, jsObjectB) in Scala 3 + Scala.js.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Announcing Scala.js 1.3.1
Nov 16, 2020. We are pleased to announce the release of Scala.js 1.3.1! This release fixes the performance issues of the ECMAScript 2015 ......
Read more >
Implementing Scala.js Support for Scala 3
This post explores how Scala.js is implemented, and not so much how it is used, ... Not all compile-to-JS languages have that property!...
Read more >
A Beginner's Guide to Scala 3.0 - Morioh
In this post, we will get a teaser of what changes to expect from Dotty as ... Rememeber that a function in javascript...
Read more >
Content - 3811e52cdfcda707d47b0ed2f8fb560e94e67923
doubleValue = this.value * 2; }; } }); ``` If you don't have time to migrate the code at the moment, you can...
Read more >
Overview - TypeScript
Additionally, generators just assumed the type of yield was always any . ... Property 'answer' does not exist on 'typeof globalThis'.
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