So I stumbled (yet again) upon a name shadowing is...
# android
d
So I stumbled (yet again) upon a name shadowing issue where I’d like the compiler to warn me that my choice is ambiguous, but I don’t know if this is yet something that the Kotlin compiler supports. Imagine the following code:
Copy code
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment

class OuterFragment : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        MyDialogFragment.create(Bundle()).apply {
            show(childFragmentManager, "DIALOG")
                // ^--- this should be this@OuterFragment.childFragmentManager
        }
    }
}

class MyDialogFragment : DialogFragment() {
    companion object {
        fun create(bundle: Bundle): MyDialogFragment =
            MyDialogFragment().apply {
                arguments = bundle
            }
    }
}
Any hints how I could make
childFragmentManager
yellowish in the IDE?
k
Not that it solves the problem of name shadowing when
this
scope changes, but I would argue that you shouldn’t be using
.apply
here and instead be using
Copy code
Fragment.create(...).also { fragment -> 
  fragment.show(...)
}
☝️ 1
d
Yes,
also
is the better alternative, but sometimes people do not think about this - I really want a warning however in case of misuse.