SysOutNull (bpfurtado) wrote,
SysOutNull
bpfurtado

Kotlin Hello World

Yesterday I did a Hello World in Kotlin following the first free chapter of the book Kotlin in Action:

   1 data class Person(val name:String, val age: Int? = null)
   2 
   3 fun main(args: Array<String>) {
   4 
   5     val persons = listOf(Person("Alice", age=35), Person("Bob", age=27))
   6     val oldest = persons.maxBy { it.age ?: 0 }
   7     
   8     println("The oldest is: $oldest")
   9 }

Using this nice web IDE for experimentation: try.kotlinlang.org there is an option to see the kotlin source code converted to Javascript:

   1 Kotlin.out.flush();
   2 (function (Kotlin) {
   3   'use strict';
   4   var _ = Kotlin.defineRootPackage(null, /** @lends _ */ {
   5     Person: Kotlin.createClass(null, function (name, age) {
   6       if (age === void 0)
   7         age = null;
   8       this.name = name;
   9       this.age = age;
  10     }, /** @lends _.Person.prototype */ {
  11       component1: function () {
  12         return this.name;
  13       },
  14       component2: function () {
  15         return this.age;
  16       },
  17       copy_bm4lxs$: function (name, age) {
  18         return new _.Person(name === void 0 ? this.name : name, 
  19             age === void 0 ? this.age : age);
  20       },
  21       toString: function () {
  22         return 'Person(name=' + Kotlin.toString(this.name) + (', age=' + 
  23             Kotlin.toString(this.age)) + ')';
  24       },
  25       hashCode: function () {
  26         var result = 0;
  27         result = result * 31 + Kotlin.hashCode(this.name) | 0;
  28         result = result * 31 + Kotlin.hashCode(this.age) | 0;
  29         return result;
  30       },
  31       equals_za3rmp$: function (other) {
  32         return this === other || (other !== null && (typeof other === 'object' && 
  33             (Object.getPrototypeOf(this) === Object.getPrototypeOf(other) && 
  34                 (Kotlin.equals(this.name, other.name) && 
  35                     Kotlin.equals(this.age, other.age)))));
  36       }
  37     }),
  38     main_kand9s$: function (args) {
  39       var oldest;
  40       var persons = Kotlin.modules['stdlib'].kotlin.collections.listOf_9mqe4v$(
  41           [new _.Person('Alice', 35), new _.Person('Bob', 27)]);
  42       maxBy_l82ugp$break: {
  43         var iterator = persons.iterator();
  44         if (!iterator.hasNext()) {
  45           oldest = null;
  46           break maxBy_l82ugp$break;
  47         }
  48         var maxElem = iterator.next();
  49         var tmp$0;
  50         var maxValue = (tmp$0 = maxElem.age) != null ? tmp$0 : 0;
  51         while (iterator.hasNext()) {
  52           var e = iterator.next();
  53           var tmp$1;
  54           var v = (tmp$1 = e.age) != null ? tmp$1 : 0;
  55           if (Kotlin.compareTo(maxValue, v) < 0) {
  56             maxElem = e;
  57             maxValue = v;
  58           }
  59         }
  60         oldest = maxElem;
  61       }
  62       Kotlin.println('The oldest is: ' + Kotlin.toString(oldest));
  63     }
  64   });
  65   Kotlin.defineModule('moduleId', _);
  66   _.main_kand9s$([]);
  67 }(Kotlin));
  68 
  69 Kotlin.out.buffer;
That is cool to see.
Tags: idea, javascript, kotlin, programming-language
Subscribe

  • Rust, the most loved Programming Lang of 2016

    I was remembering details of the LLVM project (or more specifically an umbrella of projects), driven by this wonderful article about Graal &…

  • Indenting Javascript

    As I was doing my nightly session of hacking content from sites (heavy users of AJAX) I could finally get into a set of more than 2800 lines of…

  • Ruby: faster on a Javascript VM?!?

    This John Resig's post talks about HotRuby, a project that sends Ruby code to be transformed into the opcodes of a Ruby VM written in Javascript.…

  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments