www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Automagic length of static arrays

reply Vathix <chris dprogramming.com> writes:
As everyone knows, C will magically know the length of the array when  
leaving out the number of elements if specifying an initializer like so,
    char foo[] = "abcde";
and as you know, that's not going to happen the same way in D because  
that's how to declare a dynamic array. So I have an idea to allow D to  
support C's magic with the following D syntax:
    char[$] foo = "abcde";
good?
Aug 22 2005
next sibling parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
Vathix wrote:
 As everyone knows, C will magically know the length of the array when  
 leaving out the number of elements if specifying an initializer like so,
    char foo[] = "abcde";
 and as you know, that's not going to happen the same way in D because  
 that's how to declare a dynamic array. So I have an idea to allow D to  
 support C's magic with the following D syntax:
    char[$] foo = "abcde";
 good?
looks good to me.
Aug 22 2005
prev sibling next sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Vathix <chris dprogramming.com> wrote:

[...]
     char[$] foo = "abcde";
 good?
Maybe. But why is the D-way wrong, i.e. using a `.' when it suffices? Saves two characters to type in addition. char. foo = "That is the point"; Ahh, I see, that will turn all assignments to fields or properties into a declaration. But D already has for ranges `[0..1]' the special <decimal number><point><point> - token which is to my knowledge unique in the world of computer languages. So why not define the <Identifier><point><point> - token to be the automagic length inheretor? char..foo = "That is the D way"; -manfred ;-) P.S.: I really forget the range special point when posting http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/27953
Aug 22 2005
parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
Manfred Nowak wrote:
 Vathix <chris dprogramming.com> wrote:
 
 [...]
 
    char[$] foo = "abcde";
good?
Maybe. But why is the D-way wrong, i.e. using a `.' when it suffices? Saves two characters to type in addition. char. foo = "That is the point";
Because D doesn't do that! I think you are referring to D using '.' instead of '->' for pointers? It's erroneous to claim that using a dot every where is the D way.
 
 Ahh, I see, that will turn all assignments to fields or properties 
 into a declaration. But D already has for ranges `[0..1]' the special
   <decimal number><point><point> - token
 which is to my knowledge unique in the world of computer languages.
 
 So why not define the
   <Identifier><point><point> - token
 to be the automagic length inheretor?
 
      char..foo = "That is the D way";
 
 -manfred ;-)
 
 P.S.:
 I really forget the range special point when posting
 http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/27953
 
  
Well, char[$] makes more sense, because $ is already used to refer to the array's length, and because we usually put the length inside [brackets] when declaring a statically allocated array, so the concept already has some roots. OTOH, char..foo means "from char to foo"!!!!!!! That makes no sense if you ask me.
Aug 22 2005
prev sibling parent Freejack <freejack nowhere.net> writes:
On Mon, 22 Aug 2005 05:41:35 -0400, Vathix wrote:

 As everyone knows, C will magically know the length of the array when  
 leaving out the number of elements if specifying an initializer like so,
     char foo[] = "abcde";
 and as you know, that's not going to happen the same way in D because  
 that's how to declare a dynamic array. So I have an idea to allow D to  
 support C's magic with the following D syntax:
     char[$] foo = "abcde";
 good?
Sounds alright. I'm new to D, but I've been doing C, Ada, and Assembler for better than ten years now. One thing that chaffes my ass about C style arrays(or imperative language arrays in general) is the error prone and tedious indexing. I think a better solution would be to just switch to APL style arrays and array handling(using D syntax of course). I like D, and for all it's advancements, it's still stuck in the dark ages for array manipulation. char[$] foo = "blah"; shouldn't be necessary with a language sporting the features of D. APL style arrays would certainly give D a leg up and over it's competitors. My .02 worth. Freejack
Aug 27 2005