Zig: Parameters declared with the same name as global variables are conflicting

Created on 14 Mar 2020  路  2Comments  路  Source: ziglang/zig

Hi, i'm quite noobs on the language , and need language lawyers help,
The following:

fn function(types: *MyType) void {}

const MyType = struct {};

var types = *MyType;

pub fn main() void {
    var t: MyType = .{};
    function(&t);
}

because of the same name in the parameter (types) and the name in global scope. are the parameters associated to a global type definition ? this may not scale.

Most helpful comment

Welcome, @frett27. Here are some places you can find other community members to discuss these things with: https://github.com/ziglang/zig/wiki/Community

"no shadowing" is not planned to be changed.

All 2 comments

Newbie here aswell :). Zig doesn't allow shadowing at any level, that's why the compiler issues a "redefinition error".

As for the scaling problems: I don't think this causes any problems.

  1. Global state should be reduced to a minimum on large scale projects anyway, because it messes with testability.
  2. You can use the same name in a different zig file and import the that file. As long as the definition isn't declared pub it won't leak over and therefore won't hide your definition.
  3. Giving a non-type variable the same name as a type variable is super confusing (what's the proper terminology here, lol?). Your global types is a type, while your parameter types is a value of said global types.

Welcome, @frett27. Here are some places you can find other community members to discuss these things with: https://github.com/ziglang/zig/wiki/Community

"no shadowing" is not planned to be changed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jorangreef picture jorangreef  路  3Comments

jayschwa picture jayschwa  路  3Comments

andrewrk picture andrewrk  路  3Comments

andersfr picture andersfr  路  3Comments

andrewrk picture andrewrk  路  3Comments