Skip to content

Instantly share code, notes, and snippets.

View gaplo917's full-sized avatar
🎯
Focusing on Kotlin

Gary Lo gaplo917

🎯
Focusing on Kotlin
View GitHub Profile
@gaplo917
gaplo917 / mac-install.sh
Last active June 20, 2016 03:42
Mac-install-script
#!/bin/sh
# install shit on mac osx
# author: mcor
# date: 2015-12-17
# install brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# install cask
brew tap caskroom/cask
// https://blog.gaplotech.com/content/images/2016/06/Screen-Shot-2016-06-22-at-22-09-29.png
// Result not consistent : 12291 false, 37709 true
for(var i=0; i<50000; ++i) {
console.log(typeof null === 'undefined')
}
// OR
for(var i=0; i<50000; ++i) {
console.log(typeof null == 'undefined')
}
@gaplo917
gaplo917 / TasksLocalDataSource.kt
Last active August 1, 2020 08:08
Android Singleton Kotlin
//http://stackoverflow.com/questions/40398072/singleton-with-parameter-in-kotlin
class TasksLocalDataSource private constructor(context: Context) : TasksDataSource {
private val mDbHelper: TasksDbHelper
init {
// You cannot pass null in kotlin unless you are using `context: Context?`
// therefore, checking null is useless
// checkNotNull(context)
@gaplo917
gaplo917 / NullRisk.java
Last active December 7, 2016 15:38
Java Null Risk Example
// Java null risk workaround example
// Kotlin null safe example https://gist.github.com/gaplo917/77d7ee9f48ed20f4a60bc20a1b39a2e6
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import static java.lang.System.*;
class NullRisk {
@gaplo917
gaplo917 / NullSafe.kt
Last active December 7, 2016 15:36
Kotlin Null Safe Example
// Kotlin null safe example
// Java null risk example https://gist.github.com/gaplo917/5dc037bedea39a348854ae59ba89c063
fun doSth(str: String): String {
return str.toLowerCase()
}
fun doSthOptional(str: String?): String? {
// Optional chainning, similiar to Swift
return str?.toLowerCase()
@gaplo917
gaplo917 / Verbose.java
Last active December 7, 2016 15:45
Java has no Type Inference that makes code become verbose
// Java 9
final Map<String,Integer> abc = Map.of("a",1, "b", 2, "c", 3);
// Java >= 5
final Map<String,Integer> abc = new HashMap<String, Integer>() {{
put("a",1);
put("b",2);
put("c",3);
}};
@gaplo917
gaplo917 / TypeInference.kt
Last active April 12, 2017 16:11
Type Inference example in Kotlin
// val abc: Map<String,Int> = mapOf("a" to 1, "b" to 2, "c" to 3)
// the actual type of the Map is obvious
// thus we can skip Map<String,Int> and let kotlin compiler to make type inference
val abc = mapOf("a" to 1, "b" to 2, "c" to 3)
val abcd = abc + ("d" to 4) // ok!
val abce = abc + ("e" to "5") // compile error: Type mismatch
// val c: Int? = abc["c"]
var c = abc["c"]
@gaplo917
gaplo917 / WithoutNamedDefaultArgument.java
Last active December 7, 2016 15:50
Java without Named and Default arguments makes code verbose and increase the chance of human error
// Java
// Ugly work around to save time (compared to method overload)
void doSomething(
@NotNull String fname,
@NotNull String lname,
@Nullable String addr,
@Nullable Gender gender
) {
requireNonNull(fname);
requireNonNull(lname);
@gaplo917
gaplo917 / NamedDefaultArgument.kt
Last active December 7, 2016 15:57
Kotlin with Named and Default Argument help to write clean code
// Kotlin
// Default argument
fun doSomething(
fname: String,
lname: String,
addr: String = "N/A",
gender: Gender = Gender.Unknown
){
// do something that really need fname,lname, addr, gender..
}
@gaplo917
gaplo917 / UtilsPattern.java
Last active December 7, 2016 16:10
Java Utils Pattern is not Human friendly or IDE friendly
// Java
// please focus on the function name & declaration
// As we all know, String is a final class and we can't extend
class StringUtils{
public static TypeA toTypeA(String str)
}
// Assume TypeA is from third party library that you can't extend
class TypeAUtils {