What’s new Swift 4.2 and Kotlin 1.3 bring to Mobile Development

There are only two kinds of programing languages: the ones people complain about and the ones nobody uses. That`s how Bjarne Stroustrup describe the whole programming world. And every day we are waiting for the updates that will help to refute this quote. Did it happen in the new versions of Swift and Kotlin? We can try to figure it out.
So let`s talk about Swift and Kotlin separately.

SWIFT 4.2

 

 

Recently, along with the XCODE Beta 10.0 update, a new version of the Swift 4.2 became available. The second update of the fourth Swift version brought with it quite a few useful features that are designed to make life easier for developers, and in this article we will focus on the most significant innovations.

1. Derived collections of enum cases

We start with the derived collection of enum classes, which became possible thanks to the SE-0194. This ticket introduces a new CaseIterable protocol that automatically generates an array for allCases in the enum. All you need to do is to register all your preferences for this new protocol. During compilation, Swift automatically generates allCases properties, which are an array of all the options for your enum in the order they are declared.

2. Dynamic member look up

One of the most interesting innovations were implemented thanks to SE-0195. This proposal allows you to perform a dynamic search of elements by key, just as it is implemented in scripting languages such as PHP or Python, but you don`t lose any of Swift’s safety. At the core of the innovation is @dynamicMemberLookup attribute that tells Swift to call the subscript method, subscript(dynamicMember:), when accessing properties. This subscript(dynamicMember:), which is mandatory for @dynamicMemberLookup attribute, allows you to pass a string-name of the property that was requested and return the value that you need.

You can assign @dynamicMemberLookup to protocols, structures, enums and classes, and even to all custom data types. In practice, this means two things:
First, you can create a class using a @dynamicMemberLookup and all classes that inherit from it will also automatically be a @dynamicMemberLookup.

Second, you can force other types to use the @dynamicMemberLookup by defining it in the protocol and adding the default implementation to the subscript(dynamicMember:) using the protocol extension and then running the other types according to your protocol.

3. Random number generation

New version of Swift introduced a new API for working with random numbers and values. This means that you can refuse to use arc4random_uniform() and use a cryptographic secure randomizer that is written directly to the Swift instead it. You can generate random numbers by calling the Random() method that works with all numeric types, indicating the range you want to work with. This method can be called for Int, CGFloat, Float, Double.

4. Shuffling

You also have the option of shuffling the elements of an array using new methods – shuffle() and shuffled() depending on whether you want to shuffle the elements for the current array or create a new array with the shuffled elements. So, shuffle() is mixing the elements of the current array, and the shuffled() method is used to create a new array with mixed values.

5. Hashing in Swift

Swift 4.2 also implemented ticket SE-0206, which simplifies the creation of user types corresponding to the Hashable protocol and introduces a new Hasher struct that represents a randomly filled universal hash function to make the process easier.

6. Boolean toggling

Another small but no less significant update is the ability to change the value of a boolean type using the new toggle() method, which switches the value between True and False. All the code for implementing this ticket takes only a few lines compact and readable.

Apple describes Swift 4.2 as a “waypoint for the stability of the Application Binary Interface in Swift 5”. Now there are many improvements in the language relating to new functionality, rethinking of the previous functionality, changes in ABI.

KOTLIN 1.3


The next version of Kotlin (1.3) is expected a lot of changes. We will talk only about some of them.

1. Coroutings

The long-awaited release of Korutin in Kotlin version 1.3 is planned, as the result – they will move from the kotlin.coroutines.experimental package to kotlin.coroutines.
Later it is planned to release the support library to support the existing API korutin.

2. Capturing when subject in a variable

This small addition makes the response variable properly scoped within when, and makes the logic flow in a more expression-like fashion. This was one of the most popular feature requests in our issue tracker.

3. Inline classes

Inline classes allow wrapping a value of some type without creating an actual wrapper object. When such a class is used, the compiler inlines its content and operations are performed on the wrapped value itself.
This feature can be enabled with the compiler option -XXLanguage:+InlineClasses.

4. Unsigned arithmetic

One of the possible reasons of creating inline classes are unsigned types. In the standard library, for each primitive, the corresponding unsigned variants will be described, in which work with arithmetic operators will be implemented, as well as basic methods, such as toString ().
Literal support and implicit conversion will be also added.

5. Companion object for Boolean type

Basic types in Kotlin such as Int, Char, and String have a companion with some properties like MIN_VALUE. Until recently, the Boolean type stood aside having no companion object.
Existing companion objects of basic types have received these new properties:
– Byte, Short, Int, Long, Char now have SIZE_BITS and SIZE_BYTES constants, telling how many bits or bytes a value of that type takes in binary form.
– Char now has MIN_VALUE and MAX_VALUE constants equal to ‘\u0000’ and ‘\uFFFF’, respectively.

The complete list of changes in this release can be found in the changelog.

Programing language developers understand the difficulties and problems and work with them; as a result, new versions will make Swift and Kotlin even more convenient to use nowadays.

Leave a Reply

Your email address will not be published. Required fields are marked *