-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
595 lines (511 loc) · 23.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<!DOCTYPE html><html><head><title>Kotlin is not like Java</title><meta charset="utf-8"><link rel="stylesheet" href="css/style.css"><link class="codestyle" rel="stylesheet" href="css/androidstudio.css"><script src="js/highlight.pack.js"></script><script src="js/kotlin.js"></script><script src="js/java.js"></script><script>hljs.configure({languages: []});hljs.initHighlightingOnLoad();</script></head><body><a target="_blank" href="https://github.com/kotlin-cloud/kotlin-is-not-like-java"><img id="fork-me" src="http://luckymerlin.me/images/fork-me.png"></a><div id="note">Kotlin is NOT Like Java</div><div class="section"><div class="title">BASIC SYNTAX</div><div class="case"><div class="name">Properties</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">public class MyKotlinClass {
val a: Int = 1
var b: Int = 1
val c = 1
var d = 1
private set
}
fun example() {
val myClass = MyKotlinClass()
myClass.b = 2
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">public class MyJavaClass {
private final int a = 1;
private int b = 1;
private final int c = 1;
private int d = 1;
public int getA() { return a; }
public int getB() { return b; }
public void setB(int b) { this.b = b; }
public int getC() { return c; }
public int getD() { return d; }
private void setD(int d) { this.d = d; }
}
class ExampleClass {
public static void example() {
MyJavaClass myClass = new MyJavaClass()
myClass.setB(2)
}
}</code></pre></div></div></div><div class="case"><div class="name">String Templates</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">class MyKotlinClass {
val name = "Omar"
val surname = "Miatello"
val example = "My name is $name $surname"
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">class MyJavaClass {
final String getName() {
return "Omar";
}
final String getSurname() {
return "Miatello";
}
final String getExample() {
return String.format("My name is %s %s",
getName(), getSurname()); }
}</code></pre></div></div></div><div class="case"><div class="name">Get and Set</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">fun renderItem(item: SomeItem) {
visitCount.text = "${item.visitCount}次拜访"
phone1.visibility =
if(item.cellPhone.isNullOrEmpty())
View.GONE else View.VISIBLE
shopName.text = item.name ?: "暂无商户名称"
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">void renderItem() {
visitCount.setText(String.format(...), ...)
if(item.cellPhone == null ||
item.cellPhone.isEmpty()) {
phone1.setVisibility(View.GONE)
} else {
phone1.setVisibility(View.VISIBLE)
}
shopName.text =
item.name != null ? item.name : "..."
}</code></pre></div></div></div><div class="case"><div class="name">Data Class</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">data class User(val name: String, val email: String)</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">public class User {
private String name;
private String email;
User(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}</code></pre></div></div></div></div><div class="section"><div class="title">NULL SAFETY</div><div class="case"><div class="name">Null Safety</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">class MyKotlinClass {
var a: String = "ciao"
var b: String = null // Error at compile time
var c: String? = null
var d: String? = "ok"
fun example(e: String, f: String?) {
e.length()
f.length() // Error at compile time
f?.length()
if (f != null) {
f.length()
}
}
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">static class MyUtils {
void example(String e, String f) {
e.length(); // throw NullPointerException?
f.length(); // throw NullPointerException?
if (e != null) {
e.length();
}
if (f != null) {
e.length();
}
}
}</code></pre></div></div></div></div><div class="section"><div class="title">FUNCTION AND LAMBDA</div><div class="case"><div class="name">Functions</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">val a = 1
val b = 2
val f1 = { a: Int, b: Int -> a + b }
fun f2(a: Int, b: Int): Int {
return a + b
}
fun f2_(a: Int, b: Int): Int = a + b
fun f3(a: Int, b: Int, fn: (Int, Int) -> Int): Int =
if (a > b) fn(a, b) else a - b
class MyKotlinClass {
fun test() {
print(f1(a, b)) // => 3
print(f2(a, b)) // => 3
print(f3(a, b, f1)) // => -1
print(f3(2, 1, ::f2)) // => 3
}
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">public class SomeJavaClass {
static int add(int a, int b) {
return a + b;
}
interface Addable {
int add(int a, int b);
}
static int add2(int a, int b, Addable addable) {
if (a > b) {
return addable.add(a, b);
} else {
return a - b;
}
}
void test() {
System.out.println(add2(2, 1, new Addable() {
@Override
public int add(int a, int b) {
return a + b;
}
}));
}
}</code></pre></div></div></div><div class="case"><div class="name">Lambda</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">fun f3(a: Int, b: Int, fn: (Int, Int) -> Int): Int =
if (a > b) fn(a, b) else a - b
fun test() {
f3(2, 1) { a1, a2 ->
a1 + a2
} // => 3
f3(2, 1, { a1, a2 -> a1 + a2}) // => 3
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">public class SomeJavaClass {
interface Addable {
int add(int a, int b);
}
static int add2(int a, int b, Addable addable) {
if (a > b) {
return addable.add(a, b);
} else {
return a - b;
}
}
void test() {
System.out.println(add2(2, 1, new Addable() {
@Override
public int add(int a, int b) {
return a + b;
}
}));
}
}</code></pre></div></div></div><div class="case"><div class="name">SAM Conversion</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">class MyActivity : Activity() {
fun example() {
val view = findViewById(R.id.button)
view.setOnClickListener {
Log.d("TAG", "Item clicked!")
}
}
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">class MyActivity extends Activity {
void example() {
View view = findViewById(R.id.button);
view.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("TAG", "Item clicked!");
}
}
);
}
}</code></pre></div></div></div></div><div class="section"><div class="title">COLLECTIONS</div><div class="case"><div class="name">Using Collections</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">fun example() {
val os = listOf("Android", "iOS", null,
"Windows Phone")
os.filterNotNull().sortedBy { it.length }
.map { it.toUpperCase() }
.forEach { print(it) }
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">class MyUtils {
static void example() {
List<String> os = Arrays.asList("Android",
"iOS", null, "Windows Phone");
List<String> osNotNull = new ArrayList<>();
for (String name : os) {
if (name != null) osNotNull.add(name);
}
Collections.sort(osNotNull, new
Comparator<String>() { @Override
public int compare(String l, String r) {
return l.length() - r.length();
}
});
for (String name : osNotNull) {
String value = name.toUpperCase();
print(value);
}
}</code></pre></div></div></div></div><div class="section"><div class="title">EXTESIONS</div><div class="case"><div class="name">Extension: String</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">fun String.isBig(): Boolean {
return length() > 10
}
fun example() {
"why?!".isBig() // false!
"P90, RUSH B, ...".isBig() // true!
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">// file MyUtils.java
class MyUtils {
static boolean isBig(String str) {
return str.length() > 10;
}
}
// file MyJavaClass.java
class MyJavaClass {
void example() {
MyUtils.isBig("why?!");
MyUtils.isBig("P90, RUSH B, ...");
}
}</code></pre></div></div></div><div class="case"><div class="name">Simple reified Extension</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">inline fun <reified T : Any> Activity.service(): T {
return HttpService.getApiService(T::class.java)
}
fun Activity.toast(str: String) {
Toast.makeText(this, str,Toast.LENGTH_SHORT).show()
}
class MyActivity : Activity() {
val baiduService = service<Api>()
fun test() {
toast("hahaha")
}
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">class MyActivity extends Activity {
//leave out
Api baiduService = HttpService.getApiService(Api.class);
void test() {
ToastUtil.showToast(this, "hahaha");
}
}</code></pre></div></div></div><div class="case"><div class="name">Extensions: Permission</div><div class="pair"><div class="kcard"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">inline fun Array<String>.asPermissions(activity: Activity, fn: () -> Unit) {
checkNotNull(activity)
val PERMISSION_REQUEST_CODE = 11111
var pred = true
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
this.filter { ActivityCompat.checkSelfPermission(activity, it) != PackageManager.PERMISSION_GRANTED }
.forEach { pred = false }
}
if (pred) {
fn()
} else {
ActivityCompat.requestPermissions(activity, this, PERMISSION_REQUEST_CODE)
}
}
fun test(activity: Activity) {
listOf(Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.READ_PHONE_STATE).asPermissions(activity) {
// do things with required permissions
}
}</code></pre></div></div></div><div class="case"><div class="name">Extensions: Make Phone Call</div><div class="pair"><div class="kcard"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">fun Activity.call(phone: String?) {
phone?.let {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
== PackageManager.PERMISSION_GRANTED) {
val callIntent = Intent(Intent.ACTION_DIAL)
callIntent.data = Uri.parse("tel:" + it)
this.startActivity(callIntent)
} else {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.CALL_PHONE),
StaticConfig.RequestCode.REQUEST_PHONE)
toast("请确认是否开启拨打电话权限")
}
}
if (phone == null) {
toast("没有电话!")
}
}</code></pre></div></div></div></div><div class="section"><div class="title">DELEGATES</div><div class="case"><div class="name">Lazy Delegation 1</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">class MyItem
class MyKotlinClass {
val item by lazy { MyItem() }
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">// in Kotlin is synchronized
class MyJavaClass {
class MyItem { }
MyItem item;
final MyItem getItem() {
if (item == null) {
item = new MyItem();
}
return item;
}
}</code></pre></div></div></div><div class="case"><div class="name">Lazy Delegation 2</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">class MyKotlinActivity : Activity() {
val textView by lazy { findViewById(R.id.text) as TextView }
val res by lazy { resources.getString(R.string.cancel) }
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">class MyJavaActivity extends Activity {
TextView textView;
String res;
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
textView = (TextView) findViewById(R.id.text);
res = getResources().getString(R.id.cancel)
}
}</code></pre></div></div></div><div class="case"><div class="name">Butterknife</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">//usage
class MyActivity : Activity() {
val someText by bindView<TextView>(R.id.text)
}
//implementation
private class Lazy<T, V>(private val initializer: (T, KProperty<*>) -> V) : ReadOnlyProperty<T, V> {
private object EMPTY
private var value: Any? = EMPTY
override fun getValue(thisRef: T, property: KProperty<*>): V {
if (value == EMPTY) {
value = initializer(thisRef, property)
}
@Suppress("UNCHECKED_CAST")
return value as V
}
}
public fun <V : View> Activity.bindView(id: Int)
: ReadOnlyProperty<Activity, V> = required(id, viewFinder)
private val Activity.viewFinder: Activity.(Int) -> View?
get() = { findViewById(it) }
private fun <T, V : View> required(id: Int, finder: T.(Int) -> View?)
= Lazy { t: T, desc -> t.finder(id) as V? ?: viewNotFound(id, desc) }</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">//usage
class MyActivity extends Activity {
@Bind(R.id.text)
TextView someText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ButterKnife.bind(this);
}
}
//implementation:
//https://github.com/JakeWharton/butterknife
//tons of code</code></pre></div></div></div><div class="case"><div class="name">Observable</div><div class="pair"><div class="kcard"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">class User {
var name: String by Delegates.observable("<no name>") {
prop, old, new ->
println("$old -> $new")
}
}
fun example() {
val user = User()
user.name = "first" //<no name> -> first
user.name = "second" //first -> second
}</code></pre></div></div></div><div class="case"><div class="name">Implement willSet and didSet</div><div class="pair"><div class="kcard"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">//usage
var x: Int by varWithObservableSetter(5)
.willSet { println("before") }
.didSet { println("after") }
var y: String by varWithObservableSetter("abc")
.didSet { println("after") }
fun test() {
x = 10 // before 5 // after 10
y = "haha" //after "haha"
}
//implementation
class VarWithObservableSetter<T,
out TWillSet : (() -> Unit)?,
out TDidSet : (() -> Unit)?>
internal constructor(
val initialValue: T,
internal val wilLSet: TWillSet,
internal val didSet: TDidSet
) : ObservableProperty<T>(initialValue) {
override fun beforeChange(property: KProperty<*>, oldValue: T, newValue: T): Boolean {
wilLSet?.invoke()
return true
}
override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) {
didSet?.invoke()
}
}
fun <T> varWithObservableSetter(initialValue: T) =
VarWithObservableSetter(initialValue, null, null)
fun <T, R : (() -> Unit)?>
VarWithObservableSetter<T, Nothing?, R>.willSet(action: () -> Unit) =
VarWithObservableSetter(initialValue, action, didSet)
fun <T, R : (() -> Unit)?>
VarWithObservableSetter<T, R, Nothing?>.didSet(action: () -> Unit) =
VarWithObservableSetter(initialValue, wilLSet, action)
</code></pre></div></div></div></div><div class="section"><div class="title">SMART CAST AND PATTERN MATCHING</div><div class="case"><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">fun example1(myView: View) {
if (myView is ImageView) {
myView.setImageAlpha(10)
} else if (myView is TextView) {
myView.setText("Ciao")
}
}
//or
fun example2(myView: View) {
when (myView) {
is ImageView -> myView.imageAlpha = 10
is TextView -> myView.text = "Ciao"
}
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">class MyUtils {
static void example(View myView) {
if (myView instanceof ImageView) {
ImageView imageView =
(ImageView) myView;
imageView.setImageAlpha(10);
} else if (myView instanceof TextView) {
TextView textView = (TextView) myView;
textView.setText("Ciao");
}
}
}</code></pre></div></div></div></div><div class="section"><div class="title">STANDARD LIBRARY</div><div class="case"><div class="name">let</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">customer?.let {
doSth(it.id)
}</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">if (customer != null) {
doSth(customer.id)
}</code></pre></div></div></div><div class="case"><div class="pair"><div class="kcard"><div class="lang">Source Code</div><pre class="code"><code class="java">public inline fun <T, R> T.let(block: (T) -> R): R = block(this)</code></pre></div></div></div><div class="case"><div class="name">apply</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">File(dir).apply { mkdirs() }</code></pre></div><div class="card"><div class="lang">Java</div><pre class="code"><code class="java">File makeDir(String path) {
File result = new File(path);
result.mkdirs();
return result;
}</code></pre></div></div></div><div class="case"><div class="pair"><div class="kcard"><div class="lang">Source Code</div><pre class="code"><code class="java">public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }</code></pre></div></div></div><div class="case"><div class="name">with</div><div class="pair"><div class="kcard"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">fun Activity.toast(str: String) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show()
}
visitList.setOnClickListener {
with(context as Activity) {
toast("haha")
}
}</code></pre></div></div></div><div class="case"><div class="pair"><div class="kcard"><div class="lang">Source Code</div><pre class="code"><code class="java">public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()</code></pre></div></div></div></div><div class="section"><div class="title">DSL</div><div class="case"><div class="name">Bundle</div><div class="pair"><div class="kcard"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">fun bundle(vararg pair: Pair<String, Any>): Bundle {
val bundle = Bundle()
for ((k, v) in pair) {
when (v) {
is String -> this.putString(k, v)
is Int -> this.putInt(k, v)
is Serializable -> this.putSerializable(k, v)
is Bundle -> this.putBundle(k, v)
is Long -> this.putLong(k, v)
...
}
}
return bundle
}
inline fun <reified T : Activity> Activity.jump(requestCode: Int, extras: Bundle) =
ActivityUtils.jump(this, T::class.java, requestCode, extras)
fun example() {
jump<MainActivity>(SOME_CODE, bundle("int-key" to 1, "string-key" to 2))
}</code></pre></div></div></div><div class="case"><div class="name">Converter</div><div class="pair"><div class="kcard"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">//usage
fun testDSL() {
convert("ABC") {
toLower() //ABC => abc
toUpper() //abc => ABC
}
convert(2.2) {
toLower() //2.2 => 2.0
}
convert(2) {
toLower() //编译器:没有这个方法
}
}
//implementation
class MConverter<T>(var value: T) {
val old = value
override fun toString(): String =
"[$old] => [$value]"
}
fun <T> convert(
value: T,
init: MConverter<T>.() -> Unit): MConverter<T> {
val converter = MConverter(value)
converter.init()
return converter
}
fun MConverter<String>.toLower() {
value = value.toLowerCase()
}
fun MConverter<String>.toUpper() {
value = value.toUpperCase()
}
fun MConverter<Double>.toLower() {
value = Math.floor(value)
}</code></pre></div></div></div><div class="case"><div class="name">Retrofit</div><div class="pair"><div class="kcard"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">//usage
someService.someApi(param1, param2).enqueue {
onResponse {
toast("succeed")
}
onFailure { toast("failed") }
onFinish { hideLoading() }
}
//implementation
inline fun <T> Call<T>.enqueue(init: CallbackHelper<T>.() -> Unit) {
val listener = CallbackHelper<T>()
listener.init()
enqueue(listener)
}
class CallbackHelper<T> : Callback<T> {
private var _onResponse: ((T) -> Unit)? = null
private var _onFailure: ((Throwable) -> Unit)? = null
private var _onFinish: (() -> Unit)? = null
override fun onResponse(call: Call<T>, response: Response<T>) {
onResponse?.invoke(response.body())
_onFinish?.invoke()
}
fun onResponse(listener: (T) -> Unit) {
_onResponse = listener
}
fun onFinish(listener: () -> Unit) {
_onFinish = listener
}
override fun onFailure(call: Call<T>, t: Throwable) {
_onFailure?.invoke(t)
_onFinish?.invoke()
}
fun onFailure(listener: (Throwable) -> Unit) {
_onFailure = listener
}
}</code></pre></div></div></div></div><div class="section"><div class="title">QUIZ</div><div class="case"><div class="name">step 1: solve this quiz. step 2: ???. step 3: profit.</div><div class="pair"><div class="kcard"><div class="lang">Kotlin</div><pre class="code"><code class="kotlin">class SomeHolder(var x: Int, val action: () -> Unit)
val sadHolder = SomeHolder(1) { print("I want to print x, but i don’t know how.") }
//modify the following line to let the magic happen
val happyHolder = SomeHolder(1) { print("I know what x is, it's $x")}</code></pre></div></div></div></div></body></html>