betaveros / noulith

*slaps roof of [programming language]* this bad boy can fit so much [syntax sugar] into it
1.13k stars 20 forks source link

Indexing precedence #9

Closed max-sixty closed 1 year ago

max-sixty commented 1 year ago

I'm not sure whether you're here to answer questions — no worries at all if you're not.

Does this fail because indexing is lower precedence than a function call?

X := [1,2,3];

print X[0];

# ERROR: type error: Chain cannot use nonblock in operator position: [1, 2, 3]
# print X[0];

#    at op 1/1 (14:7-8)
#    at ;-sequence(2/4) (12:13-18:9)

But this seems to work:

X[0].print;
betaveros commented 1 year ago

Hi! The problem is that print X[0] parses as calling the binary operator X on the operands print and [0], much like 1 .+ [0]. Generally, mere juxtaposition for calling a function with one operand is finicky. I'll think about how to make the error message better.

max-sixty commented 1 year ago

That makes sense, thanks

tom-huntington commented 1 year ago

@max-sixty The best way to this is

print! X[0];

! is syntax that's spiritually sort of like what Haskell's $ lets you write. It's as tight as an opening parenthesis on its left, but performs a function call that lets you can omit the closing one up to the next semicolon or so. f! a, b is f(a, b).