kotlinpoet
kotlinpoet copied to clipboard
CodeLiteral
Idea: an interface CodeLiteral
that is used whenever we have %L
to let the value itself convert to source code.
interface CodeLiteral {
fun toCodeBlock(): CodeBlock
}
Here’s how we might define a function to convert a Float
to a CodeLiteral
:
private val Float.literal: CodeLiteral
get() {
val v = this
return object : CodeLiteral {
override fun toCodeBlock() = CodeBlock.of("%Lf", v)
}
}
And here’s how we might use it:
@Test fun testLiterals() {
val taco = TypeSpec.classBuilder("Taco")
.addFunction(FunSpec.builder("getPrice")
.addModifiers(KModifier.PUBLIC, KModifier.FINAL, KModifier.OVERRIDE)
.returns(Float::class)
.addStatement("return %L", 1.99f.literal)
.build())
.build()
}
Though a little nicer, this still doesn't solve the original problem - if you don't know the type (such as when reading from a generic annotation AnnotationValue.value
which returns Object
) - you can't can't call .literal
on it.
The types that can be used in an annotation are well defined and restricted to primitives, arrays, enums, and classes. That you have to go out of your way to correctly emit these standard types is just bizarre and frustrating.
@alex2069 presumably you could write a function that takes Any
and does it. Could you give me a concrete example? In the code I've generated I rarely need to emit literals of unknown types.
I like the idea of this, but the float impl doesn't always quite work I think
1.00000001f.literal
comes out to 1.0f
That seems like a missing IDE warning. Java has http://errorprone.info/bugpattern/FloatingPointLiteralPrecision.
On Tue, Sep 3, 2019 at 11:21 PM Zac Sweers [email protected] wrote:
I like the idea of this, but the float impl doesn't always quite work I think
1.00000001f.literal comes out to 1.0f
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/square/kotlinpoet/issues/484?email_source=notifications&email_token=AAAQIEKIBEZOO7VB7PF5VPDQH4SR7A5CNFSM4FX6LZ32YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD52GPTQ#issuecomment-527722446, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAQIELE5D5M3XU3HRARFXDQH4SR7ANCNFSM4FX6LZ3Q .
huh, TIL
https://github.com/square/kotlinpoet/pull/775
Seems like https://youtrack.jetbrains.com/issue/KT-20837
On Tue, Sep 3, 2019 at 11:27 PM Zac Sweers [email protected] wrote:
huh, TIL
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/square/kotlinpoet/issues/484?email_source=notifications&email_token=AAAQIELKWQLP2AXDKKFWSYDQH4TKRA5CNFSM4FX6LZ32YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD52GYLA#issuecomment-527723564, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAQIEJ6SNWAIDEB36YANJDQH4TKRANCNFSM4FX6LZ3Q .
Another good use-case for this - we have "constantValue" CodeBlocks
in the new element handler APIs that would make more sense as a CodeLiteral