Andy Wilkinson wanted some useful tips for Kotlin in the style of my normal blog. I know nothing about Kotlin other than it’s vaguely Swifty and has a little too much Java in it for my decaf tastes. Nonetheless, I decided to oblige.
Here’s what I discovered.
Install Kotlin
Installation couldn’t be simpler. I used brew: brew install kotlin, which installed a copy to /usr/local/bin/kotlin on my first try. Woo!
Say “Hello World”
You run the Kotlin REPL with kotlinc-jvm, which I did not guess on my first try and had to hunt around doing web searches to discover:
Next, I tried to create an app. Once again I discovered that Kotlin is a little less straightforward than Swift in this regard.
Notice that you have to call kotlinc, the command line compiler, not kotlin. Then you can run the 736709-byte large jar
results with (ew) java
.
Unfortunately, it’s not as simple as one might hope to create a straightforward script:
However if you rename the file from hello2.kt
to hello2.kts
, you can run it as a command-line script:
Whee.
Hop into Xcode
Create a new Cross-platform Xcode project: File > New > Project > Cross-platform > External Build System > Next. Enter a name, and edit the build tool to kotlinc. Click Next. Navigate to a save location and click Create.
Choose File > New > macOS > Other > Empty. Click Next. Name the file HelloWorld.kts. (See the kts
extension? This allows it to be run as a script.)
Edit your Run scheme (Scheme > Edit Scheme or Command-<). Choose the Info tab, select Other from the Executable pop-up list.
Type a forward slash, then continue to navigate to /usr/local/bin. Select kotlinc, uncheck Debug executable, and then click Close.
Next choose the Arguments tab and edit the arguments passed on launch to include both the -script flag and the source file:
You’re now ready to run:
Do something interesting
Now that you have the basics all ready to play with, create a slightly more interesting testing environment. For example, pull up Project Euler and challenge yourself to a few of their ideas:
That Kotlin “Feeling”
As when I played around with Python, I was surprised at Kotlin’s familiarity.
- The
println
commands felt quaint and old fashioned like Swift 1. - The cascading dot sequence (which I cut and pasted for the second result, and Xcode didn’t automatically indent) felt as fresh as modern Swift. I also felt pretty confident in my overall Swift-influenced style despite jumping into an entirely new language.
- Extending Int with the
divisibleBy
function was a little weird and I deliberately used the=
syntax to define my function rather than a more familiar brace and return, just so I could say I tried to be more authentically kotlinny. - The two dot range did catch me but it was an easy fix from three dots to two.
- I quite liked using
$
-delimited variable names compared to Swift’s\()
insertion sequence.
Given that other than a few math functions and hello world, this post contains all my Kotlin code to date, I don’t have much more to add about the language similarities and differences, but let me know if you’d be interested in reading more about “Erica learns Kotlin”. Otherwise, I’ll get back to doing other things during this extremely dead week before WWDC starts.
3 Comments
Thanks for sharing!
I liked your comment that Kotlin’s “cascading dot sequence . . . felt as fresh as modern Swift.” I actually like that languages have trends. It’s an acknowledgement that programming languages are just that — languages. They aren’t, to me anyway, mere engineering constructs — they’re things we want to read, and we like to read modern stuff.
Anyway, I know posts like this are a lot of work so thanks for taking the time!
More,. More !!
Why on earth would you use xcode for kotlinlang.org? That sounds rather masochistic. IntelliJ is light years better