This commit is contained in:
Alexander Nozik 2021-03-05 10:26:02 +03:00
parent 21c53e699f
commit 35b9fc62f3
5 changed files with 104 additions and 5 deletions

View File

@ -0,0 +1,15 @@
package lesson1
/**
* This is the entry point of the program. The body of this method is considered to be the body of the program.
* `suspend` modifier allows to launch the program in asynchronous mode and could be omitted. [args] are used to pass
* console arguments in a desktop application and also could be omitted.
*
* On JVM there could be several entry points that a distinguished by their fully-qualified name with kotlin-to-JVM
* transformation (capitalized + Kt suffix).
*
* In Kts/Jupyter/Worksheet main function is absent. The body of the script plays that role.
*/
suspend fun main(args: Array<String>) {
println("Hello Kotlin from MIPT!")
}

View File

@ -0,0 +1,26 @@
package lesson1
/**
* Interpolated string allow to construct string fast
*/
fun main() {
println("This is a plain string")
val arg = "Interpolated"
println("This is an $arg string")
val number = 1
println("This is an $arg string number ${number + 1}")
println("This is a string with \$")
println("""
This is a
multi
line
raw
string
""".trimIndent())
println("""This is a raw string number ${number+1} with \ and ${'$'} """)
}

View File

@ -0,0 +1,21 @@
package lesson1
/**
* `val` means read-only property
*
* `var` means writeable property
*
* Unnecessary use of `var` is not recommended.
*
* **NOTE:** In performance critical cases explicit cycles are better, but mutable state should be encapsulated.
*/
fun main() {
/* Not recommended */
var counter = 0
for(i in 0..20){
counter += i
}
/* recommended */
val sum = (0..20).sum()
}

View File

@ -0,0 +1,42 @@
package lesson1
//preamble
/**
* Functions could be defined everywhere in kotlin code. This is a so-called top-level function.
*/
fun doSomething(){
println("I did it")
}
object AnObject{
/**
* This is a member-function
*/
fun doSomethingInAnObject(){
println("I did it in an object")
}
}
fun doSomethingSpecial(){
/**
* This one is inside another function
*/
fun doSomethingInside(){
println("I did it inside another function")
}
doSomethingInside()
}
//idiom 3 - Unit
fun returnUnit(): Unit{
// no return statement `Unit` is returned implicitly
}
//idiom 4 - default parameters
fun doSomethingWithDefault(a: Int = 0, b: String = ""): String {
return "A string with a == $a and b == $b"
}
//idiom 5 - function body shortcut
fun theSameAsBefore(a: Int = 0, b: String = ""): String = "A string with a == $a and b == $b"

View File

@ -1,5 +0,0 @@
package ru.mipt.npm.ks.demo
fun main() {
println("Hello Kotlin from MIPT!")
}