www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What's left before we can have array literals?

reply Stewart Gordon <smjg_1998 yahoo.com> writes:
This isn't yet another request for array literals to be implemented by 
the time 1.0 comes along, just a look at how far away we really are.

There are really two separate issues to it:


1. Deciding on a notation and writing the code to parse it

A number of notations have been proposed.  I personally like the 
notation proposed by Chris Miller (digitalmars.D:39125, "Proposal: 
struct and array literal syntax").

Basically, it works like this:

     int[]![6, 7, 8, 9]

As I see it, what follows the '!' takes the form of an array initialiser 
- consequently, an array of arrays or structs can be created by 
embedding array or struct initialisers within the array literal.  The 
only difference is that they might not have to be compile-time constants 
- but it would work considering that the types are known at compile time 
just the same.


2. Building the internal workings

We already have string literals.  Using the same internal workings for 
array literals would at least enable us to have array literals that are 
compile-time constants.  Of course, to allow elements of array literals 
to be evaluated at runtime, the workings would have to be set up to 
support it.

Walter recently wrote, of the possibility of lazy variadics:

"What it means is an array literal is created out of the arguments, 
which is then wrapped in a delegate.

That cannot work until array literals are implemented."

But I'm rather puzzled by this claim.  When you call a typesafe variadic 
function, even when it isn't lazy, is this not effectively an array 
literal, at least internally?  If it is, then this would mean that we 
already have the workings towards supporting array literals, and could 
use them in lazy variadics just the same.   Otherwise, how does it work 
at the moment?

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-  C++  a->--- UB  P+ L E  W++  N+++ o K-  w++  O? M V? PS- 
PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.
Sep 05 2006
next sibling parent Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Stewart Gordon wrote:
 This isn't yet another request for array literals to be implemented by 
 the time 1.0 comes along, 

:D
 just a look at how far away we really are.
 
 There are really two separate issues to it:
 
 
 1. Deciding on a notation and writing the code to parse it
 
 A number of notations have been proposed.  I personally like the 
 notation proposed by Chris Miller (digitalmars.D:39125, "Proposal: 
 struct and array literal syntax").
 
 Basically, it works like this:
 
     int[]![6, 7, 8, 9]

Yeah, this is really, really the best one so far.
 
 As I see it, what follows the '!' takes the form of an array initialiser 
 - consequently, an array of arrays or structs can be created by 
 embedding array or struct initialisers within the array literal.  The 
 only difference is that they might not have to be compile-time constants 
 - but it would work considering that the types are known at compile time 
 just the same.
 
 
 2. Building the internal workings
 
 We already have string literals.  Using the same internal workings for 
 array literals would at least enable us to have array literals that are 
 compile-time constants.  Of course, to allow elements of array literals 
 to be evaluated at runtime, the workings would have to be set up to 
 support it.
 
 Walter recently wrote, of the possibility of lazy variadics:
 
 "What it means is an array literal is created out of the arguments, 
 which is then wrapped in a delegate.
 
 That cannot work until array literals are implemented."
 
 But I'm rather puzzled by this claim.  When you call a typesafe variadic 
 function, even when it isn't lazy, is this not effectively an array 
 literal, at least internally?  

Hmm, I never thought about it that way but actually that is what it really is. I mean that is what I have been using T[] array(T)(T[] x...) for -> As an array literal. So I have a strong feeling that array literals aren't that far away.
 If it is, then this would mean that we 
 already have the workings towards supporting array literals, and could 
 use them in lazy variadics just the same.   Otherwise, how does it work 
 at the moment?

Magic?
Sep 05 2006
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message 
news:edl274$2m8$3 digitaldaemon.com...

 But I'm rather puzzled by this claim.  When you call a typesafe variadic 
 function, even when it isn't lazy, is this not effectively an array 
 literal, at least internally?  If it is, then this would mean that we 
 already have the workings towards supporting array literals, and could use 
 them in lazy variadics just the same.   Otherwise, how does it work at the 
 moment?

You're entirely right. Looking at the disassembly shows it. Basically the compiler creates an array on the stack, fills it with the values at runtime, and passes that array to the function. Of course, this means the data lives on the stack, and must be dup'ed in order for it not to be lost.
Sep 05 2006