Alternative syntax for working with enums

I'd be cautious of doing this. It starts to sound a lot like:

// legacy javascript
with foo {
   bar = "Hello";
}

which was found to be a huge foot-gun.

NOTE: similar "with ..." scoping has been present in many languages, and as far as I can recall, it was always found to be a foot-gun and everyone regretted having it in the language and it was eventually "deprecated".

If we did something like this for Rust, we'd have to be 100% sure it wasn't going to create problems, now or in the future, ever (which is not a bar that I think you could ever vault).

1 Like

with is only a footgun in JS because of JS-specific reasons: lack of static typing, which makes it JIT-unfriendly and can create global variables by accident. You can't create a global variable by accident in Rust, so this wouldn't apply.

1 Like

More generally, the problem with with is that it introduced dynamic scoping. i.e. the meaning of a name/identifier has to depend on what runtime values get passed into a block of code. That is indeed a giant mess no one wants in their languages any more.

But AFAICT nobody has suggested any enum syntax that would bring in truly dynamic scoping.

I don't see patterns mentioned in this thread, but I think the syntax would be very useful for matching enum variations, like Enum::{A | B | C}. I posted that as a separate idea over here.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.