Re: [Vala] Proposal for an improved delegate/lambda handling



Here is another set of proposed syntaxes, so that the post_request() looks like this:

client.post_request("GET ....") {
    debug ("Got Response: %s", response);
} {
    debug ("Got error: %s", error);
}

Another possible syntax would be:

client.post_request("GET ...") {
    (response) => {
        debug ("Got response %s", response);
    }
}

which in the case of a callback+errback would look like:

client.post_request("GET ...") {
    (response) => {
        debug ("Got Response %s", response)
    }
    (error) => {
        debug ("Got error %s", error);
    }
}

This 2nd syntax is actually inspired from Scala. Scala supports currying, as well as optional parentheses for parameter passing,and the ability to substitute parentheses for curly brackets in some place. This set of feature combined together allow building methods and functions that look and feel like control structure. (At least that's what I understood during a short trip to the Scala world).

Thanks for all your feedback.

--
Ali


On Sun, Oct 19, 2008 at 2:00 AM, Christian Hergert <christian hergert gmail com> wrote:
I like it. Might get confusing if someone wanted to mix direct
delegates and anonymous ones though.

-- Christian

On Sat, Oct 18, 2008 at 4:14 PM, Yu Feng <rainwoodman gmail com> wrote:
> On Sat, 2008-10-18 at 22:57 +0200, Ali Sabil wrote:
>> Hi all,
>>
>> I would like to start a discussion about a small syntactic sugar
>> addition to Vala, that would in my opinion slightly improve the
>> situation with asynchronous code. The following code sample should be
>> self explanatory about my proposal:
>>
>> delegate bool HttpResponseCallback (string response);
>>
>> public class HttpClient : Object {
>>     public void post_request (string request, HttpResponseCallback cb)
>> {
>>         cb ("Hello world");
>>     }
>> }
>>
>> // Current State
>> var client = new HttpClient ();
>> client.post_request("GET ....",  (response) => {
>>         debug ("got response %s", response);
>>         return true;
>>     }
>> );
>>
>>
>> // Proposed syntax:
>> // Vala would basically use the "pseudo-lambda" _expression_
>> // following the method call as a parameter to the method call
>> client.post_request("GET ....") (string response) {
>
> What about
> client.post_request("GET ..."):
> (response) => {
>   ....
> },
> (second_callback) => {
>    ....
> };
>
> Adding the colon and commas might ease the parser.
>
>
>>     debug ("got response %s", response);
>>     return true;
>> }
>>
>>
>> I am very open to comments and suggestions.
>>
>> Cheers,
>>
>> --
>> Ali
>>
>> _______________________________________________
>> Vala-list mailing list
>> Vala-list gnome org
>> http://mail.gnome.org/mailman/listinfo/vala-list
>
> _______________________________________________
> Vala-list mailing list
> Vala-list gnome org
> http://mail.gnome.org/mailman/listinfo/vala-list
>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]