evhub / coconut

Simple, elegant, Pythonic functional programming.
http://coconut-lang.org
Apache License 2.0
4.04k stars 120 forks source link

Implicit application doesn't work if the function's first token is a string literal #821

Closed GolfingSuccess closed 7 months ago

GolfingSuccess commented 7 months ago

This snippet doesn't work as I'd expect it to:

x = 'b'
print('abc'.find x)

I would expect it to print 1; instead, it fails like this:

$ cat test.coco
x = 'b'
print('abc'.find x)
$ ./coconut test.coco
Compiling         test.coco ...
CoconutParseError: parsing failed (line 2 in 'test.coco')
  print('abc'.find x)
Coconut exiting with error: CoconutParseError
$ ./coconut --version
Coconut: Version 3.0.4 running on Python 3.10.12 and Cython cPyparsing v2.4.7.2.3.2
$

An interesting behavior appears if I try it in the REPL:

Coconut Interpreter v3.0.4 (Python 3.10):
(enter 'exit()' or press Ctrl-D to end)
>>> x = 'b'
>>> print('abc'.find x)

CoconutParseError: parsing failed (line 1)
  print('abc'.find x)
  \~~~~~~~~~~~~~~~~^
>>>

Notice the empty line above the error: it actually waited for me to "continue" writing, so I just pressed Enter and the error appeared.

If I add parentheses, as in print(('abc').find x) or print(('abc'.find) x), it does print 1.

Edit: This blocking behavior also occurs if I type something nonsensical, like ('abc'.'find') x, or just 'abc'.'find' (that last one spits a CoconutSyntaxWarning before blocking, but then fails anyway).

evhub commented 7 months ago

I had disabled this exact case for some reason because it was causing some parsing inconsistencies and I didn't think anyone was using it, but reenabling it now doesn't seem to break any tests anymore, and I don't think this is super confusing or anything, so I'll bless this syntax as allowed. This should be reenabled as of coconut-develop>=3.0.4-post_dev16 (pip install -U coconut-develop>=3.0.4-post_dev16 to get the fix).

GolfingSuccess commented 7 months ago

Ah, well to be fair I've come across it due to code golf, so I'm not the perfect example of "everyday usage"... 😂 Thanks a lot!!!