GNOME Bugzilla – Bug 686562
Cascaded invocations
Last modified: 2018-05-22 14:32:17 UTC
I've been playing around with Dart lately, and one feature it has that I think could be nice for Vala is "cascaded invocations". There is a description at http://news.dartlang.org/2012/02/method-cascades-in-dart-posted-by-gilad.html which does a good job explaining it. Basically, there is an operator, "..", which allows you to chain up multiple operations without having the API return the instance. For example, say you have an API that looks like this: public class Foo { public void one (int arg); public void two (int arg); public int three; } Instead of doing something like this: Foo foo = bar.get_foo (); foo.one (1); foo.two (2); foo.three = 3; You can do something like this: bar.get_foo ().one (1)..two (2)..three = 3;
Nothing about the bug, just an additional info to say where cascading messages were born originally: http://www.inf.ufsc.br/poo/smalltalk/ibm/tutorial/chap3.html#3.70 I love the idea.
Created attachment 263851 [details] [review] Patch to add cascading member access.
I would definitely like to see some unit testing on this, especially with regard to assignment: foo..bar = false ..baz = 'foobar' ..hello ("world") ..bar = true;
Created attachment 277655 [details] [review] More stable patch to add cascading member access Completely reworked the patch. This one is much more simple and stable and supports recursive cascading (such as cascading inside arguments of a method called by cascading).
Created attachment 277656 [details] Cascading member access unit tests I really have no idea how to make unit tests. Is it something like this?
(In reply to comment #5) > Created an attachment (id=277656) [details] > Cascading member access unit tests > > I really have no idea how to make unit tests. Is it something like this? That's a good start, but you should add some assertions to make sure that everything is set to what you expect it to be. Also, please integrate it into Vala's test suite in the tests/ directory.
Created attachment 277659 [details] [review] Support for cascaded member access and unit tests to go along Okay, I think I got it. Here's a patch with both the primary changes and the unit tests.
Created attachment 277661 [details] [review] Changes for both vala and genie to support cascade member access and unit tests Last one, added relevant changes to the genie side. I didn't see any gs files in the tests directory, I'm assuming that we don't have a test suit for genie?
Thanks for your work. However, dealing with cascades at parser level is a trick that does not work :( The problem is that you are modifying the current block code, whereas expressions cannot modify the current block. Example of where this approach is broken: int foo = 10; int meth () { return foo++; } public class Bar { public int field; } void main () { (true) ? (new Bar()..field=meth()) : (new Bar()..field=meth()); assert (foo == 11); } The approach to create a variable for the cascade seems ok, but the whole cascade expression must be a new vala expression. Then, during semantic check, the cascade expression can somehow create a block where to add the cascade variable, so that the following expressions can refer to it.
Created attachment 277774 [details] [review] Support for cascaded member access and unit tests to go along How does this work? Instead of expanding the cascade statements into the current block I separate them in a method, then call that method with the needed arguments. Types where a pain but it looks like I got it working.
Luca, did you ever get a chance to look at the updated patch?
I haven't, but I had a quick look and don't understand why we should add a method to the current class. The implementation should be as follows: - Create an ast node for cascade invocations. - Transform the cascade statement to a block which does multiple operations: first create a temp variable holding the initial value, then do assignments, calls etc.
Created attachment 294723 [details] [review] vala cascade invocations Proposed implementation. Call for testing, thanks :)
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/327.