Hacker News new | ask | show | jobs
by V-2 3770 days ago
Surprised noone mentions Kotlin. It's quite Swift-like, backed by JetBrains (Android Studio is based on their IntelliJ Idea), and 1.0 has only just been released. It has full interoperability with Java.

Given the above, I don't see much point in using Swift, unless it's one of these projects that are about proving a point (nothing wrong with that and often very interesting).

8 comments

I love Kotlin, but being able to develop libraries in one language and use them in both Android and iOS is huge. I have been using J2Objc for this until now, and while it's a great tool, it forces me to use Java, which I don't love. I would prefer being able to use Kotlin on iOS, but using Swift for Android development is a great boon.
AFAIK there's nothing stopping you using Kotlin on iOS with RoboVM since it is a JVM language - https://robovm.com.
True enough, but I really don't like depending on closed source solutions for something so fundamental as getting my code to run on a platform. I feel like I am betting the future of the product on the future of this company. Of course, I am already betting on the future of iOS and Android, but I have a lot more confidence on those too than on RoboVM
On top of that, RoboVM uses its own LLVM-based compiler rather than Apple's. So if Apple throws us a new compilation requirement, similar to the requirement to provide bitcode for watchOS and tvOS, then we're stuck until RoboVM is updated. With j2objc, you're using Apple's compiler to produce the machine code.
Could you develop libraries that way, though? It would have to be absolutely pure Swift, not using any iOS libraries, like NSURL etc. etc. - surely incredibly limiting.
Not true. Both Foundation and Core Foundation libraries will be eventually ported and open sourced.
This feels a little like wishful thinking. Remember YellowBox back when Apple originally bought NeXT?

YellowBox was going to be a cross-platform updated version of the OpenStep APIs. While I remember some alphas of it, it basically got nixed.

http://www.roughlydrafted.com/RD/RDM.Tech.Q1.07/4B800F78-0F7...

As much as I enjoy using Apple products, their history says that they would not directly support something like this.

Omg, I had no idea. Awesome!
Absolutely I could. In fact, as I said, I already am, albeit with a more limited technology. For building multiplatform apps I build all the business logic in a Java library which I then convert to iOS using J2Objc. IME this is always between a third and half of the code that is now shared between platforms. For classes that will depend on platform specific stuff, like newtork requests that are handled differently by iOS and Android I use interfaces that I later implement (as protocols) on each side. iOS does end up feeling as a second class citizen though, because J2Obj is not exactly fast; if I need a change in the library, a recompile can take a few minutes
You could this for your own libraries, but I would strongly recommend against doing this for a public lib if you want traction (e.g. if you are making a bunch of SDKs for your APIs) - in this case you should publish your lib in the most popular language(s) for the platform(s) you are targeting.
>but being able to develop libraries in one language and use them in both Android and iOS is huge

I have no doubt that there are mobile only apps that would find this valuable - but swift is still only linux/android and osx/ios - if you want truly portable code - that can compile to browser/mobile/server/desktop you're stuck with C++ for the foreseeable future.

I've tried Haxe, Racket, TCL/tk, MOAI (Lua based), and others, but I am now using 8th, a Forth-like language that compiles to Android, iOS, Windows, OSX and Linux [1]. The only downside is that I still need a Mac to publish iOS apps. For Android, Windows and Linux, I am good to go. It uses the JUCE library for gui implementation. I have made two toy apps for Android so far, and I am still learning. It has a good library so far, but some people find Forth difficult. I find it concise, expressive and still readable after 6 months. The shortness makes code review a breeze.

  [1] http://8th-dev.com/
What a strange beast! A closed source language is always a tricky sell. Forth is never an easy sell in itself (unless you've already been bitten by the bug ;-) in which case it's a wonderful bonus). The tutorial is PDF only - which just seems a bit odd.

I'm intrigued by 8th - in a good way - and happy you've introduced me to it - but I do wonder what kind of uptake something like this will get.

