Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
Kotlin: Implement generic Interface multiple times with different Subclasses (Base Class is Constraint)

Error Msg: Type parameter E of 'SchedulableInserter' has inconsistent values: Incident, Assignment

So basically I have an abstract class Schedulable. Also I have two classes Incident (Single occuring event, like a timestamp) and a class Assignment(Basically an Incident but with a duration too) I have a MainActivity, where I want to insert Instances into a SQLiteDB. I want to have a different insert-method for every subclass of Schedulable to easily handle the differences between them.

//SchedulableInserter.kt

interface SchedulableInserter<E> where E : Schedulable{
        fun onInsertSchedulable(item:E)
}

//MainActivity.kt

class MainActivity() : AppCompatActivity(),
    NavigationView.OnNavigationItemSelectedListener,
    SchedulableInserter<Incident>, SchedulableInserter<Assignment> {
 /* ^                                ^
    |                                |
   Sadly Kotlin says:
   Type parameter E of 'SchedulableInserter' has inconsistent values: 
   Incident, Assignment
   A Supertype apperars twice
*/
}

I mean I could differentiate within the method if i bind SchedulerInserter to Schedulable, but I would appreciate not having to do that.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

What I'd suggest is having the inserters as properties in the activity instead of implemented interfaces:

class MainActivity() : AppCompatActivity(),
    NavigationView.OnNavigationItemSelectedListener {
    val incidentInserter = SchedulableInserter<Incident> { ... }
    val assignmentInserter = SchedulableInserter<Assignment> { ... }
}

To make that syntax available you currently need to define SchedulableInserter in Java instead of Kotlin; otherwise you have to write

    val incidentInserter = object : SchedulableInserter<Incident> { 
        override fun ...
    }
over 4 years ago · Santiago Trujillo Report

0

Answered by Ben P.

Solution:

I mean I could differentiate within the method if i bind SchedulerInserter to Schedulable, but I would appreciate not having to do that.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!