Hello, first post here! I’m having trouble working with sealed classes and generics.
I have a sealed interface with 2 enums that implement it:
sealed interface Nucleotide
enum class DNANucleotide : Nucleotide { Adenine, Cytosine, Guanine, Thymine }
enum class RNANucleotide : Nucleotide { Adenine, Cytosine, Guanine, Uracil }
Then I defined one function for each enum :
fun complement(nucleotide: DNANucleotide): DNANucleotide = when (nucleotide) {
DNANucleotide.Adenine -> DNANucleotide.Thymine
DNANucleotide.Cytosine -> DNANucleotide.Guanine
DNANucleotide.Guanine -> DNANucleotide.Cytosine
DNANucleotide.Thymine -> DNANucleotide.Adenine
}
fun complement(nucleotide: RNANucleotide): RNANucleotide = when (nucleotide) {
RNANucleotide.Adenine -> RNANucleotide.Uracil
RNANucleotide.Cytosine -> RNANucleotide.Guanine
RNANucleotide.Guanine -> RNANucleotide.Cytosine
RNANucleotide.Uracil -> RNANucleotide.Adenine
}
At last I want to add a generic function that includes the last two:
fun <N : Nucleotide> complement(nucleotide: N): N = when (nucleotide) {
is DNANucleotide -> complement(nucleotide)
is RNANucleotide -> complement(nucleotide)
}
This last function doesn’t compile because the when clause isn’t exhausted and the return type is wrong. Any help or tips to solve this? Thanks!