Stream: beginners

Topic: ✔ Idiomatic Result using static dispatch


view this post on Zulip Brian Teague (Feb 17 2025 at 18:18):

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()

view this post on Zulip Kilian Vounckx (Feb 17 2025 at 18:30):

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)

view this post on Zulip Kilian Vounckx (Feb 17 2025 at 18:32):

But with the binary ? operator, the first line could become

birth_year = birth_year_str.to_u16() ? |_| InvalidBirthYearFormat

view this post on Zulip Brian Teague (Feb 17 2025 at 18:32):

Is there a way to get rid of the empty lambda |_| using pass_to syntax?
.map_err.(InvalidBirthYearFormat)

view this post on Zulip Kilian Vounckx (Feb 17 2025 at 18:33):

Also, the foo.(bar) syntax isn't set in stone. It could become foo->bar or something else

view this post on Zulip Brian Teague (Feb 17 2025 at 18:33):

So would this be possible?
birth_year = birth_year_str.to_u16().map_err.(InvalidBirthYearFormat)?

view this post on Zulip Kilian Vounckx (Feb 17 2025 at 18:34):

Not as far as I know. As this would wrap the error in an extra tag

view this post on Zulip Kilian Vounckx (Feb 17 2025 at 18:35):

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

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 19:10):

I don't think the .(fn) relates to error handling really

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 19:11):

Except for convenient tag wrapping like .(Ok)

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 19:11):

But it don't specific interact with map_err

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 19:13):

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

view this post on Zulip Brian Teague (Feb 17 2025 at 19:19):

What's the difference with WrapperTag? Is this just a predefined function that returns a tag?

WrapperTag = |_| WrappedTag
birth_year_str.to_u16() ? WrapperTag

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 19:21):

Every tag name is a function that returns a tag

view this post on Zulip Notification Bot (Feb 17 2025 at 19:21):

Brian Teague has marked this topic as resolved.

view this post on Zulip Brendan Hansknecht (Feb 17 2025 at 19:22):

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