Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternate to '$' syntax for substitutions? #831

Open
kevinherron opened this issue Apr 10, 2021 · 3 comments
Open

Alternate to '$' syntax for substitutions? #831

kevinherron opened this issue Apr 10, 2021 · 3 comments

Comments

@kevinherron
Copy link

Any chance of adding an alternate character that can be used when specifying substitions ($T, $L, $S etc...)?

$ is a real bummer to use from Kotlin.

Regular Strings are kinda bad:

addStatement(
    "CompletableFuture<\$T> future = getMemberNodeAsync(" +
        "\"\$L\", " +
        "\"$variableName\", " +
        "\$T.parse(\"\$L\"), " +
        "false" +
        ")",
    UaNode::class.java,
    variableTypeNodeNamespaceUri,
    ExpandedNodeId::class.java,
    referenceTypeId.expanded(namespaceTable).toParseableString()
)

Multi-line Strings are even worse:

add(
    """
    new ${'$'}T<>(
        "$namespaceUri",
        "$propertyName",
        ${'$'}T.parse("nsu=${'$'}L;${'$'}L"),
        ${'$'}T.${propertyNode.valueRank.toValueRanksFieldRef()},
        ${'$'}T.class
    )
    """.trimIndent(),
    QualifiedProperty::class.java,
    ExpandedNodeId::class.java,
    dataTypeNamespace,
    dataTypeIdString,
    ValueRanks::class.java,
    dataTypeClassName
)
@JakeWharton
Copy link
Member

Here's an easy workaround I've used in the past:

private const val T = "\$T"

@kevinherron
Copy link
Author

This works but you need to be careful with type parameters in places you import those constants.

In particular, a reified type parameter T defined in any file where you've imported the constant will require its type parameter to be renamed:

inline fun <reified T> MethodSpec.Builder.addAnnotation(builderAction: AnnotationSpec.Builder.() -> Unit) {
    val clazz = T::class.java
    val annotationBuilder = AnnotationSpec.builder(clazz)
    builderAction(annotationBuilder)
    addAnnotation(annotationBuilder.build())
}

@vlsi
Copy link

vlsi commented Jun 14, 2021

@kevinherron , you might want to check #761 (comment)

An alternative approach for Kotlin would be to apply JetPack Compose for building the AST.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants