You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, both direct casting and the to_* family of functions throw an error if the value to be converted is not valid for the type being converted to. This makes a lot of sense given the highly typed nature of EdgeDB. However, sometimes it can be useful to try to cast from one type to another, or similarly, to try to parse a string to another type, and to fail gracefully if such a conversion is not possible. For example, one may have a type in one's schema with a string property, and may wish to have a computed property that tries to parse that string to another type, such as a datetime or float64, and if this is not successful, returns a null object instead. Example schema pseudocode:
Typically, other languages handle this by either not throwing an error when an invalid conversion is attempted (see javascript and typescript, where NaN is returned for invalid calls to parseInt and parseFloat), or by providing "TryParse" functions (see for example .NET).
It would be good to have a family of functions that handle this gracefully: either just returning {} if a conversion fails, or possibly allowing a user-specified fallback value: some_property := try_to_float64(.value, -1). This isn't limited to just floats, of course; I can see potential use cases with date strings, etc.
The text was updated successfully, but these errors were encountered:
I'd suggest a wrapper to intercept exceptions. It would have more usages and not increase the API surface.
Naming can lean on std::assert(input, options):
Function name could be any of suppress, catch, throws ... I feel like suppress best describes the intention
result :=std::suppress(
to_float64(.str),
catch:=<float64>{}
)
I'm not sure its possible to default an empty set here based on the passed first args return type? If so the default should be an empty set of the inferred type.
The good thing is that this would allow having more logic if you know you have a mixed set where you need different handlers.
Ofc this could be achieved with regex matching and ifs too.
result :=std::suppress(
to_float64(.str),
catch:= (to_float64(str_split("foo#12", "#")[1]))
)
Another option would be to use if-then-else syntax with keywords, which tbh might be the most idiomatic.
Currently, both direct casting and the
to_*
family of functions throw an error if the value to be converted is not valid for the type being converted to. This makes a lot of sense given the highly typed nature of EdgeDB. However, sometimes it can be useful to try to cast from one type to another, or similarly, to try to parse a string to another type, and to fail gracefully if such a conversion is not possible. For example, one may have a type in one's schema with a string property, and may wish to have a computed property that tries to parse that string to another type, such as a datetime or float64, and if this is not successful, returns a null object instead. Example schema pseudocode:And what the returned results might look like:
Typically, other languages handle this by either not throwing an error when an invalid conversion is attempted (see javascript and typescript, where
NaN
is returned for invalid calls toparseInt
andparseFloat
), or by providing "TryParse" functions (see for example .NET).It would be good to have a family of functions that handle this gracefully: either just returning
{}
if a conversion fails, or possibly allowing a user-specified fallback value:some_property := try_to_float64(.value, -1)
. This isn't limited to just floats, of course; I can see potential use cases with date strings, etc.The text was updated successfully, but these errors were encountered: