• Skip to primary navigation
  • Skip to main content
  • Skip to footer
Scinonova logo
  • Start
  • Om oss
  • Karriär
  • Blogg
  • Kontakt
  • Svenska

Programmering

Introduction to Kotlin

29 oktober, 2018 by Scionova

In this blog post I will give you a small introduction to the programming language Kotlin (which last year was added as a supported language for Android) and describe a few of my favorite features of the language.

Background

Last year at Google I/O, it was announced that Kotlin would be added as supported language in Android. A lot of tutorials, introductions and examples has emerged from the community since, and it is clear from the Android Developer page for Kotlin that Google themselves want developers to see the advantages of Kotlin with a lot of helpful documentation and examples.

Android is growing rapidly in the automotive industry, among others Volvo and Audi has decided to use the Android operating system in their infotainment systems. This gives for a perfect reason to get some more knowledge about this rising star of programming languages!

In Android, Kotlin is compiled to JVM byte code so it is possible to call Java code from Kotlin and vice versa, which makes it easy to have a gradual transition from Java to Kotlin in an application. Kotlin is supported from Android Studio 3.0, which even introduced a feature to convert a file containing Java code to Kotlin. In my experience this function doesn’t work optimally but it gives you a head start on your way to update your application to Kotlin.

Nullability

In Kotlin, the “billion dollar mistake” has been solved by forcing the programmer to express nullability explicitly in the type of a variable. By appending a question mark to the type, we let the compiler (and other programmers) know that the variable is nullable.

When we try to assign null to a variable of non-nullable type:

After appending the question mark, it is possible for the variable to hold null:

Safe call operator
In order to call a member function on a variable of nullable type, the safe call operator (?.) is one of the possible ways. If we try to call a member function the usual way, we get an error:

Though if we use the safe call operator, we get a different result:

As you can see, the safe call operator returns null if the variable it is applied to holds a null reference. The returned value will also be a nullable, in this case an Int?.

Elvis operator
Another handy feature of Kotlin is the so-called Elvis operator (?:). It returns the value to the left of the operator if it is not null, and the value to the right if it is.

Continued reading about Kotlin null safety
For more information regarding null safety in Kotlin, check out the official documentation on the subject.

Mutability of variables

As you might have noticed in the examples above, a variable can be declared with the keyword var.

This creates a variable that is mutable, e.g. it is possible to alter its value at a later point. It is also possible to make a variable immutable, this is done using the keyword val when declaring the variable instead:

Extension functions

In Kotlin, it is possible to extend classes with your own functions without creating subclasses. This concept is called extension functions and they are declared like this:

In this example the standard class Int is extended with a function square that can be called on integers in all files the declaration of the extension is included in. See the official documentation for more examples.

Data classes

What in Java is called a POJO, has its Kotlin counterpart in data classes. A data class contains a lot of extra functionality which would be boilerplate in a POJO, e.g. equals, hashCode and toString. This allows you to create a class with just a one-liner:

It is possible to declare a variable with either var or val in a class, with the difference that a var makes the compiler to generate a setter function for the variable in the class beside the getter that is generated for public variables.

Finishing words

The null safety of Kotlin is a big selling point of Kotlin and something that often is pointed out as an advantage of using Kotlin over Java when developing for Android (beside for verbosity). In this post I have scarcely scratched the surface of Kotlin and if you are interested in learning more I would recommend you to continue your reading on the official webpage of the language, but also on the Android Developer page for Kotlin.

 

// Johannes Jansson, Software Developer

6 Useful New Features in C++17 – Part 5

13 september, 2018 by Scionova

Constexpr lambdas

Since C++11 we are able to use the constexpr specifier which evaluates the expression at compile time, with C++17 lambdas can also be constant expressions. One use case could be to initialize constexpr variables that depend on other constexpr variables. See the following example:

This example maybe is silly but imagine a much more complex dependency and the array consists of more complex elements than simple integers.

Compiler Support

These features are all fully supported by GCC, MSVC, and clang. Below follows a table showing which version enables support for each feature respectively.

For more a detailed matrix with more compilers and all features of C++17 listed, follow this link.

 

Endnote

This was the last part of the series “6 Useful New Features in C++17” and I hope you found it helpful and interesting. All the previous blog posts in this series you will find under “NEWS“.

Don’t hesitate to share with your friends and colleagues and stay tuned for more!

 

 //Patrik Ingmarsson, Software Developer

6 Useful New Features in C++17 – Part 4

11 september, 2018 by Scionova

This is the fourth part of the series “6 Useful New Features in C++17”, the first part can be found here.

If Statements with constexpr Condition

This is a feature that perhaps has its main use while working with templated classes and such, but there are other uses as well, e.g., together with variadic arguments. constexpr adds the ability to discard branches at compile time. You might argue that a compiler optimizes code to achieve the same behavior anyway, and of course, you are correct. However, using constexpr if guarantees that it is evaluated at compile time, and at the very least the code compiles faster.

