Skip to content

Anonymous (unlabeled) block expressions when there is no ambiguity #5382

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

Closed
gpanders opened this issue May 20, 2020 · 1 comment
Closed

Anonymous (unlabeled) block expressions when there is no ambiguity #5382

gpanders opened this issue May 20, 2020 · 1 comment

Comments

@gpanders
Copy link
Contributor

Consider the following snippet:

const dir = blk: {                                                                                    
    if (std.os.getenv("FOO_DIR")) |dir| {                                                          
        break :blk try std.mem.dupe(allocator, u8, dir);                                              
    } else if (std.os.getenv("XDG_DATA_HOME")) |xdg_data_home| {                                      
        break :blk try std.fs.path.join(allocator, &[_][]const u8{ xdg_data_home, "foo" });           
    } else if (std.os.getenv("HOME")) |home| {                                                        
        break :blk try std.fs.path.join(allocator, &[_][]const u8{ home, ".local", "share", "foo" }); 
    } else {                                                                                          
        return error.NoFooDirFound;                              
    }                                                                                                 
};                                                                                                    
defer allocator.free(dir);         

In this case, there is no ambiguity in the meaning of break within the block, since there is no parent loop. The repeated blk: and :blk labels add code noise; would it be possible to allow for anonymous/unlabeled blocks when there is no ambiguity? Example:

var y = someFunction();
const x = {
    if (y >= 0) {
        break y * y;
    else {
        break 0;
    }
};

(I realize there are better ways to do the above, it's merely a contrived example).

In cases where there is ambiguity (e.g. in a while or for loop), the labels would still need to be explicitly defined.

@Vexu
Copy link
Member

Vexu commented May 20, 2020

Duplicate of #2990

@Vexu Vexu marked this as a duplicate of #2990 May 20, 2020
@Vexu Vexu closed this as completed May 20, 2020
@ghost ghost mentioned this issue Sep 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants