Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataRefresh extension function throws exception for subclasses of MutableCommonDataEntity #109

Closed
vestrel00 opened this issue Nov 28, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@vestrel00
Copy link
Owner

vestrel00 commented Nov 28, 2021

Using the DataRefresh extension function on a direct subclass of CommonDataEntity works just fine. For example, to refresh an immutable email,

var email: Email
email = email.refresh(contactsApi)

An exception?!

However, when trying to refresh a mutable email,

var email: MutableEmail
email = email.refresh(contactsApi)

Will throw an exception,

java.lang.ClassCastException: contacts.core.entities.Email cannot be cast to contacts.core.entities.MutableEmail

Why?

The reason this occurs is because of the function signature,

fun <T : CommonDataEntity> T.refresh(...): T? =

Combined with the following class hierarchy,

interface CommonDataEntity
interface MutableCommonDataEntity : CommonDataEntity

data class Email : CommonDataEntity
data class MutableEmail : MutableCommonDataEntity

Allows for the refresh extension function to be callable for MutableEmail given that it is an indirect subclass of CommonDataEntity. However, the refresh function itself only returns immutable instances. So,

var email: MutableEmail?
email = email?.refresh(contactsApi) // returns an instance of `Email`, which cannot be casted to a `MutableEmail`.
@vestrel00 vestrel00 added the bug Something isn't working label Nov 28, 2021
@vestrel00 vestrel00 self-assigned this Nov 28, 2021
@vestrel00
Copy link
Owner Author

Technically fixed in https://github.com/vestrel00/contacts-android/pull/114/files#r757986465 but there is a remaining TODO

@vestrel00
Copy link
Owner Author

This is a part of v0.1.9 Release Checklist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant