4

Given a data class with a nullable field, why can't I construct it by only passing the non-nullable fields ?

I have already seen this related question. What is preventing Kotlin from implicitly assigning null to the nullable fields ?

data class Blah(val id: Int, val name: String?)

val x = Blah(id = 2) // Complains I didn't pass a name ???
3
  • 4
    I think the most important question is: why would/should it? Kotlin tries to be an explicit language - it doesn't do this kind of stuff, it doesn't assign values to variables/properties without developer's consent. Please note contrary to Java, in Kotlin we can't leave a property/field uninitialized. Commented Oct 17, 2023 at 7:14
  • What broot said. Also, if the designers went this route, why would null be different from any other value? Like why not 0 by default for Int? IMO it's almost as arbitrary as null for a nullable value. Commented Oct 17, 2023 at 7:47
  • 1
    @broot "contrary to Java, in Kotlin we can't leave a property/field uninitialized." Java doesn't leave any fields uninitialized; it just doesn't require explicit initialization, but implicitly initializes them to null, 0, 0.0 or false. That's still a well-defined initialization. Kotlin just requires you to be explicit. Commented Oct 17, 2023 at 9:30

2 Answers 2

7

When you want to omit a parameter, then define the default value instead even if it was null:

data class Blah(val id: Int, val name: String? = null)
Sign up to request clarification or add additional context in comments.

Comments

6

No technical reason is preventing Kotlin to be designed so that nullable types in the parameter list of primary constructors get a default null value, but still, it is not designed this way.

Here is a possible explanation as to why.

At the end of the day, the (...) you put after the data class name is first and foremost, a parameter list, for the primary constructor. Yes, you can declare properties there, but it is still a primary constructor parameter list. Treating this particular kind of parameter list to behave differently from other parameter lists would make Kotlin a little more inconsistent. Would you want plain old function parameter lists to do this too?

fun foo(number: Int?) { ... }

// Should you be able to call foo like this?
foo()

Even if you would like every parameter list to behave like this, what if you want to opt out of this for certain functions? Perhaps you wrote a function/data class and you really want callers to explicitly pass null to one of its parameters, in order to e.g. make sure the caller is making a conscious choice. There would need to be extra complexity (e.g. a new annotation like @NoDefaultNull, or a new keyword like required - the design of this can be discussed for ages) to allow you to opt out of this behaviour.

Making primary constructors' parameter lists work like any other parameter list is the simplest option. If you want the nullable parameters optional, just add = null like you would with any other parameter list.

If you have another better idea, feel free to submit a ticket to YouTrack.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.