onyx-lang icon indicating copy to clipboard operation
onyx-lang copied to clipboard

Feature request: cascade operator

Open stugol opened this issue 9 years ago • 5 comments

Allows calling methods of an object without assigning it to a temporary, and without affecting its eventual lexical value. I know .. already has a meaning, but in this case it would be located at the start of a line.

return MyObject()       -- would return an instance of MyObject, no matter the result of the other calls
   .. some-function()
   .. some-other-function()

Equivalent to:

a = MyObject()
a.some-function()
a.some-other-function()
return a

Maybe also allow it in-line, provided it was separated by a space, to disambiguate it from a range:

o = MyObject() .. some-function .. some-other-function
puts o.class            -- "MyObject"

stugol avatar Feb 22 '16 00:02 stugol

Ah, here's another feature I contemplated implementing :-D

The things is, in LS which I've coded a lot for nodejs work, it's available, but in the end I never found that good a use of it while keeping clarity of intent. It's just so easy to use a temporary like x. And as in the above example, simply do the return afterwards, which would be much clearer and only requiring one additional line.

On a more practical perspective: as you say, the .. wouldn't be a problem practically, as long as it is required to begin on a new line. The one-line example would require the range-operator to always be non-spaced though, which I don't think is good. If using short methods on instances as range parts, it can be desirable to space it for clarity: a.l .. a.r vs a.l..a.r (contrived, of course, but...)

Can you find some examples comparing temporary with the cascade that "really" makes a difference to code clarity, which would convince me (and anyone else)?

I don't want to be discouraging, I really like your ideas and input. Things just has to be "the right thing to do", and that needs some juggling and examination, and who knows, some even better ideas might pop from twisting and turning things.

ozra avatar Feb 24 '16 22:02 ozra

I'm not very good at finding actual, practical examples of why something is useful. I just like syntactic sugar ;)

I didn't even know ranges could have spaces in them.

stugol avatar Feb 24 '16 23:02 stugol

I'm all for sugar - as long as it's warranted :-)

ozra avatar Feb 25 '16 13:02 ozra

LiveScript does cascades:

a = [2 7 1 8]
  ..push 3
  ..shift!
  ..sort!
a #=> [1,3,7,8]

document.query-selector \h1
  ..style
    ..color = \red
    ..font-size = \large
  ..inner-HTML = 'LIVESCRIPT!'

So why not us? ;)

stugol avatar Mar 23 '16 09:03 stugol

I was waiting for that one, haha ;-)

Well, LS gets many things right, not all.

Cascades are definitely still on the discussion table, they just have to be "somewhat proven" as an actual plus. The .. operator could find other better uses down the line, where we'll swear our tongues out for having used it for something that's so easy to "work around". Sugar can cause diabetes: it should be used in the best places for the best dessert. ;-)

ozra avatar Mar 23 '16 12:03 ozra