Skip to content

Switch to go's casting syntax #2231

@brson

Description

@brson
Contributor

Go does:

x.(type)

People like it. It would make our casting precedence much clearer since it would be the same as field/method access (our as precedence has changed twice in the last year).

Activity

Dretch

Dretch commented on Apr 18, 2012

@Dretch
Contributor

I am a person and I quite like the appearance of the existing syntax, though I concede it suffers from precedence issues just like other infix operators.

catamorphism

catamorphism commented on Apr 19, 2012

@catamorphism
Contributor

I don't like it. The semantics of field access don't have much to do with the semantics of casting, and the syntax makes it look like they do. Nothing wrong with as as far as I'm concerned (I always write copious numbers of parentheses when I'm not sure about precedence in my code anyway).

graydon

graydon commented on Apr 19, 2012

@graydon
Contributor

Yeah, I was on the "less keywords is better" campaign the other day but I've done some doodling with code that uses 'as' and it seems to read almost strictly worse with .(type). Not sure why. Visually cluttered or something.

I agree the precedence issue on as is a bit of a problem. Or rather, the fact that as introduces a clear switch-in from the expression grammar to the type grammar, but the switch out only comes when we "finish" reading a type. For types with suffixes, this makes for an unfortunate interface point between the two grammars. Having some kind of terminator for a cast's type sub-expression would be better. I'd almost prefer as::<T>(foo) or something.

(I guess april is syntax month, eh?)

brson

brson commented on Apr 19, 2012

@brson
ContributorAuthor

I'm convinced that as reads nicer. There don't seem to be any firm proponents of this syntax. Closing.

added a commit that references this issue on Sep 22, 2022
added a commit that references this issue on Nov 6, 2022
added a commit that references this issue on Jan 6, 2023
added a commit that references this issue on Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-grammarArea: The grammar of Rust

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @graydon@brson@catamorphism@Dretch

        Issue actions

          Switch to go's casting syntax · Issue #2231 · rust-lang/rust