what is this error mean when convert from java code with generic to kotlin, single ban after the T?

325
August 05, 2017, at 12:28 PM

trying to convert some java code to kotlin, and having problem with the ones with generic type.

one sample here is the java code from Danny Preussler’s post Of Bundles and ViewModels: here

class BundleAwareViewModelFactory<T extends ParcelableViewModel> implements ViewModelProvider.Factory {
    private final Bundle bundle;
    private final ViewModelProvider.Factory provider;
    public BundleAwareViewModelFactory(@Nullable Bundle bundle,
                                   ViewModelProvider.Factory provider) {
        this.bundle = bundle;
        this.provider = provider;
    }
    @SuppressWarnings("unchecked")
    @Override
    public T create(final Class modelClass) {
        T viewModel = (T) provider.create(modelClass);
        if (bundle != null) {
            viewModel.readFrom(bundle);
        }
        return viewModel;
    }
}
public abstract class ParcelableViewModel extends ViewModel {
    public abstract void writeTo(@NonNull Bundle bundle);
    public abstract void readFrom(@NonNull Bundle bundle);
}

trying to convert the code snippet into kotlin code, not be able do it right so using the android studio IDE to do it and get this result, still it shows compiler error:

Error:(24, 1) Class 'BundleAwareViewModelFactory' must be declared abstract or implement abstract member public abstract fun create(p0: Class!): T! defined in android.arch.lifecycle.ViewModelProvider.Factory

Error:(28, 34) Type parameter bound for T in fun create(p0: Class!): T! is not satisfied: inferred type CapturedTypeConstructor(*) is not a subtype of ViewModel!

class BundleAwareViewModelFactory<T : ParcelableViewModel>(@param:Nullable private val bundle: Bundle?, 
                                                                private val provider: ViewModelProvider.Factory) : 
        ViewModelProvider.Factory {
    override fun create(modelClass: Class<*>): T {
        val viewModel = provider.create(modelClass) as T
        if (bundle != null) {
            viewModel.readFrom(bundle)
        }
        return viewModel
    }
}
abstract class ParcelableViewModel : ViewModel() {
    abstract fun writeTo(bundle: Bundle)
    abstract fun readFrom(bundle: Bundle)
}

what does the single ban T! mean? what does the error means or how those generic to be presented in the kotlin, or any post talks about the generic in kotlin?

Answer 1

T! is a platform type. What it means is that, Kotlin isn't aware of how the final value will turn out to be, whether null or not so it leaves it to you to decide. For your code, try this.

class BundleAwareViewModelFactory<T : ParcelableViewModel>(bundle:Bundle?,
                                                                    provider:ViewModelProvider.Factory):ViewModelProvider.Factory {
  private val bundle:Bundle?
  private val provider:ViewModelProvider.Factory
  init{
    this.bundle = bundle
    this.provider = provider
  }
  override fun create(modelClass:Class<*>):T {
    val viewModel = provider.create(modelClass) as T
    if (bundle != null)
    {
      viewModel.readFrom(bundle)
    }
    return viewModel
  }
}
abstract class ParcelableViewModel:ViewModel() {
  abstract fun writeTo(@NonNull bundle:Bundle)
  abstract fun readFrom(@NonNull bundle:Bundle)
}
Rent Charter Buses Company
READ ALSO
why iphone magnetometer results are different from android

why iphone magnetometer results are different from android

Recently I was working with IOS and Android magnetometer for a project, I searched and found that there exist three ways to get the magnetometer data in IOS (check this question "In iOS, what is the difference between the Magnetic Field values from the Core Location...

300
Bitmap is returning null when using glide

Bitmap is returning null when using glide

I have been trying to figure out why bitmap is giving me a size value within the SimpleTarget but is null soon as am outside the SimpleTarget

482
Website Showing Differently on Mobile?

Website Showing Differently on Mobile?

My resume website appears great on DesktopHowever, I discovered that my HTML paragraph tags don't scale properly when viewing it on different mobile web browsers (see attached image below)

257
How to create an APK but no run it

How to create an APK but no run it

So I want to compile a kivy project so I had a go at following this guide

286