This post is a free translation of the Kotlin and Swift article . Mobile Development? by Andrew Cherkashyn
When Google announced that they would now officially use Kotlin for Android development, I, like many other Android developers, sighed with relief. Once again I went to the official Kotlin website to double-check the functionality / syntax and compare it with the latest version of Swift, on which I am writing now, and suddenly I felt it: one era passes and a new one begins, at least in mobile development ...
In Kotlin, as in Swift, there is quite a lot of syntactic sugar, which reduces the volume of the usual routine (syntax comparison here ). But what particularly pleases me is that they both, straight out of the box, support the new programming paradigms. Especially functional programming.
The principles of functional programming, of course, are not something new in development, quite the contrary. But now, when there is official support "out of the box" in the development for iOS and Android - it is worth using them.
When I first started my career in mobile development, everyone wrote cycles like this:
Java:
String[] mixedArray = new String[] { "4", "5", "a", "-2", "Str" }; int results = 0; for (String element : mixedArray) { results += Integer.parseInt(element); }
Now everyone uses a functional approach to do the same thing in one call, and this approach is much better:
Kotlin:
val mixedArray = arrayOf("4", "5", "a", "-2", "Str") val results = mixedArray .filter { obj -> obj.toIntOrNull() != null } .map { x -> x.toInt() } .reduce { acc, x -> acc + x }
Swift:
let mixedArray = ["4", "5", "a", "-2", "Str"] let results = mixedArray .filter({ (obj) -> Bool in return Int(obj) != nil }) .map { (obj) -> Int in return Int(obj)! } .reduce(0, +)
Blocks were introduced to Apple for Objective-C in 2010 ( iOS SDK 4.0 ) in order to improve the lives of developers and conform to anonymous Java classes that can be used as callbacks:
Example block in Objective-C:
void (^newBlock)(void) = ^{ NSLog(@"New block is called"); };
An example of an anonymous class in Java:
(new CallbackClass() { @Override public void call() { Log.i(StaticTag, "Callback is called"); } });
Lambda expressions in Java were introduced in 2014 as part of JDK 8 , but unfortunately they were not available to Android developers, because the Android SDK only supports JDK version 7 (therefore, there are libraries like retrolambda ).
Now both languages ​​fully support this approach: Swift has “closures” (the same as blocks in Objective-C), while Kotlin has support for lambdas, which works in the Android SDK:
An example of a closure in Swift:
{ _ in print("Closure is called!") }
An example of lambda in Kotlin:
{ println("lambda is called!") }
Starting in Xcode 4 , somewhere in 2011, Objective-C provides single-line initialization for arrays and dictionaries:
Example initialization in Swift:
let numbersArray = [2, 4, 1] let dictionary = ["key1": "value1", "key2": "value2"
In JDK , only static initialization is available, but there is no way to initialize the Map to a single line. The Map.of function that allows this was introduced only in JDK 9 .
An example of static initialization in Java:
// Array private static final int[] numbersArray = new int[] {2, 4, 1}; // Map private static final Map<String, String> map; static { map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); }
But now Kotlin can do this:
mapOf<String, String>("key1" to "value1", "key2" to "value2")
Another thing I want to highlight is Range Operators that make your life a lot easier. Now instead of using for loops for simple traversal:
for (int i = 0; i < N; i++) { // Do something }
You can do in Kotlin like this:
for (i in 0..N-1) { // Do something }
Or like this in Swift :
for i in 0..<N { // Do Something }
It is also worth mentioning tuples . They provide some freedom in interacting with other components and help to avoid creating additional classes.
So, looking at all these new "features" and many, many other things that are not mentioned in this article - I would assume that the new era has already begun. Now, all newcomers who start their way into mobile development will have all these functions available right out of the box, and they will be able to reduce the costs of the routine development of business logic and application management. And it is much more important than writing hundreds of lines to make a simple piece of work. Of course, earlier you could just put and configure an additional library, such as PromiseKit, ReactiveCocoa, RxJava, etc. But I believe that the availability of these paradigms and principles will encourage new developers to use them, which will lead us to a bright future. :)
Thanks for attention! I hope you were interested, or at least this post gave you fresh ideas. I tried to write shortly, but if you need more specific examples and / or you have any suggestions / comments - please write in the comments!
Source: https://habr.com/ru/post/329870/
All Articles