www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Makeshift array "literals"

reply Chris Sauls <ibisbasenji gmail.com> writes:
Until such time as we get /real/ array literals, maybe something like the
following ought 
to be added to Phobos somewhere (object.d?), to at least provide a near
equivelant on the 
function-body level:

# template Array(T) {
#   T[] Array (T[] arr ...)
#   body {
#     return arr.dup;
#   }
# }

Too bad functions can't be called to assign to constants and
module/class/struct level 
statics.  Maybe a rule could be made, stating its legal so long as only
literals and/or 
defined constants are used as parameters?  Thus allowing things like:

# const char[][] NAMES = Array!(char[])("Grant", "Paul", "Bonnie");

-- Chris Sauls
Jun 22 2005
next sibling parent Vathix <chris dprogramming.com> writes:
On Wed, 22 Jun 2005 08:51:26 -0400, Chris Sauls <ibisbasenji gmail.com>  
wrote:

 Until such time as we get /real/ array literals, maybe something like  
 the following ought to be added to Phobos somewhere (object.d?), to at  
 least provide a near equivelant on the function-body level:

 # template Array(T) {
 #   T[] Array (T[] arr ...)
 #   body {
 #     return arr.dup;
 #   }
 # }

 Too bad functions can't be called to assign to constants and  
 module/class/struct level statics.  Maybe a rule could be made, stating  
 its legal so long as only literals and/or defined constants are used as  
 parameters?  Thus allowing things like:

 # const char[][] NAMES = Array!(char[])("Grant", "Paul", "Bonnie");

 -- Chris Sauls

The dup kills it. Better would be to allow templates to have var args, template ConstArray(T, T[] ta ...) { const T[] ConstArray = ta; } const char[][] NAMES = ConstArray!(char[], "John", "Paul", "Ringo", "George");
Jun 22 2005
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Chris Sauls wrote:
 Until such time as we get /real/ array literals, maybe something like 
 the following ought to be added to Phobos somewhere (object.d?), to at 
 least provide a near equivelant on the function-body level:
 
 # template Array(T) {
 #   T[] Array (T[] arr ...)
 #   body {
 #     return arr.dup;
 #   }
 # }

I did something like this a while back digitalmars.D/12920 but yes, rewriting it to use a typesafe variadic now that we have it is a logical progression.
 Too bad functions can't be called to assign to constants and 
 module/class/struct level statics.  Maybe a rule could be made, stating 
 its legal so long as only literals and/or defined constants are used as 
 parameters? 

The problem is that functions can have side effects. And so it would be possible to create initialisers that confusingly depend on the order of evaluation.
 Thus allowing things like:
 
 # const char[][] NAMES = Array!(char[])("Grant", "Paul", "Bonnie");

You can already use the static initialiser syntax on such things: const char[][] NAMES = [ "Grant", "Paul", "Bonnie" ]; Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 22 2005