It is a bit odd, but I am rocking with it. I have done Java and NDK in C++ tutorials to get simple programs running, but nothing original, or beyond what the tutorial provided. With 8th, I can whip up something quick with a GUI, using the sensors and other libs it comes with, and deploy it. It looks the same on Windows, Android and Linux. The developer makes him self available, and quite honestly, the code is succinct enough and the documentation good enough that I only asked a lot the first week. You should give it a try. I am by no means a 'Forth programmer', and 8th is not a compliant Forth.
How so? Doesn't this port to Android show that a Windows port is surely possible? Swift is already running on servers afaik. As for compiling to "browser," Swift is based on LLVM so should be just as capable of this as C++.
What's "possible" and what's available and tested in production right now are two very different things. These kinds of ports take at least a year to get over the initial troubles and become usable and then you're at least that time away from a decent size of community libraries to spring up for the mentioned platforms.

By that time Rust will also mature enough and webasm will land so Rust will probably be a better candidate (since it's OSS and developed by a browser company that needs to run on as many platforms as possible they have much better incentives to maintain ports than Apple).

Also in that time C++ will probably get modules working on clang and many c++17 features.

And you might see Xamarin merge into CoreCLR and rolled up into VS integration.

A lot of things are possible - but what's available right now is C++ on every platform and not much else.

What's the canonical way of targeting Android with C++?
Write shared logic in C++ and use JNI/JNA ? Or are you asking about something more specific ?
I'm not sure about how mainstream/supported C++ is in the Android world.
Just as much as everywhere else, we're using it extensively for sharing code. If has a few qurks but it's easy better than windows for example.
Are you using NDK? I was reading about it earlier, it is something that I would like to look into, when I have some time.
Yes, NDK is after all just a name for preconfigured GCC and Clang package shipped by Google :)

We use Clang 3.5.

Yeah for an example of what I would love to be able to use on both android and ios : http://createdigitalmusic.com/2015/01/free-audiokit-lets-ios...
Two points for using Swift on Android: 1) shared code between your ios, mac, and android apps. 2) you already know swift.

That said, there is always a cost to using a non-standard language to develop for a platform. Things like having to translate documentation as you read it, friction between system libraries and your languages standard library, etc.

I've not looked at Kotlin in detail, but I wonder how much that friction is reduced because it's java-based, or if you'll experience just as much friction using Swift on Android as you would using Kotlin on Android. Worth evaluating if you're starting a project 6 months to a year from now as the Swift tooling for Android starts to mature.

As it stands now, I would have a look at Kotlin, but almost definitely write any projects started today in Java. Swift is nowhere near ready, and the advantage of writing in the platform's "native" language usually outweighs any benefits there are to using a "better" language.

>friction between system libraries and your languages standard library

Well, that's the thing : Kotlin emits bytecode. It is entirely interoperable with Java. All the Java APIs of the platform are accessible in Kotlin.

Swift on the other end can only target the NDK, which limits it to a very specific niche on Android.

Just because they are accessible, doesn't mean using them is seamless. For example all of Objective-C's runtime and classes are available to Swift but there is some friction e.g. methods that emit untyped NSArrays rather than typesafe swift arrays, or cases where you have to avoid using Swift structs because they can't be passed into the Objective-C runtime. Apple's done a lot of hard work, and each release of Swift has made this better, but it's still not entirely without hiccups... without having used Kotlin, I can't say for sure, but I would guess there are some analogous issues.
So far, it seems pretty seamless. I guess that with a couple of years working on a production app in Kotlin, I would have to discover some hairy corner cases though.

What helps a lot is that the kotlin team has written many helper methods allowing a better flow between the android API and kotlin code : while you don't need it in order to get interop, it allows to more easily write idiomatic kotlin code while interacting with Android.

I haven't ever wrote an Android app (just toyed around and patched a few projects, but never had an idea and a necessity to do a full one from scratch), but what's the limitation here?

Can't a native library call just anything from the Android/Java world?

I mean, NDK library has JNIEnv* pointer and it can call FindClass/GetMethodID/Call*Method. And it can create objects/functions if it needs to be called back. Of course this isn't fun in the raw form, but don't see any reason it can't be well sugared.

You don't gain anything except a huge debugging pain and a large loss of performance. Debugging JNI and performance loss of translation makes not using JVM downright madness.
The alternative of course being C++14 with first party support for each OS out there and maturer ecosystem.
Some people like to avoid Java and languages dependent on JVM. For them any non JVM based solution on android is welcome change. So efforts towards that need no justification as to why any of numerous JVM language is not used first.
Except all major Android APIs are Java only, even pure native apps are actually loaded as .so into a Java process.
The point is lot of application logic can be written in Go and Swift hopefully soon. That code can be shared across iOS / android app. It is still net win for people who do not like Java/JVM.
As someone that mainly codes in C++ across Android and Windows Phone for hobby projects, there is no escape from JNI for any application of significant size.

