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

can't emit primary constructor default parameters #133

Closed
burntcookie90 opened this issue Jun 19, 2017 · 5 comments · Fixed by #172
Closed

can't emit primary constructor default parameters #133

burntcookie90 opened this issue Jun 19, 2017 · 5 comments · Fixed by #172
Milestone

Comments

@burntcookie90
Copy link
Contributor

class Foo(val id: String? = null)
@burntcookie90
Copy link
Contributor Author

whoops, missed defaultValue 🤦‍♂️

@burntcookie90
Copy link
Contributor Author

Ok, sorry for the goof, I'm re-opening because the exact code block above can't be generated.

@burntcookie90 burntcookie90 reopened this Jun 19, 2017
@JakeWharton
Copy link
Member

JakeWharton commented Jun 19, 2017 via email

@burntcookie90
Copy link
Contributor Author

burntcookie90 commented Jun 19, 2017 via email

@burntcookie90
Copy link
Contributor Author

  @Test fun constructorWithDefaultParamValue() {
    val type = TypeSpec.classBuilder("Taco")
        .primaryConstructor(FunSpec.constructorBuilder()
            .addParameter(ParameterSpec.builder("a", Int::class).defaultValue("1").build())
            .addParameter(ParameterSpec.builder("b", String::class.asTypeName().asNullable())
                .defaultValue("null")
                .build())
            .build())
        .addProperty(PropertySpec.builder("a", Int::class)
            .initializer("a")
            .build())
        .addProperty(PropertySpec.builder("b", String::class.asTypeName().asNullable())
            .initializer("b")
            .build())
        .build()

    assertThat(toString(type)).isEqualTo("""
        |package com.squareup.tacos
        |
        |import kotlin.Int
        |import kotlin.String
        |
        |class Taco(val a: Int = 1, val b: kotlin.String? = null)
        |""".trimMargin())
  }

actual output:

class Taco(val a: Int, val b: kotlin.String?)

There's a withInitializer flag when emitting the primaryConstructor paramters, I'll try and take a look at debugging this tomorrow.

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

Successfully merging a pull request may close this issue.

2 participants