Recall & Review
beginner
What is an infix function in Kotlin?
An infix function is a special kind of function that can be called using infix notation, meaning you can write it between its receiver and argument without dots or parentheses, making code more readable and natural.
Click to reveal answer
beginner
How do you declare an infix function in Kotlin?
You declare an infix function by adding the keyword
infix before the function name. It must be a member function or an extension function with a single parameter and no varargs or default values.Click to reveal answer
intermediate
Why are infix functions useful in DSLs (Domain Specific Languages)?
Infix functions make DSL code look cleaner and more like natural language, improving readability and expressiveness by allowing calls without dots or parentheses, which fits well with DSL style.
Click to reveal answer
beginner
Example: What does this Kotlin code do?<br>
infix fun Int.times(str: String) = str.repeat(this)
This defines an infix function
times that repeats a string a given number of times. For example, 2 times "Hi" returns "HiHi".Click to reveal answer
intermediate
What are the restrictions on parameters for infix functions?
Infix functions must have exactly one parameter. They cannot have varargs, default parameter values, or multiple parameters.
Click to reveal answer
Which keyword is used to declare an infix function in Kotlin?
✗ Incorrect
The
infix keyword marks a function as infix, allowing it to be called without dots or parentheses.What is a requirement for the parameter of an infix function?
✗ Incorrect
Infix functions must have exactly one parameter and cannot have default values or varargs.
How would you call an infix function named
connect on an object a with argument b?✗ Incorrect
Infix functions are called using infix notation:
a connect b without dots or parentheses.Why are infix functions popular in Kotlin DSLs?
✗ Incorrect
Infix functions improve readability and make DSL code look like natural language.
Which of these is NOT allowed in an infix function declaration?
✗ Incorrect
Infix functions cannot have multiple parameters; they must have exactly one.
Explain what an infix function is and how it helps in creating DSLs in Kotlin.
Think about how you write code that reads like a sentence.
You got /4 concepts.
Describe the rules and restrictions for declaring an infix function in Kotlin.
Focus on the function signature and keywords.
You got /5 concepts.