! operator and evaluation



I’m playing with Kotlin -> Javascript and have this simple class:

class Box(val tag : String) {

  public val element: Element = window.document.createElement(tag)!!


And this is part of the generated Javascript:

var classes = function(){

  var tmp$0 = Kotlin.createClass({initialize:function(tag){

  this.$tag = tag;

  this.$element = window.document.createElement(this.get_tag()) != null?window.document.createElement(this.get_tag()):Kotlin.throwNPE();



As you can see, window.document.createElement is called two times. Is this really how it’s supposed to work?

Regards, Flow


It is certainly a bug. Feel free to report it to the tracker.


Ok, I will create a bug report of this. I'll just verify with Kotlin M3 as well. Thanks!


No need for a bug report. The generated Javascript looks just fine in Leda.

initialize: function (tag) {

          var tmp$0;

          this.$tag = tag;

          this.$element = (tmp$0 = window.document.createElement(this.get_tag())) !== null && tmp$0 !== undefined ? tmp$0 : Kotlin.throwNPE();



I'm glad the bug is fixed, although I think it would be better if we emitted a single function call like

initialize: function (tag) {           var tmp$0;           this.$tag = tag;           this.$element = Kotlin.assertNotNull(window.document.createElement(this.get_tag()));   },

The code is shorter, and the JIT will probably do a good job here.


I've already created an issue suggesting this. :)


Regards, Flow