https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

Ayfri

01/27/2022, 7:18 PM
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

Luke

01/27/2022, 7:22 PM
No, the best would be
nullableString?.let(::User) ?: User()
a

Arilson José de Oliveira Júnior

01/27/2022, 7:36 PM
The question is why you would like to do this? You can just say
val user = User()
a

Ayfri

01/27/2022, 7:45 PM
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

Dave K

01/27/2022, 7:59 PM
Or class User(name: String?) { val name: String = name ?: “test” }
1
☝️ 1
2 Views