Zig: Proposal: Go like declaration block

Created on 14 Oct 2020  Â·  9Comments  Â·  Source: ziglang/zig

It would be nice if Zig has Go like declaration block syntax.

proposal

Most helpful comment

Adding declaration blocks like this will make it significantly harder to search for where something is defined without an editor that supports jump to definition. Currently, you can search in project for const [name] = and that will find all definitions of that name (excluding functions but functions will use the same syntax eventually). Declaration blocks will mean you have to search for [name] = which can also catch assignments and variables that start with the same name instead of just declarations.

Also imo this would go against the zig zen

  • Only one obvious way to do things and
  • Focus on code rather than style.

All 9 comments

What syntax is that? Your proposal should explain with examples and a rationale.

const (
    Foo = 1
    Bar = 2
)

This is often used when defining a collection of constants (e.g. error codes).

as a gopher myself, i approve this message :D

Do you have any examples of this syntax being useful? The example jayschwa gave seems like it could be written as ↓ instead, and be more understandable and take fewer lines of code

const Foo = 1;
const Bar = 2;

Do you have any examples of this syntax being useful?
Once you have more than one constant or variable, you will start doing this automatically. You just have to try it out for a while. Although this is common mostly/only in global scope, not in functions.

Here's how Go declares some of the linux error codes in one const block: https://github.com/golang/go/blob/e4ec30965b9ca629922e83b8d335224ae4bdf062/src/syscall/zerrors_linux_amd64.go#L11-L133

Here's how zig currently does it: https://github.com/ziglang/zig/blob/bf8cc73cf397338e24183b24cd4ef2a698160877/lib/std/os/bits/linux/netlink.zig#L9-L127.

That could be reduced to;

pub const = (
/// Routing/device hook
NETLINK_ROUTE = 0;

/// Unused number
NETLINK_UNUSED = 1;

/// Reserved for user mode socket protocols
NETLINK_USERSOCK = 2;

/// Unused number, formerly ip_queue
NETLINK_FIREWALL = 3;

/// socket monitoring
NETLINK_SOCK_DIAG = 4;

/// netfilter/iptables ULOG
NETLINK_NFLOG = 5;
)

I'm neither pro nor anti this proposal.

Adding declaration blocks like this will make it significantly harder to search for where something is defined without an editor that supports jump to definition. Currently, you can search in project for const [name] = and that will find all definitions of that name (excluding functions but functions will use the same syntax eventually). Declaration blocks will mean you have to search for [name] = which can also catch assignments and variables that start with the same name instead of just declarations.

Also imo this would go against the zig zen

  • Only one obvious way to do things and
  • Focus on code rather than style.

@komuw i think your example for both Go and Zig should be an enumeration type:

pub const Netlink = enum (
    /// Routing/device hook
    route = 0,

    /// Unused number
    unused = 1,

    /// Reserved for user mode socket protocols
    usersock = 2;

    /// Unused number, formerly ip_queue
    firewall = 3,

    /// socket monitoring
    sock_diag = 4,

    /// netfilter/iptables ULOG
    nflog = 5,
};

@komuw i think your example for both Go and Zig should be an enumeration type:

pub const Netlink = enum (
    /// Routing/device hook
    route = 0,

    /// Unused number
    unused = 1,

    /// Reserved for user mode socket protocols
    usersock = 2;

    /// Unused number, formerly ip_queue
    firewall = 3,

    /// socket monitoring
    sock_diag = 4,

    /// netfilter/iptables ULOG
    nflog = 5,
};

in agreement

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrewrk picture andrewrk  Â·  3Comments

jayschwa picture jayschwa  Â·  3Comments

bheads picture bheads  Â·  3Comments

dobkeratops picture dobkeratops  Â·  3Comments

andrewrk picture andrewrk  Â·  3Comments