www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature request: lazy as a type constructor

reply "Janice Caron" <caron800 googlemail.com> writes:
Please may we have "lazy" as a type constructor (as well as a
parameter storage class)? Currently I can declare a lazy array of
ints, but not an array of lazy ints. That means we can't have variadic
functions with lazy variadic parameters. It would be quite useful to
be able to do:

    void f(lazy(int)[]a...)
    {
    }

The intent here is to declare that a is a variadic array of lazy ints
- that is, an array of delegates, so that when I do

    void f(lazy(int)[]a...)
    {
        foreach(d;a)
        {
            if (a == 0) return;
        }
    }

    int show(int n)
    {
        writefln(n);
        return n;
    }

    f(show(1),show(0),show(2));

we would expect show() to be called only twice (with parameters 0 and
1). show(2) won't have happened because of the laziness.

Unfortunately I can't do that right now, because lazy is a storage
class, not a type modifier. I can make the entire array lazy, but not
the array's elements.
Sep 25 2007
next sibling parent BCS <ao pathlink.com> writes:
Reply to Janice,

 Please may we have "lazy" as a type constructor (as well as a
 parameter storage class)? Currently I can declare a lazy array of
 ints, but not an array of lazy ints. That means we can't have variadic
 functions with lazy variadic parameters. It would be quite useful to
 be able to do:
 
This sounds like a useful addition
Sep 25 2007
prev sibling next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Janice Caron" <caron800 googlemail.com> wrote in message 
news:mailman.300.1190729504.16939.digitalmars-d puremagic.com...
 Please may we have "lazy" as a type constructor (as well as a
 parameter storage class)? Currently I can declare a lazy array of
 ints, but not an array of lazy ints. That means we can't have variadic
 functions with lazy variadic parameters.
vote += real.max; While we're at it.. make ref a type constructor too. REF RETURNS WOOO can't wait for those.
Sep 25 2007
next sibling parent downs <default_357-line yahoo.de> writes:
Jarrett Billingsley wrote:
 "Janice Caron" <caron800 googlemail.com> wrote in message 
 news:mailman.300.1190729504.16939.digitalmars-d puremagic.com...
 Please may we have "lazy" as a type constructor (as well as a
 parameter storage class)? Currently I can declare a lazy array of
 ints, but not an array of lazy ints. That means we can't have variadic
 functions with lazy variadic parameters.
vote += real.max; While we're at it.. make ref a type constructor too. REF RETURNS WOOO can't wait for those.
OH PLEASE. vote=real.infinity; // for both :D --downs aka mr. "I lost my key pair"
Sep 25 2007
prev sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
Jarrett Billingsley wrote:
 "Janice Caron" <caron800 googlemail.com> wrote in message 
 news:mailman.300.1190729504.16939.digitalmars-d puremagic.com...
 Please may we have "lazy" as a type constructor (as well as a
 parameter storage class)? Currently I can declare a lazy array of
 ints, but not an array of lazy ints. That means we can't have variadic
 functions with lazy variadic parameters.
vote += real.max; While we're at it.. make ref a type constructor too. REF RETURNS WOOO can't wait for those.
vote += real.nan; I abstain, for myself and everyone else.
Sep 25 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Christopher,

 Jarrett Billingsley wrote:
 
 "Janice Caron" <caron800 googlemail.com> wrote in message
 news:mailman.300.1190729504.16939.digitalmars-d puremagic.com...
 
 Please may we have "lazy" as a type constructor (as well as a
 parameter storage class)? Currently I can declare a lazy array of
 ints, but not an array of lazy ints. That means we can't have
 variadic functions with lazy variadic parameters.
 
vote += real.max; While we're at it.. make ref a type constructor too. REF RETURNS WOOO can't wait for those.
vote += real.nan; I abstain, for myself and everyone else.
qsort(taskBuffer.ptr, Task.sizeof, function int(void* a, void* b) { return (cast(Task*)a).vote !< (cast(Task*)b).vote; }) // now nan is at the head of the list :b
Sep 25 2007
parent "Janice Caron" <caron800 googlemail.com> writes:
The thing is, I say these things without having thought them through
properly, and then all the thinking happens as we're having these
discussions. What I /wanted/ was to be able to declare a function with
an unknown number of lazy arguments. My suggestion may have been
overkill. Perhaps it would suffice to define that

    void f(lazy T[]...)

shall mean an array of delegates. That would solve the problem that I
wanted solving.

But since I did put the original suggestion on the table, lets think
it through and see where it would lead. It would mean that you would
be able to declare lazy variables locally. Is this a good thing? I
really don't know. Let's see what an example would look like:

    lazy(int) n;

here, n is not really an int, it's a lazy(int) - that is, a delegate
returning an int. So when we assign it, we're not giving it a value,
we're giving it a function body, and then when we read it, we're
executing that function.

    lazy(int) n = x + 1;
    for (x=0; x<10; ++x)
    {
        writefln(n);
    }

What would this do? I think it would print the numbers 1 to 10. Do we
want to go down that road? I suspect not, as it's too big a departure
from the way we expect things to work.

On the other hand, to expect

    void f(lazy T[]...)

to mean an array of lazies, rather than a lazy array, is perhaps not
that unreasable. I think, therefore, that I must withdraw my original
request, and replace it with this, much simpler one: Please may there
be a way to declare a variadic function with (an unknown number of)
lazy arguments?
Sep 26 2007
prev sibling next sibling parent "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"Janice Caron" <caron800 googlemail.com> wrote in message 
news:mailman.300.1190729504.16939.digitalmars-d puremagic.com...
 Please may we have "lazy" as a type constructor (as well as a
 parameter storage class)?
I'm not sure how the implementation of this would work in general.
 Currently I can declare a lazy array of
 ints, but not an array of lazy ints. That means we can't have variadic
 functions with lazy variadic parameters.
<snip> Actually you can: http://www.digitalmars.com/d/function.html "Lazy Variadic Functions If the variadic parameter is an array of delegates with no parameters: void foo(int delegate()[] dgs ...); Then each of the arguments whose type does not match that of the delegate is converted to a delegate." Stewart. -- My e-mail address is valid but not my primary mailbox. Please keep replies on the 'group where everybody may benefit.
Sep 26 2007
prev sibling parent reply renoX <renosky free.fr> writes:
Janice Caron a écrit :
 Please may we have "lazy" as a type constructor (as well as a
 parameter storage class)? Currently I can declare a lazy array of
 ints, but not an array of lazy ints.
Uh? An int is a 32b machine word, so an array of int is a pretty efficient structure, so you cannot assingn a special value to an int not created. So an array of lazy int is probably an array of references to int, which is not very efficient.. renoX
Oct 07 2007
next sibling parent Nathan Reed <nathaniel.reed gmail.com> writes:
renoX wrote:
 So an array of lazy int is probably an array of references to int, which 
 is not very efficient..
Lazy parameters are implemented as delegates, so actually an array of lazy ints would be an array of delegates returning ints. Thanks, Nathan Reed
Oct 07 2007
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 10/7/07, renoX <renosky free.fr> wrote:
 Janice Caron a écrit :
 Please may we have "lazy" as a type constructor (as well as a
 parameter storage class)? Currently I can declare a lazy array of
 ints, but not an array of lazy ints.
Uh?
What Nathan said. Plus, I retracted this request a few posts up, and replaced it with a simpler one, which was this: please could lazy T[]... be interpreted as an array of delegates? (Currently it is interpreted as a delegate returning an array of Ts).
Oct 07 2007