0

kotlin code, the function

IntegrationModehow(a: String, b:Boolean) do not accept null-able,

how to git ride of the using of double "bang":

var messagingService: String? = null  //set later somewhere
var isAutoIntegration: Boolean? = null

......

if ( messagingService != null && isAutoIntegration != null) {
  val theMode = IntegrationMode(builder.messagingService!!, builder.isAutoIntegration!!)
}

it could use nested let:

messagingService?.let { messagingService ->
    isAutoIntegration?.let { isAutoIntegration ->
        val theMode = IntegrationMode(messagingService, isAutoIntegration)
    }
}

but is there suggested way for this type null-able checking?

lannyf
  • 9,865
  • 12
  • 70
  • 152
  • If they don't accept nullable, then don't accept them. Throw an exception for invalid input, and fix the code that incorrectly uses it. – Rogue Aug 31 '23 at 14:13
  • 2
    you are checking that `messagingService ` is not null, then using `builder.messagingService`. If you want to not have to use `!!`, then simply change the `if` to check `builder.messagingService != null` – AlexT Aug 31 '23 at 14:20
  • Or, better yet, do `val theMode = IntegrationMode(messagingService, isAutoIntegration)`? – Egor Aug 31 '23 at 14:29
  • 2
    Your code doesn't make sense. You've got `val messagingService: String? = null //set later somewhere` - this can't be true, if it's a `val` you can only set it once. You've already set it to `null` so you can't change it to something else later. Please show exactly where these are declared, and exactly where they are set. Are they at top level? Properties in a class? Variables in a function? – k314159 Aug 31 '23 at 15:03
  • @k314159, thx! it should be `var` in the sample. – lannyf Aug 31 '23 at 16:06

2 Answers2

1

I presume messagingService and isAutoIntegration are var properties in a class. Therefore, their values can be changed by another thread between the time you check them and the time you use them. To prevent this, your own suggestion of using ?.let {...} is perfectly fine. But if you don't like the look of that, you can assign them to local variables, which the Kotlin compiler knows are guaranteed not to change:

val messagingService = builder.messagingService
val isAutoIntegration = builder.isAutoIntegration
if (messagingService != null && isAutoIntegration != null) {
    val theMode = IntegrationMode(messagingService, isAutoIntegration)
}
k314159
  • 5,051
  • 10
  • 32
0

try:

with(builder) {
    if ( messagingService != null && isAutoIntegration != null) {
        val theMode = IntegrationMode(messagingService, isAutoIntegration)
    }   
}
  • 1
    using the `with(builder)` the `IntegrationMode(messagingService, isAutoIntegration)` still get compiler error. – lannyf Aug 31 '23 at 16:03
  • Sorry, I should have paid attention to the scope of 'builder'. The idea would be to check if your params are null. As user k314159 did – pORTACIOw Aug 31 '23 at 22:28
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Sep 02 '23 at 05:53