Skip to content

Instantly share code, notes, and snippets.

@javadude
Created May 23, 2018 17:08
Show Gist options
  • Save javadude/09ab16068a6be7f20d67b06f959ff429 to your computer and use it in GitHub Desktop.
Save javadude/09ab16068a6be7f20d67b06f959ff429 to your computer and use it in GitHub Desktop.
package funcs
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
class Person {
internal var name3 : String? = null
private var name2 : String? = null
var name : String? = null
var age : Int = 0
var address : Address? = null
val father by lazy {
Person().apply {
name = "Oliver"
age = 78
}
}
inner class Nickname {
var name : String? = null
fun foo() {
[email protected] = "Scott"
}
}
}
class Address {
var street : String? = null
var city : String? = null
var state : String? = null
}
fun doSomething(person : Person, f : Person.() -> Unit) {
person.f()
}
fun doSomething2(person : Person, f : (Person) -> Unit) {
f(person)
}
fun Person.introduceSelf() {
println(name)
println(name3)
// println(name2)
}
fun main(args: Array<String>) {
val person = Person()
person.introduceSelf()
doSomething(person) {
println(name)
}
doSomething2(person) {
println(it.name)
}
doSomething2(person) {person2 ->
println(person2.name)
}
with(person) {
name = "Scott"
age = 51
address = Address()
with(address) {
this!!.street = "123 Sesame"
city = "Ann Arbor"
state = "MI"
}
with(address!!) {
street = "123 Sesame"
city = "Ann Arbor"
state = "MI"
}
}
// receiver = this, return this
person.apply {
name = "Scott"
age = 51
address = Address().apply {
street = "123 Sesame"
city = "Ann Arbor"
state = "MI"
}
}
val person2 : Person? = Person()
// if (person2?.name != null) {
// foo(person2?.name)
// } else {
// fee()
// }
val name = person2?.name
if (name != null) {
foo(name)
} else {
fee()
}
person2?.name?.let {
foo(it)
}
person2?.name?.let {n ->
foo(n)
}
person2?.name?.let {n ->
foo(n)
} ?: fee()
val x = person2?.name?.run {
foo(this)
} ?: fee()
val y = person2?.name?.run {
foo(this)
} ?: fee2()
y
val z = if (person2 != null) {
b()
} else {
c()
}
z
val foo = person2?.also {
b()
}.also {
c()
}
foo
val f1 : (Person) -> Unit = {
b()
}
val f2 : (Person) -> Unit = {
c()
}
val propMap = person2?.let {
mapOf(
"name" to it.name to "",
"age" to it.age
)
}
val xxx = "a" to "b" to "c"
val yyy = ("a".to("b")).to("c")
person2?.let {
it.introduceSelf()
}
person2?.introduceSelf()
person2?.also(f1)?.also(f2)
f1(person2!!)
f2(person2)
// val aa = "a" to "a"
// when (aa) {
// aa.first == "a" && aa.second == "a" ->
// ActionListener {} as ((ActionEvent) -> Unit)
//
// is String -> {}
// else -> {}
// }
val x4 = ""
+ 5
// val x5 = "" +
// ""
// person
// .name
//
// println(x4)
println(+ 5)
}
fun Int.plus(other : Int) {
(this + other) % 10
}
open class AAA
class BBB : AAA()
class CCC : AAA()
fun b() = BBB()
fun c() = CCC()
fun foo(name : String) : String {
return "A"
}
fun fee() : String {
return "B"
}
fun fee2() : Int {
return 42
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment