rhombus-prototype icon indicating copy to clipboard operation
rhombus-prototype copied to clipboard

Have with-output-to-string and friends take a body, not a thunk.

Open gus-massa opened this issue 6 years ago • 5 comments

  • Have with-output-to-string and friends take a body, not a thunk. The current form is better named call-with-output-to-string, in the general pattern of with-x being syntax and call-with-x being 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

gus-massa avatar Jul 21 '19 19:07 gus-massa

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.

johnwcowan avatar Oct 08 '19 04:10 johnwcowan

Can you give an example of the trick in Ruby?

gus-massa avatar Oct 08 '19 14:10 gus-massa

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.

johnwcowan avatar Oct 08 '19 16:10 johnwcowan

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.

jackfirth avatar Oct 08 '19 17:10 jackfirth

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.

johnwcowan avatar Oct 08 '19 17:10 johnwcowan