Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit () in chaining #1897

Closed
ghost opened this issue Nov 27, 2011 · 3 comments
Closed

Omit () in chaining #1897

ghost opened this issue Nov 27, 2011 · 3 comments

Comments

@ghost
Copy link

ghost commented Nov 27, 2011

I usually do like this:

Db.fetch()
  .start("n=node(2)")
  .match("(n)-[r:KNOWS]->(c)")
  .return "n"
  .on (data) -> console.log data # omitting () since it's the last method call

Make cleaner code:

However, since I am using the dot (.), a new line and indentation, couldn't all the () be removed like this:

Db.fetch
  .start "n=node(2)"
  .match "(n)-[r:KNOWS]->(c)"
  .return "n"
  .on (data) -> console.log data

Often when we do chaining we will begin each method call in a new line and indent so the methods are aligned.

Change faster:

And since sometimes the method calls' position change the logic, I move them around.

Db.fetch()
  .on (data) -> console.log data # will break
  .start("n=node(2)")
  .match("(n)-[r:KNOWS]->(c)")
  .return("n")

But this wouldn't work since the last method call doesn't need a () so when I moved it up I now have to add the ().

This is forcing me to add () to the last call every time so we can change the order quickly. There are better examples where you wanna move the lines up and down to adjust the logic.

Single line:

I think using the dot (.) is sufficient to tell it's a chaining call. This could even be used as a one liner?

Db.fetch .start "n=node(2)" .match "(n)-[r:KNOWS]->(c)" .return "n" .on (data) -> console.log data

Parseable:

If somehow the dot isn't enough we have the new line and indentation as a confirmation that it's a chaining method call.

Another example:

I usually write a method call with arguments like this:

db.print "hello"

But then I have to chain and have to go back and change it to:

db.print("hello").print "world"

And again:

db.print("hello").print("world").print "again"

It would be better to just allow me to:

db.print "hello"
  .print "world"
  .print "again"

db.print "hello"
  .print "again"
  .print "world"

or as a one liner:

db.print "hello" .print "world" .print "again"

Issues:

In these two examples you can clearly see why I am "forced" to just use () in the last method call in the beginning so I don't have to add them later when I need to chain. But that kinda defeats the purpose of not having to use () in the last method call. "You can, but I wouldn't". Also I can't omit the () in the middle layer method calls.

Benefits of the nicer syntax:

  • more clean code
  • type/chain faster
  • don't have to change anything already there

I think this is doable and will give a better user experience.

Thoughts?

@erisdev
Copy link

erisdev commented Nov 27, 2011

This has been brought up a few times before. I think #1495 has the most recent discussion of the issue. #1889 has a similar proposal that would probably include a feature like this.

There was another issue that suggested the one-liner form, but I can't find it #1407. I'm not sure I'm quite so keen on that form, though.

@TrevorBurnham
Copy link
Collaborator

Right, there are two separate issues that are open right now with proposals that could be adopted independently or combined:

  1. Unnecessary parentheses in function calls. #1407: Allow . after whitespace to close implicit parentheses, e.g. foo x .bar() would become foo(x).bar()
  2. Improve chaining syntax #1495: Multiple proposals, but notably @raganwald's.

If both proposals were adopted, you would indeed be able to write

db.print "hello"
  .print "world"
  .print "again"

So, I'm closing this issue as a duplicate, but I encourage you to express your support at the above issues.

@ghost
Copy link
Author

ghost commented Nov 28, 2011

Ok I will

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants