validUntil

fun <T> validUntil(invalidateOn: (invalidate: () -> Unit) -> Any?): ReadWriteProperty<Any?, T>

Creates an automatically invalidated property.

The property starts out invalid and must be set to become valid. When it becomes invalidated you have to set it, again, to make it valid.

The property is invalidated when the provided invalidateOn function calls the lambda function passed as its first argument.

Example:

class SomeClass {
var value by validUntil<String> { invalidate ->
onSomeEvent { invalidate() }
}
}

Android-specific example:

class MainFragment : Fragment() {
private var binding by validUntil<MainFragmentBinding>(::onDestroyView)

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = MainFragmentBinding.inflate(inflater, container, false)
val username = binding.username
// ...
return binding.root
}
}