Frage

I want to implement a language construct similiar to Ruby´s blocks. So

main()
{
    i = 123
    array.function(|x| { puts x + i; }) //the block needs access to the local variable i
}

should compile fine. I looked at the source code for 1 hour but since I´m not that good at C I still have no idea how they implement it. My first thought was it is a nested function which address gets passed as a parameter, but I´m not sure how the nested function has access to the local variable because it seems in most languages this isnt possible. If anyone can give me an idea of how to implement this I would be very glad

War es hilfreich?

Lösung

Here is implementation of code blocks and yields in C, it may help you. http://tilarids.blogspot.com/2012/03/ruby-like-blocks-and-yield-keyword-in-c.html

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top