Blog

At 51zero we pride ourselves on being flexible when it comes to language choice. Java, Scala, C#, and more recently, Kotlin. Kotlin is a JVM language developed by Jetbrains (the people behind IntelliJ amongst other things) and recently has just reached version 1.0 public. We’ve even ported ScalaTest over to Kotlin in the guise of KotlinTest (of which we’ll do a blog soon).

There are some great articles already published on the main features of Kotlin, for example here, here and here, which are useful introductions for Java developers wishing to look at a different language. This blog is for Scala developers interested in having a play with Kotlin, where we review features of Scala you use every day, and their equivalents, or closest features, are in Kotlin.

Data classes vs Case classes

One of the main selling points of Scala when you first start to use it over Java is the use of case classes to take away all the boilerplate from Java beans. (Quite why Java didn't add this years ago I have no idea, surely the addition of an annotation that the compiler can pick up on wouldn't break existing code). But anyway, Kotlin has the same case class functionality in the form of data classes. The minor difference is that Kotlin won’t automatically infer the constructor parameters to be fields, so you must declare them as either val or var. Eg, the following scala case class:

caseclassPerson(name:String, age:Int)

Would become:

data classPerson(val name: String, val age: Int)

The automatic derivation of equals(), hashCode() etc works in the same way as Scala.

Implicit Conversions

Kotlin doesn’t support implicits as such but it does allow us to achieve one common use of implicits, which is the pimp-my-library pattern. In Scala, when we want to invoke foo() on a type A which contains no foo() method, the compiler will check in the appropriate scopes for an implicit method that accepts an A and returns a type B which does have the foo() method. So we create another type B, which contains all the extra methods we want for A, and then add a conversion from A to B (or mark B as an implicit class that accepts an A). In Kotlin we can use extension methods defined on A directly.

Option

Kotlin doesn't have an Option type provided by the SDK (other than the Optional type available in Java 8) instead focusing on offering null safety as a first class feature. Whether you prefer this over the Haskell inspired Options is a matter of preference. I’ll explain a way to do something very common with options.

Often given an option returning method, you want to map over the result before calling getOrElse or something similar. The equivalent in Kotlin would be the ?. operator which says, if the value is null, keep it null, otherwise execute the method. So this

map.get("key").map(_.foo)

Would become

map.get("key")?.foo

Getting the value out of the option in Scala usually involves a getOrElse or a fold. In Kotlin we would use the so called Elvis operator (am I the only one that thinks this name is ridiculous! Let's call it the getOrElse operator). So this

option.getOrElse(-1)

would become

value ?: -1

Property access

In Scala, we can access methods that declare a single empty parameter list without the parenthesis, so that methods and fields can both appear as properties. Eg,

classFoo{def wibble():String="wibble"}

Can be invoked as either foo.wibble or foo.wibble()

In Kotlin, we can do something similar for methods that adopt the java-bean standard. So if you have a method called fun getName() then we can do foo.name and that will be compiled into a call to the getName() function.

Kotlin provides something similar for setters. If there is a method called setName(str: String) then we can invoke this as either foo.setName(“moo”) or foo.name = “moo”. The second form will be compiled into the first form.

Operator Overloading

Scala allows us to declare methods with almost any operators to make (for what its proponents would argue) richer and more readable symbols for methods where operators make sense (the canonical example tends to be matrices, vectors or complex numbers). The opponents would say you end up with things like this. Personally, I don’t mind either way.

Kotlin allows us to override some operators, but only ones predefined. Eg, plus, minus etc. To do this we just declare a method with the name that matches the symbol, and annotate the function declaration with the keyword operator. Eg

Pattern Matching

Kotlin unfortunately doesn't support full pattern matching, so you cannot do things like

person match{casePerson("bobby", age)=>case_=>}

There is a limited form of extraction, which you can read about later, and there is a replacement for switch which allows matching on types and invocation of functions on the argument. This uses the keyword when, eg,

Anonymous Lambda Parameter

In scala we can refer to the lambda parameter using an underscore, eg list.map(_.name) which is very concise and very readable. Kotlin doesn’t offer a direct equivalent but there is an implicit it parameter available: list.map { it.name }.

This has the drawback that its ever so slightly more verbose, but offers the advantage that you can use it in multiple places, eg, in Scala we can’t do

list.map(_.firstname +_.lastname)

but instead must introduce a parameter

list.map(x => x.firstname + x.lastname)

In Kotlin we can use the it parameter multiple times, eg

list.map { it.firstname + it.lastname }

Companion objects

Companion objects are supported in Kotlin using slightly different syntax. Instead of declaring an object with the same name, declare an object with the keyword companion inside the class itself.

classFoo {
companion object {
// methods here
}
}

Extractors

While kotlin doesn’t have extractors as such, it does support a limited form of extraction called desugaring declarations which go some way to performing the same job. Any class that implements the functions component1(), component2(), and so on (similar to the product() provided by Scala’s Product1, Product2, ProductN traits) can be desugared into component variables. For example

Microsoft is preparing to release the second version of its Open CloudServer design for use with the the Open Compute Project (OCP). Dubbed “Project Olympus,” it lives up to its Goliath namesake by boasting significant improvements over its predecessor.

First released as a Technical Preview version on October 1, 2014 and later released under general availability on October 12, 2016, Windows Server 2016 is the newest version of Microsoft's signature server operating system. It was developed alongside Windows 10, offering many of the same functions and features. Of course, Windows Server 2016 also introduces several new features, including Active Directory Federation Services, Windows Defender antimalware, Remote Desktop Services to support OpenGL 4.4 and OpenCL 1.1., Storage Services, Failover Clustering, Web Application Proxy for preauthorization of HTTP Basic, and ISS:10 support for HTTP/2, and Windows PowerShell 5.1.

If you currently use Microsoft's Hadoop offering, you'll be pleased here about several new features and enhancements that are being added. Earlier this month, Microsoft announced that it's cloud-based Hadoop service, Azure HDInsight, would receive a security upgrade and performance boost. Microsoft claims these changes will provide greater security for users, while also speeding up Big Data queries by as much as 25x.

Microsoft and Huawei have joined forces with EastWest Institute to help IT organizations and professionals securely buy informations and communications technology (ICT) products. The powerhouse tech companies say ICT buyers are at risk for cyber attacks when using commercial products and services. And with phishing and ransomware attacks on the rise, this isn't a trend that's likely to go away anytime soon.

Red Hat, Inc., the North Carolina-based multinational software company that's best known for its enterprise operating system Linux, has released a new version of its virtualization product, Red Hat Virtualization (RHV).

Virtual machines (VM) have become a hot commodity among IT companies and organizations. They perform the functions of a standalone computer or server in an emulated environment. But if you're thinking of investing in a preemptive virtual machine, you should check out the service offered by Google, because the Mountain View company has just slashed the prices for such services.