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

Nil-safe accessors #124

Closed
kengorab opened this issue May 30, 2020 · 0 comments
Closed

Nil-safe accessors #124

kengorab opened this issue May 30, 2020 · 0 comments

Comments

@kengorab
Copy link
Owner

kengorab commented May 30, 2020

I'd like to be able to have a nil-safe accessor operator (?.) to access fields on Optional variables. If the variable is None, the entire rest of the expression is skipped and the result is None; if the value is not None, the next expression in the chain is evaluated. For example:

type Person {
  name: String
}

val people = [
  Person(name: "Ken")
]

people[0]?.name?.toLower() // "ken"

people[0]?.name.toLower()
               ^ Error, no field 'toLower' on String?

There should also be nil-safe accessors for indexing:

val arr = [[0], [1]]
arr[0]?.[1]

which should behave the same way; a None value would be chained along if ever present.

kengorab added a commit that referenced this issue Jun 8, 2020
Addresses #124

- Add lexing of `?.` operator
- Add parsing of `?.` expressions (it creates an `AstNode::Accessor`
whose `AccessorNode` has `is_opt_safe: true`).
kengorab added a commit that referenced this issue Jun 10, 2020
Addresses #124

- Add lexing of `?.` operator
- Add parsing of `?.` expressions (it creates an `AstNode::Accessor`
whose `AccessorNode` has `is_opt_safe: true`).
- Add typechecking of `?.` expressions. `a?.b` will "unwrap" `a` if it's
an optional, and then typechecks the field `b` against it, then re-wraps
it as an optional; if `a` is _not_ optional, it's effectively the same
as `a.b`; a `Token::Dot` is actually emitted instead of
`Token::QuestionDot`, so downstream code will actually compile away the
unnecessary optional-safeness.
kengorab added a commit that referenced this issue Jun 10, 2020
Addresses #124

- Add lexing of `?.` operator
- Add parsing of `?.` expressions (it creates an `AstNode::Accessor`
whose `AccessorNode` has `is_opt_safe: true`).
- Add typechecking of `?.` expressions. `a?.b` will "unwrap" `a` if it's
an optional, and then typechecks the field `b` against it, then re-wraps
it as an optional; if `a` is _not_ optional, it's effectively the same
as `a.b`; a `Token::Dot` is actually emitted instead of
`Token::QuestionDot`, so downstream code will actually compile away the
unnecessary optional-safeness.
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

No branches or pull requests

1 participant