%S or %C for Strings
If you want to emit code that includes string literals, you can use `%S` or `%C` to emit a string into the generation.
The programming language Dart allows the usage of strings with a double quotes `("")` and single quotes `('')`. There are no difference between both variants.
If you want to have a string with double quotes use the `%S` formatter and for single quotes the '%C' formatter.
In the following there is a small program that emits a unique string for each available string formatter:
fun main(args: Array<String>) {
val mainClass = ClassSpec.builder("HelloWord")
.function(getFunction("%S", "Dart")) // Uses double quotes
.function(getFunction("%C", "Poet")) // Uses single quotes
.build()
val dartFile = DartFile.builder("hello_world")
.type(mainClass)
.build()
dartFile.write(System.out) // Writes the code into the output stream
}
The generated code result is:
class HelloWord {
String getDart() {
return "Dart";
}
String getPoet() {
return 'Poet';
}
}Last updated