Zig: fix inability to interact with C ABI symbols with underscore name (`_`) by making it a keyword

Created on 28 Nov 2018  路  3Comments  路  Source: ziglang/zig

In Zig one can use @"this syntax" to name an identifier any arbitrary sequence of bytes, including keywords. The purpose of this is to allow interoperability with any C ABI names, even names that conflict with Zig keywords.

However one cannot do this with @"_" because this is semantically equivalent to _, because _ is an identifier, not a keyword. Meaning that Zig will treat @"_" just like _, not allowing variables and functions with this name. And thus it is impossible to have an external variable named _ even though that is legal in C.

This proposal is to change _ into a keyword. Although it's technically a breaking change, it shouldn't break anyone's code in practice, but it does require changes to the grammar and parser, e.g. to implement #1797.

breaking proposal

Most helpful comment

An alternative solution would be to add a linksymbol property to variables. Then you could just do:

var c_underscore linksymbol("_") = ...

All 3 comments

One question I want to answer before deciding on this issue is whether the same should be done to all primitive types, including, for example, i32. It feels a bit strange, but I'm leaning towards yes. I'll note that this conflicts with #2926.

An alternative solution would be to add a linksymbol property to variables. Then you could just do:

var c_underscore linksymbol("_") = ...

After wasting a bunch of time coming up with alternatives to 'type' I'm starting to heavily lean in favor of turning _ and all primitive types into keywords.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bheads picture bheads  路  3Comments

bronze1man picture bronze1man  路  3Comments

dobkeratops picture dobkeratops  路  3Comments

fengb picture fengb  路  3Comments

jayschwa picture jayschwa  路  3Comments