Stream: ideas

Topic: Unary operator precedence


view this post on Zulip Kilian Vounckx (Sep 04 2024 at 12:42):

With the new purity inference proposal the idea is to have ? be a postfix operator with lower precedence than function calling. I propose we do the same with the prefix operators - and ! to have consistency. Concretely this means something like -foo x is equivalent to -(foo x), instead of (-foo) x which it is now

view this post on Zulip Richard Feldman (Sep 04 2024 at 12:45):

I think that would be useful in the specific case where foo is a function call

view this post on Zulip Richard Feldman (Sep 04 2024 at 12:46):

for example, I think these situations are different:

answer = -foo x
answer = blah -foo x

view this post on Zulip Richard Feldman (Sep 04 2024 at 12:47):

in the first example, it's more useful to parse it as -(foo x) because parsing it as (-foo) x will always result in a type mismatch

view this post on Zulip Richard Feldman (Sep 04 2024 at 12:47):

in the second example, parsing it as blah (-(foo x)) would be strange because it would be introducing a function call where otherwise there wouldn't be one, so I think that one should stay as blah (-foo) x

view this post on Zulip Kilian Vounckx (Sep 04 2024 at 12:54):

I didn't think about the second case. I don't know if having the first become -(foo x) and the second blah (-foo) x would be a real issue though.
That said, how would the following parse with ? in the new proposal?
answer = blah foo? x

view this post on Zulip Kilian Vounckx (Sep 04 2024 at 12:55):

I think it is the same issue again. As long as we are consistent with both I think anything is fine

view this post on Zulip Richard Feldman (Sep 04 2024 at 13:59):

yeah that's a good point! I hadn't thought about what we should do in that scenario

view this post on Zulip Kilian Vounckx (Sep 04 2024 at 14:31):

I think the two options are again

  1. (blah foo)? x
  2. blah (foo?) x

I think 2 would be way more desired. It does need to be handled differently though

view this post on Zulip Kilian Vounckx (Sep 04 2024 at 14:34):

However I feel you wouldn't often have a variable with a result like this. Either it would be handled with for example a when ... is, or the ? operator will have been used before, when foo was defined. If this assumption is correct, then arguments would almost never need a ?. The exception here is when you calculate an argument inline like foo (bar baz?) x). But obviously, the ? must already be in parentheses here


Last updated: Jun 16 2026 at 16:19 UTC