Chukwudi Ogbonna
Chukwudi Ogbonna

Reputation: 3

Not using a safe call operator

Suppose we have a Nullable string assigned as null But somewhere down the code we give it a value I call str. Length

it says we can’t call the method without the safe call operator(?.), I mean since we already know it’s no more null as we have it a value , can’t I just call the length method

Upvotes: 0

Views: 68

Answers (1)

Alex Lysun
Alex Lysun

Reputation: 506

if you are sure that the value is not null you can call

value!!.length

or use default value, for example:

value?.length ?: 0 // default value will be 0
value?.length ?: return // return if value is null

Upvotes: 1

Related Questions