Skip to content

Clarify blocks being statements in doco #14640

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

Conversation

zenspider
Copy link

As decided here: #9758 (comment)

example of expression/statement disparity:

const lol: u8 = if (args.len == 2)
    3
  else v: {
    std.debug.print("logging info", .{...});
    break :v 5;
};

@@ -4034,7 +4034,7 @@ test "access variable after block scope" {
x += 1;
}
{#code_end#}
<p>Blocks are expressions. When labeled, {#syntax#}break{#endsyntax#} can be used
<p>Blocks are statements, not expressions. When labeled, {#syntax#}break{#endsyntax#} can be used

This comment was marked as outdated.

This comment was marked as outdated.

Copy link
Contributor

@leecannon leecannon Feb 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not true that unlabelled blocks are always statements as they can be used on the right side on an assignment and result in a type of void

It is also not true that labelled blocks are always expressions, the label can be used only for break and continue control flow without any return value.

pub fn main() void {
    const a = {
        // this block is unlabelled but it is an expression and it evaluates to `void`
    };
    @compileLog(@TypeOf(a));

    blk: {
        // this block is labelled but is not an expression
        break :blk;
    }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I'm very new to Zig so this may be a silly question, but is the block evaluating to void construct useful? Writing something like that seems like a mistake on the programmers side (and the compiler shouldn't allow it).

Also please take my comments with a grain of salt, I'm very new at Zig. Actually @zenspider was helping me try to understand why my if expression wasn't working, and it turned out to be due to the fact that blocks aren't expressions (IOW const x = if () { ... } is very different than const x = if () ..., and I didn't understand that)

Copy link
Contributor

@mitchellh mitchellh Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I'm very new to Zig so this may be a silly question, but is the block evaluating to void construct useful? Writing something like that seems like a mistake on the programmers side (and the compiler shouldn't allow it).

Its the only way to initialize a void field. This is common practice in particular if you're using comptime to turn a field "on" or "off". i.e.:

const Widget = struct {
  const KnobType = if (builtin.target.isDarwin()) u64 else void;
  const knob_default = if (builtin.target.isDarwin()) 0 else {};

  knobs: KnobType = knob_default,
}

A void field takes up no space.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it is one of two ways to initialize a void value. The second way is the void literal 'void{}'.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I'm very new to Zig so this may be a silly question, but is the block evaluating to void construct useful? Writing something like that seems like a mistake on the programmers side (and the compiler shouldn't allow it).

Its the only way to initialize a void field. This is common practice in particular if you're using comptime to turn a field "on" or "off". i.e.:

const Widget = struct {
  const KnobType = if (builtin.target.isDarwin()) u64 else void;
  const knob_default = if (builtin.target.isDarwin()) 0 else {};

  knobs: KnobType = knob_default,
}

A void field takes up no space.

Awesome, this makes a lot of sense. Thank you for taking the time to explain, I really appreciate it!!

@andrewrk andrewrk closed this Jun 18, 2023
@kevin-neel-oracle-com
Copy link

I'm admittedly a little new to github. I wonder if this is committed into master, why is this change not reflected in https://ziglang.org/documentation/master/#Blocks (nor 0.13.0)?

@nektro
Copy link
Contributor

nektro commented Nov 13, 2024

it was not committed

@kevin-neel-oracle-com
Copy link

What does this mean?

Clarify blocks being statements in doco
zenspider
zenspider
committed
on Feb 13, 2023

@kevin-neel-oracle-com
Copy link

Does it mean committed into his fork?

@kevin-neel-oracle-com
Copy link

Because I do find the current wording quite confusing. Maybe something like

Blocks are expressions. They return void unless you use break :labl...

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

Successfully merging this pull request may close these issues.

9 participants