Rfcs: Parse `||` or `| |` as empty closure in expression position

Created on 28 Apr 2017  路  5Comments  路  Source: rust-lang/rfcs

I don't know much about grammars, but would it be possible and desirable to parse || as an empty closure? To avoid confusion with the "or" operator, we could use a space like | |? Right now we have to do || { } for an empty closure which is tough to read. This would be convenient for example if a function takes a callback and we want to provide no callback.

T-lang

Most helpful comment

I'd much rather have an stdlib function nop in scope and use that one. Some arbitrary symbol magic sounds like it makes for unreadable and easily breaking code.

All 5 comments

@leodasvacas,
As for me || is harder to read as an empty closure.

I'd much rather have an stdlib function nop in scope and use that one. Some arbitrary symbol magic sounds like it makes for unreadable and easily breaking code.

Note that you can just declare nop in your own in fewer characters that it would take you to write a use to get it out of the stdlib. So I suggest you simply write fn nop() {} somewhere in your code.

use std::nop;
fn nop() {}

fn foo<F: Fn()>(_: F) {}
fn bar<F: FnMut()>(_: F) {}
fn bak<F: FnOnce()>(_: F) {}

fn main() {
    foo(nop);
    bar(nop);
    bak(nop);
}

Why not just use {} to present the empty clousure? This is an anonymous code block anyway, and supposed to do nothing. And furthermore we could let the anonymous code block present any clousure that does not accept parameters.

Clearly we don't want this, thanks to those who gave feedback!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

burdges picture burdges  路  3Comments

torkleyy picture torkleyy  路  3Comments

mqudsi picture mqudsi  路  3Comments

clarfonthey picture clarfonthey  路  3Comments

3442853561 picture 3442853561  路  4Comments