🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1043

Re: Proposed: "closures"

#1043
From: Benjamin Schreiber
Date: Tue, 23 Apr 1991 03:22
69 lines
3009 bytes
(before you ask, the answer is no: I am NOT Bjarn Stroustroup; I just 
happen to have the same alias)

In article <1991Apr12.081539.22690@kestrel.edu> Gyro@Reasoning.COM writes:
> 2) Proposal 2 unifies the domain `closure of T' with that of `pointer
>    to function of signature T'.  That is, under proposal 2, the same
>    syntax that has been interpreted as declaring a pointer to
>    function of signature T will now be interpreted as declaring a
>    closure of T.
>
>If proposal 1 is adopted but not proposal 2, it will be necessary to
>invent a declaration syntax for closures.  I prefer to avoid doing so
>unless and until it may be seen to be the sense of the committee that
>this is a likely outcome.

Closures are an excelant idea.  However, I think that they should be
kept distinct from pointers to functions and pointers to member functions.
My primary argument for keeping them distinct from normal pointers to
functions has to do with functions of "C" linkage: if yoou attempt to
give it a closure, it will choke.

If the  concepts are indeed kept distinct, a clearly distint syntax is
necessary.  My suggestion would be to use a unary && (an extention of
taking the address of a function).  This operator would also be used
in the declaration.  Example:

class to_be_closed {
private:
	int data;
public:
	int method(int);
	to_be_closed(int d) {data = d;};
};

int f(int c)		// A function to be close over
{
  return c;
}

extern "C" {
int g(int);
int use(int (*)());
}

void do_stuff(void)
{
  int &&closure(int);	// Declaration of a closure
  to_be_closed  obj(5);

  closure = &&obj.method;	// take closure of method over obj
  closure = obj.method;		// same, since there is no other meaning

  closure(5);			// Invoke the closure
  
  closure = &&f;		// Take closure of global method
  closure = f;			// Error: f alone equivelant to &f

  use(closure);			// Error: can't pass closure in place of fptr
  use(f);			// OK: pointer to function expected
}

I hope this example is clear (and not error riddled; my editor is having
problems...)

If you don't want to take me seriously, you don't have to; I'm only an
undergraduate with an enthusiasm for C++.

	-- Benjamin Schreiber
	   bs@cs.brandeis.edu
Technical Details
Message-ID: <1991Apr23.032208.3264@news.cs.brandeis.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!zaphod.mps.ohio-state.edu!ncar!gatech!wrdis01!nstn.ns.ca!clyde.concordia.ca!altitude!matrox!uvm-gen!kira!nic!news.cs.brandeis.edu!chaos.cs.brandeis.edu!bs
References: <1991Apr12.081539.22690@kestrel.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!zaphod.mps.ohio-state.edu!ncar!gatech!wrdis01!nstn.ns.ca!clyde.concordia.ca!altitude!matrox!uvm-gen!kira!nic!news.cs.brandeis.edu!chaos.cs.brandeis.edu!bs
From: bs@chaos.cs.brandeis.edu (Benjamin Schreiber)
Newsgroups: comp.std.c++
Subject: Re: Proposed: "closures"
Summary: Closures and pointers to functions should be distinct.
Keywords: proposal, closures, function pointers
Message-ID: <1991Apr23.032208.3264@news.cs.brandeis.edu>
Date: 23 Apr 91 03:22:08 GMT
References: <1991Apr12.081539.22690@kestrel.edu>
Sender: Benjamin Schreiber (NOT Bjarn Stroustroup)
Distribution: comp.std.c++
Organization: Brandeis University
Lines: 69