Have you used the NDK? You cannot even access the filesystem outside the APK deployment tree without a few JNI calls.

Guess what. No one cares if you don't use Swift. Go with Kotlin. It's probably a great choice for you. It seems like a great language and I hope it gains traction.

You're doing that nerd thing where you need to tell the world why something isn't good for you. We'd waste a lot less time if we simply found the group we desired and added value there rather than find another group and tell them why what they're doing doesn't work for you.

He's saying that Kotlin is similar enough to Swift, but integrates far easier with the Android ecosystem and development environment, such that it's probably a better choice for a lot of people wanting to make Android apps with a modern "swift-like" language. I think that's pretty relevant.

You're doing that nerd thing where you're assuming we're divided into groups based on the technology we're using. We'd waste a lot less time if we simply tried to find the best tool for the job, rather than joining with a group and sticking with them blindly.

If all you're doing is Android programming Kotlin is probably a better choice at this time. If you're a Java programmer, Kotlin is probably a better choice. If you're already a Swift programmer...

Also, let's see how both languages gain traction in the next 3-5 years.

I wish I had a thousand up votes for this post. it's easier to change your group than to change the group.
The main problem with Swift on Android is that AFAIK it is going to be limited to NDK.

There is certainly a niche where it can be useful, but for most developers, it makes it a no go.

Kotlin on the other end is indeed a very good stand-in replacement for java on Android.

Swift is compiled. One can expect better performance.

The target is probably iOS developers.

Swift gets most of its performance gains by not doing GC and better struct support.

I have a distaste for Java but it's performance problems stem mostly from the way people write it and how hard it discourages writing in a performant style.

Aren't these "performance gains" a wash due to the overhead of ARC?
Not really, reference counting is generally considered to be more performant than GC. GC can end up thrashing the cache and doesn't work very well when using more than 8 to 16 GB of RAM.

Even the linux kernel uses reference counting as you need a rc / gc system to solve some problems.

ARC just inserts the retain/release calls that you'd make anyway, pretty much everyone agrees that there are very few degenerate cases with ARC and in the vast majority of cases it does the best that can be done, which is why almost everyone uses ARC now. (You can still turn it off if you really want in ObjC)

I came across this explanation on StackExchange on the performance penalties with ARC:

>As for performance both have performance penalties. Automatic reference counting delivers a more consistent performance, no pauses, but slows down your application as a whole as every assignment of an object to a variable, every deallocation of an object, etc, will need an associated incrementation/decrementation of the reference counter, and taking care of reassigning the weak references and calling each destructor of each object being deallocated. GC does not have the performance penalty of ARC when dealing with object references however it incurs pauses while it is collecting garbage (rendering unusable for real-time processing systems) and requires a large memory space in order for it to function effectively such that it is not forced to run, thus pausing execution, too often.

>As you can see both have their own advantages and disadvantages, there is no clear cut ARC is better or GC is better, both are compromises.

>PS: ARC also becomes problematic when objects are shared across multiple threads requiring atomic incrementation/decrementation of the reference counter, which itself presents a whole new array of complexities and problems.

> Swift is compiled. One can expect better performance.Swift is compiled. One can expect better performance.

Since we are discussing targeting Android: Java (and Kotlin) are also compiled ahead of time by the Android Runtime (ART). This has been the case since Android 4.4 (KitKat) when ART became an optional replacement to Dalvik.

? Android apps are also compiled ahead of time right now (whether they are written in java or kotlin).
Don't you still have GC issues for heap allocation/destruction? Swift uses the ARC pattern so no GC lockups or performance issues.

[1] https://developer.apple.com/library/ios/documentation/Swift/...

I have spent a large amount of time optimizing Android apps and GC pauses are generally not the issue (as long as you don't do something stupid like allocating objects in draw calls).

I think that ARC is probably an overall better solution but as far as GC pauses are concerned, we have reached the point where they don't impact performances.

Can you use Xcode for Kotlin? Serious question.
I do not understand people's fascination with SWIFT. It is a great language compared to the atrocity of Objective-C but then no where close to Java.

I think there are lot of iOS programmers in the wild for whom SWIFT is first language and hence the fascination.

How in the world so is Java better?