Fix cast for value which is already an option

This commit is contained in:
Arnaud 2024-12-27 12:09:40 +01:00
parent 3f9f421c74
commit d2388a32fc
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
1 changed files with 8 additions and 0 deletions

View File

@ -9,6 +9,14 @@ proc `as`*[T](value: T, U: type): ?U =
## Casts a value to another type, returns an Option.
## When the cast succeeds, the option will contain the casted value.
## When the cast fails, the option will have no value.
# In Nim 2.0.x, check 42.some as int == none(int)
# Maybe because some 42.some looks like Option[Option[int]]
# So we check first that the value is an option of the expected type.
# In that case, we do not need to do anything, just return the value as it is.
when value is Option[U]:
return value
when value is U:
return some value
elif value is ref object: