Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes noticed during the training #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ We'll work with the `exercises` module most of the time. It contains a single te
where you will find all resources and exercises. For every topic discussed in the training, you will find the following files:
* `T01Topic.kt` - template used by trainers during the live-coding demo
* `T01Topic.md` - short reference explaining the given topic in a nutshell
* `T01TopicTasks.md` - tasks for the trainees. Every task is a simple unit test, that you will have to fix.
* `T01TopicTasks.kt` - tasks for the trainees. Every task is a simple unit test, that you will have to fix.

Sample solutions for all the tasks are provided in the package `de.iteratec.kotlin.basic.solutions`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Requirements: none

Things to discuss:
- Java to Kotlin Converter in IntelliJ (Right click on File -> Convert Java file to Kotlin file)
- Inspect Kotlin bytecode and decompiled Java class (Shift, Shift -> Show Kotlin Bytecode -> Decompile)
- Inspect Kotlin bytecode and decompiled Java class
- Shift, Shift -> Show Kotlin Bytecode -> Decompile
- Tools -> Kotlin -> Show Kotlin Bytecode -> Decompile
- Comparison with main-function in Java
- Explain function syntax and Unit in Kotlin
- Invocation with positional or named arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class T01FunctionsTasks {
@Test
fun defaultValues() {
/*
assertEquals(addAll(0, 1), 1)
assertEquals(addAll(0, 1, 2), 3)
assertEquals(1, addAll(0, 1))
assertEquals(3, addAll(0, 1, 2))
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ fun main() {
// Multiline strings allow for nice formatting and conveniently escape some characters automatically.
// You can activate syntax highlighting (Right-Click on string -> Show Context Actions -> Inject language or reference -> JSON)
val uglyJson = "{\n\"property\":\"value\",\n\"array\":[\n\"element1\",\n\"element2\"\n]\n}"

// language=json
val multiLineString = """
{
"property": "value",
Expand All @@ -32,4 +34,4 @@ fun main() {
}
""".trimIndent().replace(" ", "")
println(multiLineString == uglyJson)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package de.iteratec.kotlin.basic

/**
# Collections & lambdas
Requirements:
- Functions
- Mutability and expressions
Requirements:
- Functions
- Mutability and expressions

Things to discuss:
- Instantiating collections
- Mutable & immutable collections
- Collection transformations & lambdas
- TODO: Only use sequences in the exercises?
Things to discuss:
- Instantiating collections
- Mutable & immutable collections
- Collection transformations & lambdas
- Sequences
*/
fun main() {
// Kotlin collections can be instantiated via factory methods of the form "<CollectionType>Of(...)"
Expand All @@ -32,7 +32,9 @@ fun main() {
val d = words.map() { word -> word.uppercase() } // Lambda as the last argument of a function call can be taken out of the argument list
val e = words.map { word -> word.uppercase() } // Empty argument list can be omitted
val f = words.map { it.uppercase() } // If lambda has only one argument, you can refer to it with 'it'

val g = words.asSequence()
.filter { it.length > 4 }
.map { it.uppercase() }.toList()
println("Using a lambda or method reference with map $a")

// Kotlin offers a lot of convenient methods from the Filter-Map-Reduce-idiom. Most methods return new lists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ myString.filter({ letter: Char -> letter.isWhitespace()}) // an ordinary lambda
myString.filter({ letter -> letter.isWhitespace()}) // input types of lambda functions can often be auto-inferred
myString.filter({ it.isWhitespace() }) // if the lambda function has only one input parameter of aut-inferred type, you can refer to it as "it" without declaring it
myString.filter(){ it.isWhitespace() } // as a convention, if the last parameter of a function invocation is a lambda, it should be taken out of the argument list
myString{ it.isWhitespace() } // if the function invocation has only 1 lambda as argument, we can even remove the argument brackets
myString.filter { it.isWhitespace() } // if the function invocation has only 1 lambda as argument, we can even remove the argument brackets
```

# Collections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fun main() {
// This constructor call becomes the first line of the body of the primary constructor of your subclass. Inheriting
// from an interface is simpler since interfaces do not have constructors, and you only have to supply the name of
// the interface.
class MyCustomRuntimeException(message: String): RuntimeException(message), Serializable {
class MyCustomRuntimeException(message: String) : RuntimeException(message), Serializable {
override fun toString(): String {
return "Great! MyCustomRuntimeException happened with message: $message"
}
Expand Down
21 changes: 21 additions & 0 deletions exercises/src/test/kotlin/de/iteratec/kotlin/basic/T08Objects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,24 @@ class ClassWithStaticMethod {
}
}

// please do not do this
class Singleton private constructor() {

fun myShinyMethod() {
println("method")
}
companion object {
var instance: Singleton? = null
get() {
return if (field == null) {
instance = Singleton()
field
} else {
field
}
}
}
}



Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,44 @@ package de.iteratec.kotlin.basic
- Scope & resolution of extension functions
- Extension functions in bytecode
- Use cases of extension functions
- Best practices: Shadowed functions / properties
*/
fun main() {
// In Kotlin, you can define "instance methods" of classes outside of those classes, so-called extension functions.
fun List<Int>.multiplyEachBy(factor: Int): List<Int> {
return this.map {
it * factor
}

// In Kotlin, you can define "instance methods" of classes outside of those classes, so-called extension functions.
fun List<Int>.multiplyEachBy(factor: Int): List<Int> {
return this.map {
it * factor
}
}

val simpleSuccession = listOf(1, 2, 3)
fun main() {
val simpleSuccession: List<Int> = listOf(1, 2, 3)
println(simpleSuccession.multiplyEachBy(10))

MyShinyProperties().myMethod()
}

class MyShinyProperties {
val publicProperty: Int = 2
internal val internalProperty: String = ""
protected val protectedProperty: String = ""
private val property: String = ""

// Which method will be executed?
fun myMethod() {
println("instance")
}
}

// Which method will be executed?
fun MyShinyProperties.myMethod() {
println("extension")

// Only public / internal properties of the extended class are visible in extension functions
this.publicProperty
this.internalProperty

// Does not compile
// this.protectedProperty
// this.property
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ fun main() {
}
println("Announcement with receiver = $announcementText2")


// Scope functions with receiver
// 'with' function allows to switch context in the lambda as we were a member method of the receiver object
val resultWithScopeFunction = with(StringBuilder()) {
Expand Down