With the static dispatch proposal, what is the idiomatic way to return an Err tag using the "?" character?
I'm a little confused by the new pass_to .() syntax.
Existing code
birth_year = Result.map_err(Str.to_u16(birth_year_str), |_| InvalidBirthYearFormat)?
Ok({ first_name, last_name, birth_year })
Is this correct syntax for Static Dispatch with pass to syntax?
birth_year = Str.to_u16(birth_year_str).map_err.(InvalidBirthYearFormat)?
{ first_name, last_name, birth_year }.Ok()
The first one would be correct, but perhaps could be more idiomatic. (But what idiomatic is, isn't clear because the syntax doesn't even exist yet.)
Since map_err
would be defined within theResult
module, you would do
birth_year = birth_year_str.to_u16().map_err(|_| InvalidBirthYearFormat)?
{ first_name, last_name, birth_year }.(Ok)
But with the binary ?
operator, the first line could become
birth_year = birth_year_str.to_u16() ? |_| InvalidBirthYearFormat
Is there a way to get rid of the empty lambda |_|
using pass_to syntax?
.map_err.(InvalidBirthYearFormat)
Also, the foo.(bar)
syntax isn't set in stone. It could become foo->bar
or something else
So would this be possible?
birth_year = birth_year_str.to_u16().map_err.(InvalidBirthYearFormat)?
Not as far as I know. As this would wrap the error in an extra tag
This is the good default because you can define the one you want with the empty lambda, but not the other way around. Also, by default, you keep more context this way which is better most of the times
I don't think the .(fn)
relates to error handling really
Except for convenient tag wrapping like .(Ok)
But it don't specific interact with map_err
Anyway, yeah, the three expected forms of error propagating
Raw propagation:
birth_year_str.to_u16()?
Wrapping:
birth_year_str.to_u16() ? WrapperTag
New tag:
birth_year_str.to_u16() ? |_| NewTag
What's the difference with WrapperTag? Is this just a predefined function that returns a tag?
WrapperTag = |_| WrappedTag
birth_year_str.to_u16() ? WrapperTag
Every tag name is a function that returns a tag
Brian Teague has marked this topic as resolved.
So the wrapper tag case leads to WrapperTag [InvalidNumStr]
where the new tag case throws away the original error InvalidNumStr
Last updated: Jul 06 2025 at 12:14 UTC