Syntactic Sugar Idea for Go 2.0: until/unless, and postfix conditionals

349 views
Skip to first unread message

Jeffrey Paul

unread,
Nov 2, 2020, 11:52:08 PM11/2/20
to golang-nuts
Hello Gophers,

There's two tiny pieces of syntactic sugar I really miss from a few other languages that I think would add a nice bit of ergonomics and convenience to Go (which I now play as my main) without increasing any magic or spooky action at a distance.

They are:

- postfix conditionals

and

- until (while!) and unless (if!) (which probably also means adding `while`)

This allows for lovely expressions such as the following examples:

i.NotifySomeone() if j.HasExpiredItems()

time.Sleep(1 * time.Second) until thing.IsReady()

b.ReportActivity(e) unless u.HasOptedOut()

q.ProcessItem() while server.Active()

until(time.Now().After(notAfter)) {
// do something while we still can
}

p.processEvents() until p.shutdownRequested

and, my favorite:

panic("can't even") if err != nil

There are, of course, various ways of doing some of this sort of synchronization stuff in these contrived examples using channels/goroutines/timers, but this sort of syntax is quite useful for simple straight-line synchronous code, and, in my view, increases readability without sacrificing anything.

I know that `while` isn't a distinct thing in Go, and for consistency's sake, this might necessitate adding such a construct as well (if you can use it as a postfix conditional, you should probably be able to use it as `while(cond) {}` too).

What do you think? I really miss this syntax from other languages. It's been in Perl and Ruby for ages, and, more recently, CoffeeScript had it for a moment, but it didn't make it over into ES with the other notable features from it. I think it's a lovely convenience without changing the operation of the language itself.

Best,
-sneak

--
Jeffrey Paul - sn...@sneak.berlin
+1 312 361 0355 (voice, sms, Signal)
5539 AD00 DE4C 42F3 AFE1 1575 0524 43F4 DF2A 55C2

Dan Kortschak

unread,
Nov 3, 2020, 12:10:16 AM11/3/20
to golang-nuts
My first professional programming language was Perl, decades later I
still wake up in a sweat thinking about post-fix conditionals and the
'unless' conditional.

Please no.

Tyler Compton

unread,
Nov 3, 2020, 12:23:25 AM11/3/20
to Dan Kortschak, golang-nuts
I don't think I'm personally sold on this proposal either, but I'm curious what bad experiences you've had with post-fix conditionals. I haven't personally used a language with post-fix conditionals and it sounds like that might be to my benefit :)

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/99896e4fcd832c0003bb5e23ffd1a72837ea49bd.camel%40kortschak.io.

Dan Kortschak

unread,
Nov 3, 2020, 12:33:50 AM11/3/20
to golang-nuts
There are two parts. The worse part is the negative conditional
(unless), which has the problem that humans are bad at negations;
nearly always when there is a complex condition with an "unless", it
needs to be mentally refactored into an "if !" (when working through
other people's bugs, I invariably — at least temporarily — inverted the
condition and replaced the "unless" with an "if").

The post-fix conditional syntax says a whole heap of stuff that's going
to happen, and only when you get to the end of the line do you see that
it might not.

Putting a single positively oriented syntax, at the front of
conditional blocks greatly simplifies the thinking about what is going
to happen in a section of code.

Tyler Compton

unread,
Nov 3, 2020, 2:35:51 AM11/3/20
to Dan Kortschak, golang-nuts
Thanks, Dan. Those seem like well-reasoned points.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Jan Mercl

unread,
Nov 3, 2020, 4:06:39 AM11/3/20
to Dan Kortschak, golang-nuts
On Tue, Nov 3, 2020 at 6:09 AM 'Dan Kortschak' via golang-nuts
<golan...@googlegroups.com> wrote:

> My first professional programming language was Perl, decades later I
> still wake up in a sweat thinking about post-fix conditionals and the
> 'unless' conditional.

I don't miss MUMPS post conditional either.
Message has been deleted

Marcin Romaszewicz

unread,
Nov 3, 2020, 2:47:15 PM11/3/20
to Amnon Cohen, golang-nuts
Indeed!

Go has while loops :)

for {
  // do some stuff
  if !condition { break }
}

instead of
{
  // do some stuff
} while condition

They are identical functionally, so why bother with the second syntax? You then get into arguments about which one to use.

-- Marcin

On Tue, Nov 3, 2020 at 11:31 AM 'Amnon Cohen' via golang-nuts <golan...@googlegroups.com> wrote:
Languages often have a whole variety of loop constructs.
C has for loops as well us while loops.
Go adopts the principle from the Zen of python:
There should be one-- and preferably only one --obvious way to do it.
It takes a minimalist approach - there is only one looping construct: the for loop.

This is an excellent decision, IMO.



Red Sift is the power behind OnDMARC and OnINBOX.

You can find us at 21A Noel Street, 4th Floor, London, W1F 8GR.


Red Sift is a limited company registered in England and Wales. Registered number: 09240956. Registered office: Kemp House, 152 City Road, London, EC1V 2NX.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages