Skip to content

idea: postfix pointer-dereference operator #10011

Closed
@1fish2

Description

@1fish2

This is something of a wild idea: Use a postfix operator to dereference a pointer (as in Pascal) for fewer mistakes:

(*rect).area() becomes rect~.area()

That reads left-to-right with nary a precedence mistake.

Although Rust’s auto-dereference feature and type checker will sometimes catch the mistaken *rect.area(), it's good to just fix the failure mode. (All security holes in the field got past the type checker and the unit tests.)

The disadvantages are (1) the changeover work, and (2) being different than C/C++. C defined it that way to reuse the expression parser to help fit the compiler on a 16-bit CPU.

David Piepgrass points out that if pointer dereference were a suffix operator, it would have to be changed from * to (e.g.) ~. Why? Because if foo* means "dereference foo" then it becomes ambiguous whether foo * - bar means (foo*)-bar "dereference foo and subtract bar" or foo*(-bar) "multiply foo by negated bar". Due to this ambiguity, it is difficult for a language to simultaneously have

  1. Operators that can be prefix or infix, and
  2. Operators that can be infix or suffix.

To avoid the ambiguity, one can introduce an operator like ~ that is prefix or suffix but never infix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions