From mboxrd@z Thu Jan 1 00:00:00 1970 MIME-Version: 1.0 In-Reply-To: <8b3f8874fbb5ead75e41d071391448e9@quanstro.net> References: <25CF9336-C071-44A5-AB04-6BB042BC5755@kix.in> <8b3f8874fbb5ead75e41d071391448e9@quanstro.net> Date: Thu, 3 Sep 2009 21:18:43 -0700 Message-ID: <3e1162e60909032118h60620d2cj74791672e5f55a5f@mail.gmail.com> From: David Leimbach To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Content-Type: multipart/alternative; boundary=000e0cd59a0ab2c76a0472b8cbb5 Subject: Re: [9fans] "Blocks" in C Topicbox-Message-UUID: 63a4586c-ead5-11e9-9d60-3106f5b1d025 --000e0cd59a0ab2c76a0472b8cbb5 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Sep 3, 2009 at 8:52 PM, erik quanstrom wrote: > > Did you even read the article or any of the examples? There are plenty > > of things that you can "do" with blocks that you can't with just > > function pointers. That's besides the fact that some of them are more > > elegantly expressed with blocks that look sort of ugly with function > > pointers. > > on the other hand, apple says this is illegal > > dispatch_block_t p; > > if(cond){ > p =^ { print("cond\n"); }; > }else{ > p =^ { print("cond\n"); }; > } > p(); > > since the first part is equivalent to > > if(cond){ > struct Block _t = ...; > p = &_t; > } > > intuitive? easy to read? pretty? > > also, if this from david's example is allowed (i'll assume > that the original examples' print(X+y) for int X and y > was a bit of a typo --- i hope!) > > block =^ (int x){ print("%d\n", x ); }; > Wasn't my example... came off an email I cut and pasted from another email after googling around a bit for block stuff. Also I think you mean printf since we're being pedantic? :-) > > that sucker is on the stack. by-by no-execute stack. > how does it get to the stack? is it just copied from > the text segment or is it compiled at run time? > I don't think I posted the whole code, so that's my bad. The X was on the stack to begin with as the first X was an automatic variable in a function. I'd be a little surprised to find an automatic variable in the text segment, but perhaps that's just my not remembering things properly. (didn't mean that tongue in cheek, I don't think about that stuff much these days, as I've spent the last year or so doing Erlang and Haskell.) > > - erik > > --000e0cd59a0ab2c76a0472b8cbb5 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Thu, Sep 3, 2009 at 8:52 PM, erik qua= nstrom <quans= tro@quanstro.net> wrote:
> Did you even read the article or any of the examples= ? There are plenty
> of things that you can "do" with blocks that you can't w= ith just
> function pointers. That's besides the fact that some of them are m= ore
> elegantly expressed with blocks that look sort of ugly with function > pointers.

on the other hand, apple says this is illegal

=A0 =A0 =A0 =A0dispatch_block_t p;

=A0 =A0 =A0 =A0if(cond){
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p =3D^ { print("cond\n"); };
=A0 =A0 =A0 =A0}else{
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p =3D^ { print("cond\n"); };
=A0 =A0 =A0 =A0}
=A0 =A0 =A0 =A0p();

since the first part is equivalent to

=A0 =A0 =A0 =A0if(cond){
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0struct Block _t =3D ...;
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p =3D &_t;
=A0 =A0 =A0 =A0}

intuitive? =A0easy to read? pretty?

also, if this from david's example is allowed (i'll assume
that the original examples' print(X+y) for int X and y
was a bit of a typo --- i hope!)

=A0 =A0 =A0 =A0block =3D^ (int x){ print("%d\n", x ); };

Wasn't my example... came off an email I c= ut and pasted from another email after googling around a bit for block stuf= f. =A0Also I think you mean printf since we're being pedantic? :-)=A0
=A0

that sucker is on the stack. =A0by-by no-execute stack.
how does it get to the stack? =A0is it just copied from
the text segment or is it compiled at run time?

I don't think I posted the whole code, so that's my bad. = =A0The X was on the stack to begin with as the first X was an automatic var= iable in a function. =A0I'd be a little surprised to find an automatic = variable in the text segment, but perhaps that's just my not rememberin= g things properly. =A0(didn't mean that tongue in cheek, I don't th= ink about that stuff much these days, as I've spent the last year or so= doing Erlang and Haskell.)
=A0

- erik


--000e0cd59a0ab2c76a0472b8cbb5--