Skip to content

Make addSuperclassConstructorParameter args nullable #789

Open
@PaulWoitaschek

Description

I want to add a nullable property to an enum. This doesn't currently work using the format function becuase the arg elements are not declared as nullable.

TypeSpec.enumBuilder("Sample")
  .primaryConstructor(
    FunSpec.constructorBuilder()
      .addParameter(
        ParameterSpec.builder(
          "myNullableString",
          String::class.asTypeName().copy(nullable = true)
        ).build()
      )
      .build()
  )
  .addEnumConstant(
    name = "Rock",
    typeSpec = TypeSpec.anonymousClassBuilder()
      .addSuperclassConstructorParameter("%S", "Hello")
      .build()
  )
  .addEnumConstant(
    name = "Paper",
    typeSpec = TypeSpec.anonymousClassBuilder()
      .addSuperclassConstructorParameter("%S", null) // <-- this doesn't compile
      .addSuperclassConstructorParameter(CodeBlock.of("%S", null)) // <-- workaround
      .build()
  )
  .build()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions