4 ms·
Closure Compiler uses type annotations on variables and functions that give information on null and undefined. These annotations look like {?Object|undefined} (
by oever 10y ago
Closure Compiler uses type annotations on variables and functions that give information on null and undefined. These annotations look like {?Object|undefined} (Object may be null or undefined) and {!Object} (object is guaranteed to not be null).
In Java, NullPointerExceptions is huge problem. IDEs have plugins to check for this error, but there is no standard solution.
Does Scala help in avoiding NullPointerExceptions? Do function signatures specify if a value may be null?
- gmartres 10y agoScala does not have non-nullable types currently (but that might happen in the future), in practice this is not a problem simply because using null is seen as a code smell and basically no Scala library API expects parameters to be null or return null, optional values are usually represented with the standard library class Option: http://www.scala-lang.org/api/2.11.8/#scala.Option http://www.scala-lang.org/api/2.11.8/#scala.Option
- oever 10y agoOne of the main points of the blog is that Scala.js integrates well with the JavaScript apis and those apis are full of null. A typical loop in the DOM looks like this: var node = element.firstChild; while (node) { // do something node = node.nextSibling; } Perhaps those interfaces use the Option class?
- oever 10y agoI wrote a small test function. It appears that Scala.js has no default protections against null values from the browser DOM. def printNodes(targetNode: dom.Node): Unit = { var c = targetNode.firstChild while (c != null) { if (c.nodeValue != null) { println(c.nodeValue) } c = c.nextSibling } } The return value from firstChild and nextSibling can be null.
- deleted 10y ago[deleted]