Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 138 Vote(s) - 3.64 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Kotlin, smart cast is impossible because of complex expression

#1
I have this code:

// allocate one mesh
pScene.mNumMeshes = 1
pScene.mMeshes = mutableListOf(AiMesh())
val pMesh = pScene.mMeshes[0]

Where `mMeshes` is a parameter of type

`var mMeshes: MutableList<AiMesh>? = null,`

Compilers complains on the last row, where I try to declare `pMesh`

> Smart cast to `MutableList<AiMesh>` is impossible because `pScene.mMeshes` is a complex expression

What's the problem?
Reply

#2
Since `mMeshes` is a `var` property, it can change between the assignment of `mutableListOf(AiMesh())` and the usage in `pScene.mMeshes[0]`, meaning that it is not guaranteed to be not-null at the use site.

The compiler enforces [null-safety][1], treating `pScene.mMeshes` as nullable `MutableList<AiMesh>?` and not allowing you to use it as `MutableList<AiMesh>` (i.e. it cannot safely perform a [smart cast][2]).

To fix that, you can simply make a [non-null assertion][3]:

val pMesh = pScene.mMeshes!![0]

Or just reuse the value you put into the list:

val pMesh = AiMesh()
pScene.mMeshes = mutableListOf(mesh)
// use `pMesh` below


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through