Nice TWiki > Dev > FeatureProposals > BlockCallSyntax (r1.1) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Dev . { Changes | Index | Search | Go }
It would be good to allow a different syntax for method application in some circumstances. Specifically, it would be nice if:
foo
{
 baz();
}

could expand into

foo(()=> 
  { 
     baz();
  });

Basically, if a function call is followed immediately by a block, then the block is converted to a void->A function, where A is the return type of the function, and treated as the last argument to the function call.

This would allow us to add features like C#'s 'using' keyword, without having to modify the syntax every time we want a new one:


<A> A using(Disposable resource, void->A func)
{
  try
  {
     return func();
  }
  finally
  {
    resource.dispose();
  }
}

let stream = new FileInputStream("foo");
let value = using(stream) { char c = stream.read(); }

Of course, we might also want something other than a void->A function, perhaps we'd like to have a version of 'using' that doesn't require to first bind the used object to a name. We could solve this by instead expanding the block function into a function which takes exactly the same arguments, with the same names, that the named function takes:

//Assume an abstract interface for disposables here, makes the
//typing nicer.
<Disposable A, B> B using(A resource, A->B func)
{
  try
  {
     return func(resource: resource);
  }
  finally
  {
    resource.dispose();
  }
}

so now we can do

using(new FileInputStream("foo"))
{
  //use the file input stream with the name 'resource':
  char c = resource.read();
}

which the compiler will translate to:

using(new FileInputStream("foo"), (FileInputStream resouce) =>
  {
    //use the file input stream with the name 'resource':
    char c = resource.read();
  });

-- BrynKeller - 31 Oct 2003

Topic BlockCallSyntax . { Edit | Attach | Ref-By | Printable | Diffs | r1.16 | > | r1.15 | > | r1.14 | More }
Revision r1.1 - 31 Oct 2003 - 17:40 GMT - BrynKeller
Parents: WebHome > FeatureProposals
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.