Hi, I have a class like this `class User(val name:...
# getting-started
a
Hi, I have a class like this
class User(val name: String = "test")
and I have a place where I do this
val user = User(nullableString)
, is there a way to say that if the nullableString is null, use the default value ?
l
No, the best would be
nullableString?.let(::User) ?: User()
a
The question is why you would like to do this? You can just say
val user = User()
a
No Arlison as I have to use the value anyway, but I've used a second constructor with a name: String? = null parameter instead
👍 1
d
Or class User(name: String?) { val name: String = name ?: “test” }
1
☝️ 1