1. Introduction of Function Literals
-
Function literals are frequently used as a constructor of objects.
-
Well known examples are
build.gradle.ktsandjetpack-composefor Andriod or Desktop UIs.
In the sequel let's consider the following builder function, a function literal takes the form
which means that:
- We define a lambda function that can be treated as a method of
ClassA - This lambda function takes the signature
() -> Unit. - To call this lambda function, we invoke
classA.dummyName(). builderorchestrates the execution of the above defined operations.
2. Example
Let's create a builder function which builds a SpringBootExtension object.
Which means that
springBootEextensionaccepts a lambda function which is defined as if we are inside of the class definition ofSpringBootExtension,- The name
buildis in fact dummy and can be anything we want, it is just a name to invoke the execution of the lambda.
-
In short,
ClassA.() -> Unitcan be read as configuration ofClassAin many cases (and bear in mind that it is a lambda function defined inside ofClassA). -
In terms of this terminology:
springBootEextension(_: SpringBootExtension.() -> Unit)accepts a configuration ofSpringBootExtensionwithBuildInfo(_: BuildInfo.() -> Unit)accepts a configuration ofBuildInfowithProperties(_: Properties.() -> Unit)accepts a configuration ofProperties












