Regenerated samples
This commit is contained in:
parent
805755eb1f
commit
3831572bd7
|
@ -3,6 +3,7 @@ package creatinggenerics
|
|||
import atomictest.eq
|
||||
|
||||
inline fun <reified T> check(t: Any) = t is T
|
||||
// fun <T> check1(t: Any) = t is T // [1]
|
||||
|
||||
fun main() {
|
||||
check<String>("1") eq true
|
||||
|
|
|
@ -4,8 +4,8 @@ import kotlin.random.Random
|
|||
|
||||
private val rnd = Random(47)
|
||||
|
||||
// Accesses action() but can't return
|
||||
// the exact type:
|
||||
// Accesses action() but can't
|
||||
// return the exact type:
|
||||
fun List<Disposable>.inexact(): Disposable {
|
||||
val d: Disposable = this[rnd.nextInt(size)]
|
||||
d.action()
|
||||
|
|
|
@ -22,6 +22,13 @@ class GImplementation<T>(
|
|||
override fun f(): T = x
|
||||
}
|
||||
|
||||
class ConcreteImplementation
|
||||
: GInterface<String> {
|
||||
override val x: String
|
||||
get() = "x"
|
||||
override fun f() = "f()"
|
||||
}
|
||||
|
||||
fun basicGenerics() {
|
||||
gFunction("Yellow")
|
||||
gFunction(1)
|
||||
|
@ -41,4 +48,7 @@ fun basicGenerics() {
|
|||
GImplementation("Cyan").f()
|
||||
GImplementation(11).f()
|
||||
GImplementation(Dog()).f().bark()
|
||||
|
||||
ConcreteImplementation().f()
|
||||
ConcreteImplementation().x
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
// CreatingGenerics/Erasure.kt
|
||||
package creatinggenerics
|
||||
|
||||
fun main() {
|
||||
val strings = listOf("a", "b", "c")
|
||||
val all: List<Any> = listOf(1, 2, "x")
|
||||
useList(strings)
|
||||
useList(all)
|
||||
}
|
||||
|
||||
fun useList(list: List<Any>) {
|
||||
// Doesn't compile:
|
||||
/*
|
||||
if (list is List<String>) { // [1]
|
||||
}
|
||||
*/
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
// CreatingGenerics/ReificationA.kt
|
||||
package creatinggenerics
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
fun <T> a(class_: Class<T>) {
|
||||
// Uses Class<T>
|
||||
fun <T: Any> a(kClass: KClass<T>) {
|
||||
// Uses KClass<T>
|
||||
}
|
|
@ -2,4 +2,4 @@
|
|||
package creatinggenerics
|
||||
|
||||
// Doesn't compile because of erasure:
|
||||
// fun <T> b() = a(T::class.java)
|
||||
// fun <T: Any> b() = a(T::class)
|
|
@ -1,8 +1,9 @@
|
|||
// CreatingGenerics/ReificationC.kt
|
||||
package creatinggenerics
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
fun <T> c(class_: Class<T>) = a(class_)
|
||||
fun <T: Any> c(kClass: KClass<T>) = a(kClass)
|
||||
|
||||
class K
|
||||
|
||||
val kc = c(K::class.java)
|
||||
val kc = c(K::class)
|
|
@ -1,6 +1,6 @@
|
|||
// CreatingGenerics/ReificationD.kt
|
||||
package creatinggenerics
|
||||
|
||||
inline fun <reified T> d() = a(T::class.java)
|
||||
inline fun <reified T: Any> d() = a(T::class)
|
||||
|
||||
val kd = d<K>()
|
|
@ -20,6 +20,8 @@ files:
|
|||
visible: true
|
||||
- name: src/Constraints.kt
|
||||
visible: true
|
||||
- name: src/Erasure.kt
|
||||
visible: true
|
||||
- name: src/ReificationA.kt
|
||||
visible: true
|
||||
- name: src/ReificationB.kt
|
||||
|
|
|
@ -1,20 +1,23 @@
|
|||
// ExceptionHandling/CaptureImplementation.kt
|
||||
package exceptionhandling
|
||||
import atomictest.eq
|
||||
import atomictest.CapturedException
|
||||
|
||||
fun capture(f: () -> Unit): String = // [1]
|
||||
try { // [2]
|
||||
fun capture(f:() -> Unit): CapturedException =
|
||||
try { // [1]
|
||||
f()
|
||||
"Error: Expected an exception" // [3]
|
||||
} catch (e: Throwable) { // [4]
|
||||
"${e::class.simpleName}: ${e.message}"
|
||||
CapturedException(null, // [2]
|
||||
"<Error>: Expected an exception")
|
||||
} catch (e: Throwable) { // [3]
|
||||
CapturedException(e::class, // [4]
|
||||
if (e.message != null) ": ${e.message}"
|
||||
else "")
|
||||
}
|
||||
|
||||
fun main() {
|
||||
capture {
|
||||
throw Exception("!!!")
|
||||
} eq "Exception: !!!"
|
||||
} eq "Exception: !!!" // [5]
|
||||
capture {
|
||||
1
|
||||
} eq "Error: Expected an exception"
|
||||
} eq "<Error>: Expected an exception"
|
||||
}
|
|
@ -6,7 +6,7 @@ function that uses `java.io.File`. Create a `File` object initialized to
|
|||
and add `text` to the file (IntelliJ IDEA will give you hints to help choose
|
||||
the member functions to call for `File`). Use `useLines()` to read the file and
|
||||
display it with `println()`, then use `forEachLine()` to read the file and
|
||||
display it with `println()`. The starter code in `main()`
|
||||
display it with `println()`. The starter code in `main()` tests
|
||||
`writeAndRead()`.
|
||||
|
||||
<sub> This task doesn't contain automatic tests,
|
||||
|
|
|
@ -1,6 +1,18 @@
|
|||
// NothingType/ListOfNothing.kt
|
||||
import atomictest.eq
|
||||
|
||||
fun main() {
|
||||
val none: Nothing? = null
|
||||
|
||||
var nullableString: String? = null // [1]
|
||||
nullableString = "abc"
|
||||
nullableString = none // [2]
|
||||
nullableString eq null
|
||||
|
||||
val nullableInt: Int? = none // [3]
|
||||
nullableInt eq null
|
||||
|
||||
val listNone: List<Nothing?> = listOf(null)
|
||||
val ints: List<Int?> = listOf(null) // [4]
|
||||
ints eq listNone
|
||||
}
|
|
@ -2301,6 +2301,11 @@ public class TestAllExamples extends AbstractTestExamples {
|
|||
testExample("Power Tools/Creating Generics/Examples/src/Select.kt", creatinggenerics.SelectKt::main);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErasure() {
|
||||
testExample("Power Tools/Creating Generics/Examples/src/Erasure.kt", creatinggenerics.ErasureKt::main);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCarCrate() {
|
||||
testExample("Power Tools/Creating Generics/Examples/src/CarCrate.kt", creatinggenerics.CarCrateKt::main);
|
||||
|
|
Loading…
Reference in New Issue