Skip to content

Instantly share code, notes, and snippets.

@JohanneA
Created November 22, 2021 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohanneA/507518abcc8ca1db13822e901c4990f3 to your computer and use it in GitHub Desktop.
Save JohanneA/507518abcc8ca1db13822e901c4990f3 to your computer and use it in GitHub Desktop.
Kotlin test DSL
// Lambda with receiver
fun buildString (
builderAction: StringBuilder.() -> Unit // Note the location of the parentheses
) : String {
val stringBuilder = StringBuilder()
stringBuilder.builderAction() // This is now a method on the string builder
return stringBuilder.toString()
}
val string = buildString {
append("Hello, ") // Note we no longer need the qualifyer in front
append("World!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment