Skip to content

Instantly share code, notes, and snippets.

@Tanapruk
Last active September 7, 2016 06:07
Show Gist options
  • Save Tanapruk/a5505e498077f3107271b2b44d766729 to your computer and use it in GitHub Desktop.
Save Tanapruk/a5505e498077f3107271b2b44d766729 to your computer and use it in GitHub Desktop.
Kotliner

###Extension Functions

####Kotlin

fun CharSequence.isEmail() = this.matches("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+".toRegex())


val stringImpl : String = "foo@email.com"
val isThisStringEmail = stringImpl.isEmail() //return true

####Java

public class TextValidator {
    public static boolean isEmail(String string) {
        return string.matches("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+".toRegex());
    }

}
String stringImpl  = "foo@email.com";
boolean isEmail = TextValidator.isEmail(stringImpl); // return true

###Kotlin file content

####Kotlin

/* Alphabet.kt */
class A {
  //class name won't need to match file name
}

class B {
 //another class inside Alphabet.kt file
}

//top-level declaration (declarations without classes)
fun CharSequence.isEmail() = this.matches("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+".toRegex())
/* Alphabet.java */

class Alphabet {
    //class name must be the same as filename
    //Only one class perfile
    //No top-level declarations
}

###Safe Calls

####Java

Animal animal;
if (animal != null) {
    animal.bark();
}

####Kotlin

var animal : Animal? = null
animal?.bark()

###Type Inference

//variables
val word = "this is a word with variable inferred"
val word02: String = "this is also a word with an explicit type"

//methods
fun getWord() = "this is a word return from a method. One liner with an inferred type"
fun getWord(): String = "this is another word from a method with an explicit type"
fun getWord(): String {
    return "this is a word from a method with a block with an explicit type"
}

###Singleton

####Kotlin

object EmployeeManager {
  fun registerStaff(staffName: Staff) {
  }
}

EmployeeManager.registerStaff(...)

####Java

public class EmployeeManager {

private static EmployeeManager instance;

private EmployeeManager() {
}

public static getInstance() {
    if(instance == null) {
    instance = new EmployeeManager() 
}

public void registerStaff(Staff staff) {
///
}

EmployeeManager.getInstance().registerStaff(...);

###with

####Java

EditText editText = (EditText) findViewById(R.id.xxx);
editText.setText(...);
editText.setTextColor(...);

####Kotlin

with(editText) {
    setText(...)
    setTextColor(...)
}

###let

####Java

if(fragment != null) {
    fragment.doSomething();
}

####Kotlin

fragment?.let {
    fragment.doSomething()
}

###Destructuring Declarations

####Java

String id = student.getId();
String name = student.getName();
String major = student.getMajor();

####Kotlin

val (id, name, major) = student

/* need additional setup inside Student class */


Student(val id: String?, val name: String?, val major: String?) {
    operator fun component1() = id
    operator fun component2() = name
    operator fun component3() = major
}

###String Templates

####Java

String aString = "This is a book";
Log.d(TAG, "aString is: " +  aString + " and has length of " + aString.length());

####Kotlin

val aString = "This is a book"
Log.d(TAG,"a String is: $aString and has a length of ${aString.length}")

###Smart Casts

####Java

if(fragment instanceof RegisterFragment) {
    RegisterFragment registerFragment = (RegisterFragment) fragment;
    registerFragment.clearInputFields();
}

####Kotlin

if(fragment is RegisterFragment) {
    fragment.clearInputFields()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment