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

Have some form of destructing if #1529

Closed
kevina opened this issue Jan 15, 2012 · 10 comments
Closed

Have some form of destructing if #1529

kevina opened this issue Jan 15, 2012 · 10 comments
Labels
A-frontend Area: frontend (errors, parsing and HIR) C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@kevina
Copy link
Contributor

kevina commented Jan 15, 2012

Having to constantly write code like this:

alt optional_value {
  some (value)  { do something; }
  _ {/*do nothing*/}
}

Is unnecessary verbose in my opinion, and gets annoying to have to constantly write. I would like to be able to say something like:

if (let some(value) = optional_value) {
  do something;
}

Although it doesn't have to be that exact syntax.

@jckarter
Copy link

Could you achieve this with a HOF?

    if_some(optional_value) {|value|
        do something;
    }

@kevina
Copy link
Contributor Author

kevina commented Jan 15, 2012

For the special case of the option::t type sure, but not as a general mechanism when you only care about checking for one of any number of possible enum values.

@kevina
Copy link
Contributor Author

kevina commented Jan 16, 2012

Rust does have an if_some function but it oddly named as may.

@graydon
Copy link
Contributor

graydon commented Feb 15, 2012

if let is not the weirdest combination for a single refutable pattern. But I think this is pretty low priority, as it's perfectly achievable with alt already, and only saves 1 line of code.

(To be fair, marijn just added alt check because that same one line -- _ { } -- was irritating him too much.)

@catamorphism
Copy link
Contributor

Macros?

Also, https://mail.mozilla.org/pipermail/rust-dev/2012-April/001636.html may be relevant. The "brace-free" syntax that @pcwalton suggested in that thread may help with this problem.

@kevina
Copy link
Contributor Author

kevina commented Apr 12, 2012

On Thu, 12 Apr 2012, Tim Chevalier wrote:

Macros?

I am proposing new syntax. As Rust syntax is not at the moment extensible, I do not see how a macro will really help.

Also, https://mail.mozilla.org/pipermail/rust-dev/2012-April/001636.html
may be relevant. The "brace-free" syntax that @pcwalton suggested in
that thread may help with this problem.

I do not see how that can help.

@catamorphism
Copy link
Contributor

I mean that having to write the alt might be slightly less annoying if the braces weren't required. It could potentially fit on one line that way.

@kevina
Copy link
Contributor Author

kevina commented Apr 12, 2012

Yes it might. But the problem is with the empty default case. I always fell obligated to have some sort of comment in the default case to make it clear that it was meant to be empty. With a destructing if, the intention is clear.

@catamorphism
Copy link
Contributor

Ok. I see how this could be useful, but right now we're generally trying not to add more features. Leaving it open in case anyone else has insight.

@pcwalton
Copy link
Contributor

The right solution here is to implement macros, and then implement your feature on top of that. I'd rather not add features that we then remove later in favor of macros because Rust macros are hard to use right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-frontend Area: frontend (errors, parsing and HIR) C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

5 participants