template
Extension function on strings where you can use a named format to template strings at runtime.
Usage example for single value replacements:
print("hello {someword}".template(mapOf(Pair("someword") { "world" }))) // prints "hello world"Additionally, there is a light-weight no typing support for function calls which allows for more flexible integration:
print("a{@concat(b,c)}".template(
mapOf(
"concat" to {
it.first().toString() + it.last().toString()
}
)
)
) // prints "abc"The syntax for function calls are very loose meaning that it is up to the supplier in context to determine if everything is in order. Additionally, parsing the individual parameters are all treated as Strings and that these following parameters:
hello, worldhello,world
will produce different results. In the first, world will have a space before while in the latter, it will not.
Notes
If a given tag in the string is not found in context, the tag is removed from the original string.
If a given tag returns
null, the tag is removed from the original string (i.e. same behavior as if the tag didn't exist)Tags are case-sensitive
Using
@tells the template function that this should be parameterized as a function call; however, function calls and single values are all treated the same in definition terms within context