kotlinpoet icon indicating copy to clipboard operation
kotlinpoet copied to clipboard

CodeLiteral

Open swankjesse opened this issue 6 years ago • 8 comments

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()
}

swankjesse avatar Sep 28 '18 20:09 swankjesse

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 avatar Nov 26 '18 07:11 alex2069

@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.

swankjesse avatar Nov 26 '18 12:11 swankjesse

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

ZacSweers avatar Sep 04 '19 03:09 ZacSweers

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 .

JakeWharton avatar Sep 04 '19 03:09 JakeWharton

huh, TIL

ZacSweers avatar Sep 04 '19 03:09 ZacSweers

https://github.com/square/kotlinpoet/pull/775

ZacSweers avatar Sep 04 '19 03:09 ZacSweers

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 .

JakeWharton avatar Sep 04 '19 03:09 JakeWharton

Another good use-case for this - we have "constantValue" CodeBlocks in the new element handler APIs that would make more sense as a CodeLiteral

ZacSweers avatar Sep 17 '19 02:09 ZacSweers