Skip to content

Instantly share code, notes, and snippets.

View cassiozen's full-sized avatar
:atom:

Cassio Zen cassiozen

:atom:
View GitHub Profile
var nullableGreeting: String? = "Hello, World"
nullableGreeting = null // Works
var greeting: String = "Hello, World"
greeting = null // Compilation Error
suspend fun getStatus(): List<String> {
val currentUserDeferred = someApi.fetchUser()
val currentCompanyDeferred = someApi.fetchCompany()
return listOf(currentUserDeferred.await(), currentCompanyDeferred.await())
}
async function getStatus() {
const currentUserPromise = someApi.fetchUser();
const currentCompanyPromise = someApi.fetchCompany();
return await Promise.all([currentUserPromise, currentCompanyPromise]);
}
data class Movie(
val name: String,
val rating: Int,
val director: String
)
val movie1 = Movie("Back to the Future", 5, "Bob Zemeckis")
val movie1 = Movie("Star Wars: Episode IV - A New Hope", 5, "George Lucas")
const movie1 = {
name: "Back to the Future",
rating: 5,
director: "Bob Zemeckis"
}
const movie2 = {
name: "Star Wars: Episode IV - A New Hope",
rating: 5,
director: "George Lucas"
}
class Monster(val name: String, val color: String, val numEyes: Int) {
fun speak(likes: String):String {
return "My name is $name and I like $likes"
}
}
var nhama = Monster("Nhama", "red", 1)
// Kotlin doesn't have a `new` keyword - you instantiate a class by calling it directly
nhama.speak("guacamole")
// "My name is Nhama and I like guacamole"
class Monster {
constructor(name, color, numEyes) {
this.name = name;
this.color = color;
this.numEyes = numEyes;
}
speak(likes) {
return `My name is ${this.name} and I like ${likes}`;
}
}
fun weatherReport(location) {
// Make an Ajax request to fetch the weather...
return Pair(72, "Mostly Sunny") // Pair is a standard class in Kotlin that represents a generic pair of two values
}
val (temp, forecast) = weatherReport("Berkeley, CA")
function weatherReport(location) {
// Make an Ajax request to fetch the weather...
return [72, "Mostly Sunny"];
}
const [temp, forecast] = weatherReport("Berkeley, CA");