1

in the below, i am trying to lateinit a variable as shown. however after folloing some examples in the internet i understood the concept of it but however i received the below posted error message due to the code stated in the method setupCommRequestService()

error message:

assignment are not expressions kotlin and only expression are allowed

please have a look at the code posted below and please let me know how can i fix it

code

lateinit var initCommRequestService : Single<CommunicationRequestService>

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    setupCommRequestService()
        .map {
            it.getAllPhotos()
        }

}

fun setupCommRequestService() : Single<CommunicationRequestService> {
    return initCommRequestService = CommunicationRequestService.initRetrofit(this@MainActivity)!!
}
}
Amrmsmb
  • 1
  • 27
  • 104
  • 226

2 Answers2

1

To expand on Egor's answer, the issue is not really linked to lateinit or other stuff, it's just that in Kotlin assignment is not an expression, so x = y is a statement and not an expression. Given that return expects an expression (or nothing at all, if jumping out of a function/method returning Unit), return x = y is not allowed as it's wrong from a grammatical point of view.

Note that in Java that would be allowed, though.

As others said, the fix consists in splitting the 2 instructions:

val x = y
return x
user2340612
  • 10,053
  • 4
  • 41
  • 66
0
fun setupCommRequestService(): Single<CommunicationRequestService> {
  initCommRequestService = CommunicationRequestService.initRetrofit(this@MainActivity)!!
  return initCommRequestService
}
Egor
  • 39,695
  • 10
  • 113
  • 130
  • 5
    I would have thought someone with your rep level would know better than to give code only answers. https://stackoverflow.com/help/how-to-answer – Rob Jul 10 '19 at 11:28