0
0
Kotlinprogramming~5 mins

Infix functions in DSLs in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afun
Binline
Cinfix
Doverride
What is a requirement for the parameter of an infix function?
AIt must be a vararg parameter
BIt can have multiple parameters
CIt must have default values
DIt must be a single parameter without default values
How would you call an infix function named connect on an object a with argument b?
Aa.connect(b)
Ba connect b
Cconnect(a, b)
Dconnect a b
Why are infix functions popular in Kotlin DSLs?
AThey make code look more natural and readable
BThey improve performance
CThey allow multiple parameters
DThey require less memory
Which of these is NOT allowed in an infix function declaration?
AMultiple parameters
BSingle parameter
CExtension function
DMember function
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.