1

I'm trying to initiate the RoomAlarmRepository inside the RepoInit 1 & 2.

The first one says "Type mismatch", and wouldn't let me compile. That is, unless I cast it like in RepoInit2, at which point it tells me that the cast is unchecked.

class RepoInit1(app: App) {
internal val repo: AlarmRepository<AlarmModel> = RoomAlarmRepository(app.database)
}

class RepoInit2(app: App) {
internal val repo: AlarmRepository<AlarmModel> = RoomAlarmRepository(app.database) as AlarmRepository<AlarmModel>
}


class RoomAlarmRepository(val database: AppDatabase) : AlarmRepository<RoomAlarmModel> {

}

class RoomAlarmModel : AlarmModel {


}

I'm currently using the second one, as it actually works. I know I'm missing something where I declare the variable, but I'm not sure what it is.

Type mismatch Unchecked Cast

1 Answer 1

1

Declare the generic type it with the out modifier as covariant. See here https://kotlinlang.org/docs/reference/generics.html for details.

class RepoInit2(...) {
    internal val repo: AlarmRepository<out AlarmModel> = RoomAlarmRepository(...)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.