Kotlin vs Java
final var order = Sandwich()
.type("toastie")
.bread("white")
.fillings(List.of("Cheese", "Ham"))
.dressings(List.of("Pepper", "Worcestershire sauce"))
.side("Crisps")
.makeMeASandwich()
System.out.println(ooSandwich.receipt())
// https://www.kotlindays.com/2019/12/02/days-of-kotlin-a-dsl-for-everyone/index.html
val dslSandwich = sandwich {
with type "toasted"
bread = "baguette"
filling("cheese")
filling("ham")
filling("tomato")
dressings {
+"Basil"
+"Pepper"
}
sideOrders {
side("French Fries")
}
}
println(dslSandwich.receipt())
// https://www.kotlindays.com/2019/12/02/days-of-kotlin-a-dsl-for-everyone/index.html
val result = html {
head {
title { +"HTML encoding with Kotlin" }
}
body {
h1 { +"HTML encoding with Kotlin" }
p {
+"this format can be used as an"
+"alternative markup to HTML"
}
// an element with attributes and text content
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
// mixed content
p {
+"This is some"
b { +"mixed" }
+"text. For more see the"
a(href = "http://jetbrains.com/kotlin") {
+"Kotlin"
}
+"project"
}
p {
+"some text"
ul {
for (i in 1..5)
li { +"${i}*2 = ${i*2}" }
}
}
}
}
final var act = this;
final var layout = new LinearLayout(act);
layout.setOrientation(LinearLayout.VERTICAL);
final var name = new EditText(act);
final var button = new Button(act);
button.setText("Say Hello");
button.setOnClickListener(
() -> {
Toast.makeText(act, "Hello, " + name.text + "!", Toast.LENGTH_SHORT)
.show()
}
layout.addView(name);
layout.addView(button);
verticalLayout {
val name = editText()
button("Say Hello") {
onClick { toast("Hello, ${name.text}!") }
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
verticalLayout {
padding = dip(30)
editText {
hint = "Name"
textSize = 24f
}
editText {
hint = "Password"
textSize = 24f
}
button("Login") {
textSize = 26f
}
}
}
application.install(Routing) {
get("/") {
call.respondText("Hello, World!")
}
get("/bye") {
call.respondText("Good bye, World!")
}
}