Anton Rutkevich
JunoLab
// Constant
val a: Int = 1
// Variable
var b: Int = 2
b++
// Nullable variable
var c: String? = null
c?.charAt(0) // returns Char?
fun cases(obj: Any) {
when (obj) {
1 -> print("One")
"Hello" -> print("Greeting")
is Long -> print("Long")
!is String -> print("Not a string")
else -> print("Unknown")
}
}
fun textColorRes(): Int {
return if (isValid()) {
R.color.green
} else {
R.color.red
}
}
class Cat
class Dog(val name: String)
class Mouse(name: String) {
init {
logger.info("Initialized with ${name}")
}
}
class MyView : View {
constructor(ctx: Context) : super(ctx) {
}
constructor(ctx: Context, attrs: AttributeSet)
: super(ctx, attrs) {
}
}
class Address {
var city: String = // ...
var street: String
get() = {
// ...
}
set(value) {
// ...
}
}
open class Base {
open fun canBeOverridden() {}
fun notOverrideable() {}
}
class Derived() : Base() {
override fun canBeOverridden() {}
}
class Outer {
val bar = 1
class Nested {
// has no access to 'bar'
}
}
class Outer {
var bar = 1
inner class Inner {
// can access 'bar'
}
}
class MyActivity: Activity() {
var loginButton: Button by Delegates.notNull()
val userNameLabel: TextView by Delegates.lazy {
findViewById(R.id.user_name_label) as TextView
}
var userName: String by Delegates.observable("") {
metadata, old, new ->
userNameLabel.setText(new)
}
}
fun Context.shortToast(text: String) {
Toast.makeText(this, text, Toast.LENGTH_SHORT)
.show()
}
class MainActivity: Activity() {
override fun onStart() {
super.onStart()
shortToast("On Start")
}
}
class MainActivity: Activity(), SomeCallbackListener {
var loginButton: Button = ...
override fun onCreate(savedInstanceState: Bundle) {
loginButton.setOnClickListener {
// do something
}
}
override fun onJobDone(result: String) {
runOnUiThread {
// Modify UI on main thread
}
}
}
val list: List<String> = listOf("1", "2", "3")
list.add("4") // No such method :)
val mutableList: MutableList<String>
= listOf("4", "5", "6")
mutableList.add("7") // OK
val list = listOf("one", "two", "three")
list.filter {
it.length() < 5
}.map {
it.reverse()
}.forEach {
Log.d(TAG, "String: ${it}")
}
and others
* regular: no APT, reflection,
other "black magic"
// non-null
val list = ArrayList<String>()
list.add(null) // allowed
// allowed, will throw an exception
val item = list.get(0).substring(1)
// non-null
val list = ArrayList<String>()
list.add(null) // allowed
val list: MutableList<String> = ArrayList()
list.add("Item") // null is rejected at compile time
// allowed, will throw an exception
// will never throw NPE
val item = list.get(0).substring(1)
Library | Size | Method count |
---|---|---|
scala-library-2.11.7 | 5.7 Mb | 50811 |
groovy-2.4.4 | 4.6 Mb | 28768 |
kotlin-runtime-0.13.1513 | 294 Kb | 1067 |
kotlin-stdlib-0.13.1513 | 878 Kb | 7672 |
Avg. lines / file | 63.5 |
Avg. SLOC / file | 50 |
Largest file, lines | 360 |
@AntonRutkevich
+AntonRutkevich
anton.rutkevich