RFC: `foo = 42`
SYNOPSIS
A new declarator:
foo = 42
Before you go with your immediate gut instinct and start formulating a reply, please take a moment to do something else to clear those initial emotions, and read the rest of this proposal. Thanks!
DETAILS
Currently a program with just:
foo = 42
yields:
SORRY! Error while compiling
Preceding context expects a term, but found infix = instead.
Did you make a mistake in Pod syntax?
What if instead it did an equivalent of this:
constant foo = 42
?
----
In existing Raku code, if foo is already declared in the lexical scope you'll get the same error unless foo has been declared as a term (sub term:<foo>).
In the latter case you'll still get an error (though at run-time) unless the term returns an assignable container.
In the latter case, if someone isn't aware of this foo = ... declarator syntax I'm proposing, it means they might accidentally write foo = 42 thinking they're creating a new foo "variable" when they're actually modifying an existing one.
I currently view this latter case as this proposal's biggest downside.
But:
- I'm pretty confident it's a rarity -- each of the "latter case" scenarios is a small subset of the previous scenario.
- If this proposal works as I hope it will, it'll be widely used so ought not lead the above problem/downside scenario to being a surprise.
- Presuming it's introduced at a language version change (
Eid), it'll get widely talked about, again reducing the chance it'll be a surprise. - Perhaps it can be semi-mitigated by introducing a warning for a first use of
foo = ...where thefoois a term.
Overall I don't currently view this downside as a deal-breaker.
----
I've been mulling this for years as part of thinking about how to introduce better support for immutability into Raku. To the degree this idea is about immutability (it's also about some other things I'll get to later), this part is about ergonomics. I'm proposing this declarator is a "moral" equivalent of this:
constant foo = immutable ...
where immutable is (conceptually at least) a function that does one or other of:
- Checks its RHS is entirely immutable, for some definition (TBD) of "checks" and of "entirely immutable". And, if the check fails, then something appropriate happens, eg it throws an exception.
or:
- The same, except that it both coerces and checks its RHS. This might help reduce an awkward problem Raku faces when it "properly" introduces immutable types, to wit, what about literals? If
foo = ...coerces, well, maybe that'll help.
Again, please consider keeping your open mind engaged, and reading on and mulling all I say in this comment before starting to formulate your overall response to this proposal / Request For Comments.
----
Brief tangent:
I also have ideas that are the flipside of the mutability coin, ways in which I see sigils becoming a remarkably strong and generally popular positive, even among some Pythonistas by virtue of them conveniently marking mutability.
But I think for now it's better that I keep the focus in this opening proposal comment on identifiers without sigils, without yet discussing how that could elegantly dovetail with generally using sigils to mark mutability.
----
Putting aside the topic of immutability/mutability and its ergonomics, another reason I arrived at this foo = ... syntax is my thoughts and feelings about sigils.
I love (Raku's strong support for) sigils.
I hate (Raku's weak support for high signal / low noise) sigils.
And, vastly more important than my loves/hates, I think and feel that vast swathes of the potential Raku user market dislike them and/or don't see their point.
What about me? More to the point, what about these people?
I've read Rakoons saying that if you don't like sigils you won't like Raku. I don't buy that argument, partly because I am a Rakoon, and partly because, while I see their upsides, I see their downsides too.
I've felt for over 20 years that Raku would best not ignore these problems forever. And indeed, Larry stepped in this direction with my \foo = ... and sub foo (\foo) .... These are much better than nothing. But they fall far short of the radical transformation in perception and reception I imagine Raku can evoke if it introduces a few (I hope) simple "tweaks", with the above described foo = ... being one of three things I have in mind.
----
Another reason I think this simple declarator is worthwhile is use in one liners. No need for no strict, and fighting with shell escaping of sigils -- just write foo = ....
----
A final thought for this introductory comment.
(I think that) all of Raku's identifier declarators are subject to shadowing.
For reasons I'm not going to try rationalize in this opening comment, I'm thinking this foo = ... idea will work best if it's non-shadowing and non-shadowable.
----
Thoughts? Feelings? Intuitions? Problems you see / forsee?
Posted Jan 26, 2022, 1:59 PM UTC
The problem is what if it was actually a typo.
The correct course of action would be to produce a compilation error. Even if it was the wrong error.
There is a reason that Raku forces declarations to be explicit. It's because of all the errors caused by them being implicit by default in Perl over the years.
Hi Brad,
Thanks for responding. :)
I'm well aware of that argument, and am happy it's on the table.
Ooc, did you read the whole proposal? If so, and you understood this declarator is designed to display a compile-time error if someone tries to use it to change the value of a variable, do you think people will use it to try to change the value of a variable?
Are you planning on removing functionality from the language?
Because you'd have to make it so that only the new feature can be written that way without sigils.
One of the great design features of Raku is that there are relatively few special cases to remember.
By doing the above you are going to force people to have to alias a non-sigilled to a sigilled variable in order to assign to it.
A better option in my opinion would be to make the
myoptional inmy \foo =...No.
That's not true.
would be fine.
Of course, I get that the dangerous and ugly idiom you used would lose typo protection.
But is that such a bad thing?
Indeed, shouldn't we be considering deprecating it?
BTW, did you know:
I (afaik) invented the idiom?
It's not threadsafe?
Yes.
But that idiom is absolutely nothing to do with great design, but instead just my warped mind when I invented it.
And to make it threadsafe one has to write:
Imo, while my original idiom was bad, that is downright ugly.
Conversely, I like the way non-sigil'd identifiers are generally bound to fixed values, and sigil'd identifiers are generally bound to variable ones, and I'd like Raku to evolve toward further normalizing that rather than maintaining error prone oddities like the idiom I invented.
Why not just use a sigil'd variable in the first place?
I mulled that for about a year, but that's different semantics than what I proposed in my OP.
What you suggest would still leave it ambiguous whether
foowas immutable or not. One could write:Both
fooandbarwould be mutable.(BTW, I note you haven't said whether you read my OP; I would love it if you did read it and/or ask that you at least reveal if you've read it or not. If it was too long, fair enough, but maybe it was too short?)