Skip to content

Instantly share code, notes, and snippets.

View shruti-basil-hs's full-sized avatar

shruti-basil-hs

View GitHub Profile
@shruti-basil-hs
shruti-basil-hs / DataClassCar.java
Last active December 22, 2017 18:12
Car DataClass
public class Car {
private String make;
private String model;
private String registration;
public String getmake() {
return make;
}
@shruti-basil-hs
shruti-basil-hs / ^
Last active December 20, 2017 01:00
Nullability
//**Attempting to assign null to a regular variable**
var hello: String = "hello"
hello = null // compilation error
//**Null safety**
//By checking for Null
val b = "kotlin"
//* let
fun <T, R> T.let(f: (T) -> R): R = f(this)
//Scoping
Person.getName().let { name ->
print(name)
}
// name is no longer visible here
//To check for null values
data class Car(val make: String, val model: String, val registration: String)
static class Person {
   String name;
}
public void printName(Person person) {
   if (person.name != null) {
       foo(person.name.length());
   }
}
class Person(var name: String?) // the name property can be null
class Person(var name: String) // the name property cannot be null
fun <T, R> T.let(f: (T) -> R): R = f(this)
Person.getName().let { name ->
print(name)
}
// name is no longer visible here
fun <T> T.apply(f: T.() -> Unit): T { f(); return this }
Person(name).apply { foo() };
//Equivalent 4 line Java code
Person getFoo(String name) {
Person person = new Person(name);
person.foo();
return person;
}