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

Option<T> <> ScVal requires infallible conversions but shouldn't #345

Open
leighmcculloch opened this issue Feb 19, 2024 · 1 comment
Open
Labels
bug Something isn't working

Comments

@leighmcculloch
Copy link
Member

It appears that the Option type only has conversions to ScVal as infallible, and therefore only has conversions for versions of T that are also infallible:

impl<T> From<Option<T>> for ScVal
where
T: Into<ScVal>,
{
fn from(v: Option<T>) -> Self {
match v {
Some(v) => v.into(),
None => ().into(),
}
}
}
impl<T> From<&Option<T>> for ScVal
where
T: Into<ScVal> + Clone,
{
fn from(v: &Option<T>) -> Self {
match v {
Some(v) => v.clone().into(),
None => ().into(),
}
}
}

This makes the use and compatibility of Option as a Soroban type limited, since many other types are only convertible through the Try interface because they can error.

Custom types only require TryFrom and not From.

Option should be implemented for TryFrom, instead of From, so that it is usable in custom types with a wider variety of T's.

Related discussion:

@leighmcculloch leighmcculloch added the bug Something isn't working label Feb 19, 2024
@leighmcculloch leighmcculloch changed the title Option<T> <> ScVal requires infallible conversions Option<T> <> ScVal requires infallible conversions but shouldn't Feb 19, 2024
@leighmcculloch
Copy link
Member Author

leighmcculloch commented Feb 19, 2024

Introducing TryFrom conversions for Option may have some conflicts on other trait impls, so we'll need to test the change that attempts to fix this up most of the stack up to at least the SDK before committing to a fix.

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

2 participants
@leighmcculloch and others