I would like to show an example when one makes use of SFINAE, a situation where I think constexpr if is useful. What follows is a non-sensical code snippet using SFINAE and C++14 revision.

With the use of constexpr if in C++17 it can be written with only one function, like so:

I know which version I would like to work with.

Compiler Support

These features are all fully supported by GCC, MSVC, and clang. Below follows a table showing which version enables support for each feature respectively.

 

For more a detailed matrix with more compilers and all features of C++17 listed, follow this link.

 

READ PART 5 “6 Useful New Features in C++17” HERE.

 

// Patrik Ingmarsson

6 Useful New Features in C++17 – Part 3

6 september, 2018 by Scionova

This is the third part of the series “6 Useful New Features in C++17”, the first part can be found here.

Init statements in if statements

Like for statements, if and switch statements get an initializer part. Convenient when handling shared resources between threads. As an example, if there are a lot of workers that share a single job queue but the jobs themselves are independent, then we only want to block while popping the queue and not while executing the job. One could solve this with the following:

The extra scope makes sure that we release the guard after we successfully grabbed a job to process. In my opinion it looks a bit messy and we’ve added extra parentheses to keep track of. If we instead make use of the init statement it could be rewritten as follows:

At least I think this is easier to follow and less error prone while refactoring.

Compiler Support

These features are all fully supported by GCC, MSVC, and clang. Below follows a table showing which version enables support for each feature respectively.

For more a detailed matrix with more compilers and all features of C++17 listed, follow this link.

 

READ PART 4 “6 Useful New Features in C++17” HERE.

 

// Patrik Ingmarsson

6 Useful New Features in C++17 – Part 2

3 september, 2018 by Scionova

This is the second part of the series “6 Useful New Features in C++17”, the first part can be found here.

 

Structured Binding

One thing that has been bugging me when trying to write nice looking code is iterating through maps. Most often it is when acting on information read from a JSON or XML file, where if a key is found then something should be done with the value.

For example, an application has a set of settings with default values, but a user can set their own values for these settings. However, the file specifying these user specific values only needs to contain those settings that deviate from the default values. A code snippet doing that could look like:

With Structured Binding supported by C++17 this can be rewritten in a prettier and more readable way:

One other example where structured binding helps you clean up your code would be when one needs to handle tuples.

Doesn’t look very intuitive even in this very simple example. What we could do before C++17 is make use of std::tiewhich unpacks the tuple into independent variables.

Well, I like this better. However, it is a bit cluttered because we need to declare the variables which is used in std::tie. Fortunately, structured binding lets us get rid of that as well:

Please note that structured binding can be used with other types than tuples!

Compiler Support

These features are all fully supported by GCC, MSVC, and clang. Below follows a table showing which version enables support for each feature respectively.

For more a detailed matrix with more compilers and all features of C++17 listed, follow this link.

READ PART 3 “6 Useful New Features in C++17” HERE.

// Patrik Ingmarsson

6 Useful New Features in C++17 – Part 1

29 augusti, 2018 by Scionova

In this 5 parts of blog series, Patrik from Scionova will talk about useful features in C++17, that can help you write better codes. Every Monday and Thursday new posts in this series will come out.

Every now and again, I have some time to spare in which I try to brush up or gain some new knowledge in different areas. In the previous project I was working in, C++14 was used as the latest approved revision of the C++ standard. But the C++17 is (almost entirely) supported by commonly used compilers and can be used in new projects. Therefore, I was thinking that this time it might be a good idea to familiarise myself with C++17 revision.

And it was good stuff that I was finding, so why not share these findings with my colleagues, and why not everyone? So that is what I did,  and the following is a small set of features of the C++17 revision that can help you write better and more maintainable code. Keep in mind though that there are a lot more features in this revision, here is a compilation of all changes between C++14 and C++17.

Short hand for nested namespace declaration

Large projects I’ve been working in have had a lot of nested namespaces, in the format like company::area::component. This pattern is also found in Android Open Source Project, e.g., android::hardware::wifi::V1_0::IWifi. C++17 supports a short hand in writing these Nested namespace on one single, neat looking line:

What’s more is that one can attach attributes to entire namespaces as well. One convenient use case could be to deprecate an entire namespace in an API.

Compiler Support

These features are all fully supported by GCC, MSVC, and clang. Below follows a table showing which version enables support for each feature respectively.

For more a detailed matrix with more compilers and all features of C++17 listed, follow this link

READ PART 2 “6 Useful New Features in C++17” HERE.

 

// Patrik Ingmarsson

  • « Go to Previous Page
  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4
  • Go to Next Page »

Footer

Göteborgskontoret


Theres Svenssons Gata 13,
417 55 Göteborg

Varbergskontoret


Kungsgatan 28b,
432 44 Varberg

Gasell