The Crystal Programming Language Forum

Captured blocks and "with"

Hello! Now, it’s not possible to use `with [something] yield/call" on captured blocks. I can understand, that you can’t inject “anything” to the captured block in compiled language, but isn’t possible to inject “default self target” at the moment, when we are capturing the block?

“with” is good for DSLs.

Thanks!

I don’t think it can. Actually is another reason for RFC: `with ... yield` replacement leave the decision of the DSL-ish on the call-site instead of the implementation might be the way to allow captured blocks in DSL.

“Oops! That page doesn’t exist or is private.”

But I will follow your advice about “call-site”! Thanks.

Actually, something like that might be possible to do. Kotlin supports it: https://kotlinlang.org/docs/reference/lambdas.html#function-literals-with-receiver

What we’d need to do is allow a new type of Proc that has an implicit receiver, where calling methods inside it will first search in that receiver.

It’s possible, but it’s pretty complex to implement.

1 Like

link fixed

1 Like