Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
Scalajs, js.Dictionary inconsistent behavior

I have an inconsistent error using ScalaJS. Here is a minimal example: the size and the content of a dictionary variable are inconsistent.


def A(mapping: js.Dictionary[String]): Unit = {
  dom.console.log(mapping)
  dom.console.log(mapping.size)
}

I link the IRs using fastOptJS. In the driver code in html, I have

const colorSetting = new Map([
        ["1", "black"]
]);
ScalaJSExample.A(colorSetting)

In the console, the first line prints the content of the variable correctly,

"Map {"1" => "black"}"

but the second line prints 0. I have also tried to swap the position of the two lines, but the result is the same. Any methods such as .foreach or the for (x <- mapping) all treat the variable mapping as empty.

Any suggestion why this is the case? I am using Scala 2.12.8, ScalaJS 1.7.1.

Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A js.Dictionary represents a POJO (plain old JavaScript object) where elements are fields. But you are passing a JS new Map() to it, which is not a POJO--it's a Map. The correct type to represent a JS Map is js.Map.

Either pass a dictionary to ScalaJSExample.A, as follows:

const colorSetting = {
    "1": "black"
};
ScalaJSExample.A(colorSetting)

or declare mapping as a js.Map in the Scala.js code:

def A(mapping: js.Map[String, String]): Unit = {
  dom.console.log(mapping)
  dom.console.log(mapping.size)
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!