Git Product home page Git Product logo

guide-to-kotlin's Introduction

Guide to Kotlin

This tutorial assumes that all you know is Java, but you want to learn Kotlin.

Please note that the resources are in the wiki page.

Additional resources

A few things that are worth watching to know more about Kotlin.

More resources to read

More resources to watch

Style-guide and reference for common mistakes to avoid

https://www.reddit.com/r/androiddev/comments/77sl1c/devs_who_review_kotlin_regularly_what_are_things/dorsk3i/

Table of Content

Major Syntax Differences

Basic Kotlin Features

The Cool Stuff

The Tricky Stuff

Android-Specific Stuff

Stuff that is currently not covered, and should eventually be added in this tutorial

  • Coroutines, coroutines on Android, suspend fun

  • Channels: LinkedListChannel, BroadcastChannel

  • @DslMarker

  • multi-platform things: expect and actual

  • contracts

License

Copyright 2018-2020 Gabor Varadi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

guide-to-kotlin's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

guide-to-kotlin's Issues

Interface with val and vars

Re: https://github.com/Zhuinden/guide-to-kotlin/wiki/1.)-Major-Syntax-Differences#interfaces-with-val-var-and-fun

I would add that you can define a default implementation in the interface

interface MyContract {
  var value: String
  val name: String
  val params: Map<String, String> 
     get() = emptyMap()

  fun doSomething(input1: String, input2: String) {
    // Default Implementation that does nothing
  }
}

Default implementations are great especially when you add an optional feature to an interface later. You don't break the existing implementations if you have a default implementaiton.

And that using properties instead of methods is such a great idea because you can use all the powerful encapsulation features that come with properties.

Like defining the overriding properties in the constructor

class SimpleContract(override var value: String, override val name: String) 
  : MyContract 

Kotlin properties and encapsulation

Hello @Zhuinden
I have finished reviewing your guide.
Nice job, this will be useful to lots of people! ๐Ÿ‘

There is one big thing I rewrote that you probably will want to review: Kotlin properties.
They look simple so people don't pay attention, but I find them one of the most useful and under-hyped aspects of Kotlin.

https://github.com/Zhuinden/guide-to-kotlin/wiki/4.)-The-Tricky-Stuff#kotlin-properties-and-encapsulation
(Not sure if they should stay in the "tricky stuff" page!)

Another concern I have is that by creating, moving or renaming some paragraphs, I have probably broken some of the links in the README.

Visibility modifiers: package visibility does exist

In the visibility section, it says:

There is NO package visibility in Kotlin

This is actually not true, as explained in the official guide, in the section Visibility modifiers - package.

I was also confused by the explanation about internal. In the wiki it says that internal is not equivalent to package, and it is true, but I'd add that internal is "module" visibility (where modules are IntelliJ modules, Maven projects, gradle source sets, etc).

data class and objects

I now have writing rights but I prefer to open an issue about this.

Re: https://github.com/Zhuinden/guide-to-kotlin/wiki/2.)-Basic-Kotlin-Features#data-classes

For me data class with zero parameters is simply an object.

object gives you the right toString(), equals() and hashCode().

This happens most frequently in the context of a sealed class

sealed class Cmd

data class Reset(val value: Int) : Cmd()

// ERROR: Data Class must have at least one primary constructor parameter
data class Increment : Cmd()

// BAD: Decrement() != Decrement()
class Decrement : Cmd()

The correct thing to do here is to use an object and JetBrains actually has a quick-fix for it:

sealed class Cmd
data class Reset(val value: Int) : Cmd()
object Increment : Cmd()
object Decrement : Cmd()

And JetBrains actually made a quick-fix for that!

image

setValue() in MyContract interface is not a setter

public interface MyContract {
    String getValue();

    // this is not a setter
    String setValue();

    String getName();

    void doSomething(String input1, String input2);
}
// corrected setter
void setValue(String value); 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.