Skip to content

Should we provide a syntactic shorthand for Core.Optional? #5979

@zygoloid

Description

@zygoloid

Summary of issue:

Should we have a syntactic shorthand for Core.Optional(T)? This type will come up frequently in interop scenarios, as C++'s T* is expected to map into Core.Optional(T*) in Carbon.

Details:

We brainstormed on this during the 2025-07 Carbon summit, but didn't come to a definitive conclusion.

Leading options seem to be:

  • Postfix ?, matching postfix * for pointers, so C++ T* becomes Carbon T*?. This then needs a solution that's not postfix ? for a "try" operator.
  • Prefix ?, leaving postfix ? for a "try" operator. If we do this, we would likely want to make * for pointers prefix instead.
  • A keyword, perhaps opt, either prefix or postfix, perhaps with parens: opt T* or T* opt or opt(T*).

Any other information that you want to share?

For a "try" operator, we've also been considering postfix .try, which would leave syntactic space for postfix ? as a type operator.

Switching to prefix * and ? for types, and postfix * and ? for expressions, would allow us to provide a more consistent operator set with fewer parentheses required, by making ~all of our value-level unary operators postfix. For example, p->x becomes p*.x, and *(*p)->y becomes p**.y*. But this would cost us a lot of our C++ familiarity budget, probably more than we have for Carbon in total.

Metadata

Metadata

Assignees

No one assigned

    Labels

    leads questionA question for the leads team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions