! operator and evaluation


#1

Hi!

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


#2

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


#3

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


#4

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();

  },


#5

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.


#6

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

http://youtrack.jetbrains.com/issue/KT-2976

Regards, Flow