Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property type is not a type of overriden

Tags:

kotlin

Sorry for not specific title, I don't really know how to name it better.
I have the following example:

class User

interface Repository<T, ID>

interface UserRepository : Repository<User, Long>

abstract class RepositoryTest<T, ID> {

    abstract var repository: Repository<T, ID>

}

class UserRepositoryTest : RepositoryTest<User, Long>() {

    //Error: Var-property type is "UserRepository", which is not a type of overriden 
    lateinit override var repository: UserRepository

}

I need this architecture for the database testing. Repository have methods like insert, save, delete etc. I want to use this abstraction in RepositoryTest to delete all entries and insert needed data before each test. In UserRepositoryTest I want to specify the repository as UserRepository (it's a child of Repository), but I take an error mentioned in the example.
Why it gives me the error? I thought I can pass a subtype`s type.

like image 893
Feeco Avatar asked Apr 13 '17 10:04

Feeco


1 Answers

repository is a var field. Since UserRepositoryTest can be referred as RepositoryTest<User, Long>, you should be able to assign Repository<User, Long> to the repository field. However, it does not have to be a UserRepository, hence the error.

Changing repositoryto val in RepositoryTest class should fix it.

like image 116
Yoav Sternberg Avatar answered Nov 18 '22 19:11

Yoav Sternberg