Kotlin is a small language


#1

My adoption of Kotlin is purely as a language targeting the Android platform so I have very little experience developing with Java and the JVM (I am primarily a C# developer). I started writing a quick guide to Kotlin two weekends ago just to exercise my knowledge of the language and I am surprised to discover that I am using most of the language features on my daily Android development (including all the myriad features related to functions).

Features that I haven’t used in my projects are local class (defining a class inside a function), delegated class, and the brand new delegated properties.


#2

I actually realize that local class is a great way to hold throw away json values

import com.github.kevinsawicki.http.HttpRequest import com.google.gson.Gson

public fun googleShorten(url: String): Result<String>
{
  class shortenData (public val longUrl: String)
  class shortenDataReply(public val kind: String, public val id: String, public longUrl: String)

  try
  {
  val gson = Gson()
  val payload = gson.toJson(shortenData(url))
  var pst = HttpRequest.post(“https://www.googleapis.com/urlshortener/v1/url”)
  pst?.contentType(“application/json”)
  pst?.send(payload)

  &nbsp;&nbsp;val code = pst?.code()

  &nbsp;&nbsp;if (code == 200){

           val reply = pst!!.body()!!
           val s = gson.fromJson(reply, javaClass<shortenDataReply>())!!
           return Result.right(s.id)
  }
  else{
           return Result.wrong<String>(null)
  }
  } catch(ex: Exception){
  return Result.wrong<String>(ex)
  }
}


#3

I think you can omit public in local classes.