RFC: Make function reassignment distinct #17399
Labels
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone
In the wake of #1717, and in particular of the specific reason that #1717 was rejected, I think, let's lean into that.
The Observation
I think the reason people have an easy time thinking of functions as values, even in the context of Zig, is that they can well be:
Here is a function (the function itself, not a pointer to it) being passed around like any other comptime-only value, with no special annotation like a definition would have. Now, disallowing this would be silly, but the rejection of #1717 mentions that functions having explicit annotation as such at the definition level is actually a big bonus for Zig specifically, which this undermines.
The Solution
The idea is simple:
It's the exact same semantics as the
const
case above, except afn
declaration can only declare a function; meanwhile, aconst
declaration loses that ability. Of course this only applies to functions themselves -- function pointers remainconst
orvar
, andfn
cannot declare them. (Function definitions are unaffected.)Pros
foo
is callable, thengrep 'fn foo'
will find its declaration, whether that be a definition or a reassignment.var
, as could have been done in status quo; as I remember that would have permitted some weirdness for no practical benefit.const
forr--
,var
forrw-
,fn
forr-x
. It doesn't matter but it's neat, maybe.Cons
(
or=
to appear outside the context of a keyword, and that would tell us unambiguously what it is.anytype
identifier. It's not necessarily a huge deal as function pointers still can, but it's something to be aware of.var
if they plan to mutate it.The text was updated successfully, but these errors were encountered: