Skip to content

Commit 583f262

Browse files
committed
added some lambda stuff, a bit more text to go with the code
1 parent d235ff5 commit 583f262

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/main/java/me/kotlinintegration/UseAKotlinClass.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ private void printADataClass() {
1919

2020
private void useAnExtensionFunction() {
2121
String blah = "blah";
22-
blah.doo
22+
// blah.doo
2323
}
2424
}

src/main/kotlin/me/kotlindemo/01_IntroToKotlin.kt

+25-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ fun main() {
2020

2121
/**
2222
* FUNCTIONS
23+
* Functions in kotlin are 'first class objects', ie the can be assigned to variables, passed as params
24+
* to other functions, and returned as a result from higher order functions.
2325
*/
2426
fun functions() {
2527
println("\n>>>>> Some functions")
@@ -34,6 +36,7 @@ fun functions() {
3436
14
3537
)//note four is using default, declard in order without names
3638
paramsWithDefaultValues(two = false) //named params do not have to be in order
39+
doSomethngWithLambdas()
3740
}
3841

3942
//functions are first class and do not need to be part of a class
@@ -50,15 +53,35 @@ fun addAndPrint(a: Int, b: Int) = println(" fun with expression body: printing
5053

5154

5255
fun addAndPrintBody(a: Int, b: Int) {
53-
println(" fun with block body: printing from a body with no return: ${a + b}")
56+
println("\n fun with block body: printing from a body with no return: ${a + b}")
5457
return Unit //not required, can be ignored
5558
}
5659

5760
fun paramsWithDefaultValues(one: String = "blah", two: Boolean = false, three: Int = 0,
5861
fourNullable: String? = null){ //one, two, three are not nullable. four is
59-
println(" fun params with default values: one: $one two:$two three: $three, fourNullable: $fourNullable")
62+
println("\n fun params with default values: one: $one two:$two three: $three, fourNullable: $fourNullable")
6063
}
6164

65+
/**
66+
* Lambdas
67+
* Similar to the way java does them for the most part
68+
*/
69+
//Lambda Expressions
70+
//return type is inferred, ie we don't explicitly say 'Int'
71+
val sumTypeInferred = {x: Int, y: Int -> x + y}
72+
val aResult = sumTypeInferred(3, 4)
73+
74+
//Explicit type declaration
75+
// Format: (input params) -> return type = implementation
76+
val printNumber : (Int) -> Unit = {num -> println(" explioct type declared for lambda num: $num")}
77+
// the following doesn't work even tho I found lots of examples with it
78+
//val printNumberErrors : Int -> Unit = {num -> println(" explioct type declared for lambda num: $num")}
79+
80+
fun doSomethngWithLambdas() {
81+
println("\n>>>>> do a couple of lambda things")
82+
println(" invoke a lambdaExpression 3 + 4 = ${sumTypeInferred(3, 4)}")//need ${} since params, even tho it is a 'property'
83+
printNumber(14)
84+
}
6285
/**
6386
* VARIABLES
6487
*/

0 commit comments

Comments
 (0)