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

Add Option to stdlib #158

Merged
merged 2 commits into from
Jun 28, 2020
Merged

Add Option to stdlib #158

merged 2 commits into from
Jun 28, 2020

Conversation

phated
Copy link
Member

@phated phated commented Jun 27, 2020

This adds the Option data type to pervasives, and additionally adds an option.gr module to the standard library that shamelessly reimplements APIs I've liked from OCaml, BuckleScript's Belt library, and Rust.

I also added peek because I think it is useful once there's currying and you can chain things with |> or similar.

One glaring omission from this implementation is converting to/from Result because that doesn't exist yet.

Copy link
Member

@peblair peblair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I think it'd be nice to streamline a few of these implementations (see suggestions), but this looks good!

stdlib/option.gr Outdated
Comment on lines 80 to 83
| (Some(a), Some(b)) => Some((a, b))
| (Some(_), None) => None
| (None, Some(_)) => None
| (None, None) => None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| (Some(a), Some(b)) => Some((a, b))
| (Some(_), None) => None
| (None, Some(_)) => None
| (None, None) => None
| (Some(a), Some(b)) => Some((a, b))
| _ => None

stdlib/option.gr Outdated
Comment on lines 89 to 92
| (Some(a), Some(b)) => Some(fn(a, b))
| (Some(_), None) => None
| (None, Some(_)) => None
| (None, None) => None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| (Some(a), Some(b)) => Some(fn(a, b))
| (Some(_), None) => None
| (None, Some(_)) => None
| (None, None) => None
| (Some(a), Some(b)) => Some(fn(a, b))
| _ => None

stdlib/option.gr Outdated
Comment on lines 99 to 101
| Some(Some(x)) => Some(x)
| Some(None) => None
| None => None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| Some(Some(x)) => Some(x)
| Some(None) => None
| None => None
| Some(Some(x)) => Some(x)
| _ => None

Copy link
Member

@ospencer ospencer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! In the future we might move the Option type into the compiler itself so other compiler types can use/return options (that'll be important when we have optional arguments) but for now this is perfect 👨‍🍳

stdlib/option.gr Outdated
}
}

# TODO: Do you like `andThen` or should it be `flatMap` or both?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would lean towards flatMap, since the "then" part of andThen feels like it could imply that something async is happening.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Plus, flatMap is nice since Options are monads/functors

stdlib/option.gr Outdated
}
}

# TODO: Would be cool to have a recursive version
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the typechecker would let us write one without a custom type to match against :/

The type would have to look like this:

data RecOpt<a> = RecOptCons(Option<a>, RecOpt<a>) | RecOptNone

You could maybe get this to work with higher kinded types, but I'm not sure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not sure how calls to a recursive version would type-check in the first place...

@phated
Copy link
Member Author

phated commented Jun 27, 2020

@belph when I make your suggested changes, I get this warning:

Warning 9: this pattern-matching is fragile.
It will remain exhaustive when constructors are added to type pervasives.Option.

How should I avoid that?

@phated
Copy link
Member Author

phated commented Jun 27, 2020

I made the changes and rebased. We should figure out how to disable that warning for the stdlib maybe?

@ospencer
Copy link
Member

Yeah, I'll make an issue.

@ospencer ospencer dismissed peblair’s stale review June 28, 2020 05:45

The requested changes have been made.

@ospencer
Copy link
Member

I wrote a thing to set the compiler's default warnings, and opened an issue so we can have them be configurable. (I made FragileMatch default to off, and left everything else as on for now.)

@ospencer ospencer merged commit 864e3a6 into grain-lang:master Jun 28, 2020
@phated phated deleted the stdlib-option branch May 30, 2022 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants