Infix Function Kotlin

fun main(args: Array<String>) {
val a = true
val b = false
var result: Boolean

result = a or b // same as a.or(b)
println("result = $result")

result = a and b // same as a.and(b)
println("result = $result")
}
result = true
result = false
  • has only one single parameter.
  • is marked with infix keyword.
class Pattern() {

infix fun createPattern(rows: Int) {
var j = 0
for (i in 1..rows) {
j = 0
for (space in 1..rows-i) {
print(" ")
}
while (j != 2*i-1) {
print("* ")
++j
}
println()
}
}
}

fun main(args: Array<String>) {
val p = Pattern()
p createPattern 3 // same as p.createPattern(4)
}
      * 
* * *
* * * * *

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store