Have with-output-to-string and friends take a body, not a thunk.
- Have
with-output-to-stringand friends take a body, not a thunk. The current form is better namedcall-with-output-to-string, in the general pattern ofwith-xbeing syntax andcall-with-xbeing a procedure.
Extracted from the old racket2 wiki. #33
More generally, is it good to keep the with-x and call-with-x forms?
Tangentially discussed in https://github.com/racket/racket2-rfcs/issues/49
Better, have a general way to turn thunky procedures into body-bearing syntax, like Ruby's trick of putting a lambda immediately after the function call rather than before.
Can you give an example of the trick in Ruby?
In Ruby, instead of writing my_procedure(a, b, c, {|x, y| do_something(x, y)}) where braces surround a lambda and vertical bars surround its arguments), you write:
my_procedure(a, b, c) {
|x, y| do_something (x, y)
}
which makes my_procedure look like syntax. You can prepend lambda or proc (with a slight difference in semantics) to the {} construction in order to make it an ordinary value.
Note that in Smalltalk, the ancestor of Ruby, if is theoretically a method, not syntax: you write it (using Scheme syntax here as more familiar) as
(if-then-else p (lambda () something1 ) (lambda () something2), and it works because of method dispatch: the Boolean class has subclasses True and False with different definitions of if-then-else. The bytecode compiler will optimize the lambdas away, turning them into jumps. It may or may not work to put your own definition on other classes to make their objects truthy or falsy.
So, supposing an S-expression-like syntax, maybe an #%app macro where this:
(function arg ... { body ... })
Is short for this:
(function arg ... (lambda () body ...))
But I'm not sure how to add arguments to the lambda without making it messier in the common case of simple thunks.
In Ruby and Smalltalk the |...| argument syntax is optional. We don't want to use actual vertical bars, but we could write {[x y] body}, where [x y] is optional. It depends on how much the Rhombus syntax actually changes Scheme.