What sets apart an object within the scalajs scope from the exact same object within the js.global scope?

Attempting to create a basic example for rendering a cube using the THREEJS library.

package three

import org.scalajs.dom

import scala.scalajs.js
import scala.scalajs.js.Dynamic._
import scala.scalajs.js.annotation.JSName

...

object ThreeExample
{
  def render() =
  {
    val scene = new THREE.Scene()
    val camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
    val renderer = new THREE.WebGLRenderer(js.Dynamic.literal(
      canvas = global.document.getElementById("ThreeCanvas")
    ));
    val geometry = new THREE.BoxGeometry(1,1,1);
    val material = new THREE.SimpleMeshBasicMaterial()
    camera.position.z = 2;
    val cube = new THREE.Mesh(geometry, material);

Now comes the interesting part. When attempting to do this:

scene.add(cube);
renderer.render(scene, camera);

An error is thrown:

ScalaJS.c.jl_ClassCastException {s$1: "[object Object] is not an instance of scala.runtime.Nothing$", e$1: null, stackTrace$1: null, stackdata: TypeError, constructor: function…}

But when trying this instead:

global._scene = scene
global._cube = cube
global._camera = camera
global._renderer = renderer

global._scene.add(global._cube)
global._renderer.render(global._scene, global._camera);

Everything renders properly without any errors. So, what's the difference between `global._scene` and `scene`, or `global._scene.add` and `scene.add`?

Answer â„–1

Expanding on my previous explanation: there is truly no significant difference between the two scenarios. The statically typed interface acts as a labeled facade for the same functionalities utilized in the dynamically typed interface. However, due to its static nature, it enforces validations on the values returned by the called methods.

For example, when you execute renderer.render(), the compiler ensures that the value generated by this JavaScript function matches the predetermined result type of WebGLRenderer.render(). But what exactly is this expected result type? It's specified as Nothing! Why? Because the method's implementation remains as ???, indicating a type of

Nothing</code, hence leading scalac to deduce the output as <code>Nothing
. Consequently, the validation fails since the return value from render in JavaScript does not fit into the category of
Nothing</code (simply because no valid value falls under this classification), resulting in a <code>ClassCastException
.

The intended outcome here is clearly not Nothing, but rather Unit. Therefore, specifically declaring this result type using

: Unit</code resolves the issue:</p>

<pre><code>def render(scene:js.Object, camera:js.Object): Unit = ???

In general, it is highly advisable to explicitly state the result types of methods within facade types, as otherwise they default to being interpreted as Nothing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Accept JSON data in ASP.NET MVC action method for posting data

I have a model class named Parcel which contains the parameters Name and CenterPoint: public class Parcel { public string Name { get; set; } public object CenterPoint { get; set; } } The values for these parameters are obtained from a map. When a ...

Exploring Manipulation of M:N Associations in Sequelize

I have set up a sequelize schema using postgre DB export const Commune = sq.define("commune",{ codeCommune: { type: DataTypes.STRING(5), allowNull: false, primaryKey: true }, libelleCommune: { type: ...

I can't seem to establish a connection with my MongoDB Atlas cluster. I encountered the MongooseError, which is as follows:

Error [MongooseError]: The uri parameter for the openUri() method needs to be a string but is currently set as "undefined". Please ensure that the first parameter for mongoose.connect() or mongoose.createConnection() is a valid string. const express = r ...

Difficulty switching back and forth between three varying heights

I have a container with a button labeled "Show more". Upon clicking the button, the height of the container will transition through 3 different states. <div class="segment-suggestion-content height-small"> <div class="segment-suggestion-sh ...

The window.addEventListener function is failing to work properly on mobile devices

Hey there! I am facing an issue in my JS code. I wrote this code because I want the menu to close when a visitor clicks on another div (not the menu) if it is open. The code seems to be working fine in developer tools on Chrome or Firefox, but it's no ...

What are the reasons for not accessing elements in a more "direct" way like elemId.innerHTML?

Recently, I came across a piece of JavaScript code that accesses HTML elements using the shorthand elementID.innerHTML. Surprisingly, it worked perfectly fine, but interestingly, most tutorials opt for the traditional method of using document.getElementByI ...

PHP MySQL - Automatically trigger email notification upon insertion of a new record

Is there a way to trigger an email alert only when a new record is added, not when the user edits input fields in a table? The database I'm working with stores equipment delivery dates. Users schedule deliveries and input the dates into a SQL Server ...

Transforming a d3 line chart into a bar chart

Beginner in D3 seeking assistance. I have successfully created a line chart that meets my requirements. Below is the code I used, with some modifications, mostly inspired by this source: <!DOCTYPE html> <meta charset="utf-8"> <sty ...

Angular sends a POST request using $http to open in a new tab

In my Angular application, I am trying to send a POST request to a URL ./makeFile.php. This request will create a file with contents retrieved from a database query based on the data provided in the POST. When using PHP, the browser automatically opens a ...

Unable to retrieve push token for the device. Please verify the validity of your FCM configuration

Looking for a solution to the issue of obtaining a push token Error: Unable to retrieve push token for device. Ensure that your FCM configuration is correct I have integrated Expo permissions and notifications in my React Native app, but nothing seems to ...

What is the best way to include React globally when running unit tests?

In my Rails project, I have integrated the react-rails gem, which sets up a global variable like this: window.React = React; While this is convenient for regular usage, it causes issues when running unit tests with Jest because the global React variable ...

Creating a private variable in Javascript is possible by using getter and setter functions to manage access to the

Is this the correct way to simulate a private variable? var C = function(a) { var _private = a + 1; // more code... Object.defineProperties(this, { 'privateProp': { get: function() { return _privat ...

Utilizing jQuery Ajax to submit multiple forms using a single selector in one go

I'm having an issue with jQuery Ajax involving multiple forms. Each time I execute it, only the top form works properly while the others do not function as expected. Can anyone provide assistance with this? Here is the source code: <form id="form_ ...

Automatically generate the first user on the Parse Server system

Is it feasible to programmatically create a User on Parse server without the need for sign up? More information can be found at https://github.com/parse-community/parse-server We attempted this using cloud code. var user = Parse.User(); user.setUserna ...

Unlock the power of Angular JS to display dynamic content effortlessly

My experience with AngularJs is very limited. In one of my projects, I need to display dynamic content on a page that changes based on the database values retrieved via an HTTP GET request. Before implementing the entire functionality, I decided to test i ...

Discovering the Voice Channel of a User using Discord.js v13

I'm currently working on a 'wake up' command for my bot that should move the mentioned member between 2 specific voice chats and then return them to their original VC. I've successfully got the bot to move me between those 2 VCs, but no ...

Python's method for passing an array to a Django template

If I had an array in my Python backend and needed to send it to the front end as JSON or a JavaScript array, how could I accomplish this utilizing the Django framework? A possible template implementation is as follows: <script type="text/javascript"&g ...

Using JavaScript, enter the value into the input field and then redirect the

I'm encountering a minor issue with my contact form and jQuery redirection based on input fields. Everything was working smoothly until I attempted to integrate the redirection into the form validation code. Redirection snippet: $("#signup").submit ...

Exploring ways to traverse a JSON encoded object in PHP with the help of JavaScript

I am facing an issue while trying to access my data from PHP. I am using the following code: echo json_encode($rows); When I comment out datatype: 'json', I can see a normally encoded JSON string. But when I use it, the alert shows me an array ...

The equivalent of the $.curCSS method in jQuery version 1.10 is as follows:

While working with a library called mcdropdown from http://www.givainc.com/labs/, I encountered an issue with the $.curCSS method. The latest version of jQuery no longer supports this method and suggests using $().css instead. Following the documentation, ...