Jump to page: 1 2
Thread overview
what was the problem with length prop ?
Mar 04, 2005
Ben Hinkle
Mar 05, 2005
Regan Heath
Mar 04, 2005
Derek Parnell
Mar 04, 2005
Manfred Nowak
Mar 04, 2005
Derek Parnell
Mar 05, 2005
Manfred Nowak
Mar 05, 2005
Derek Parnell
Mar 04, 2005
Regan Heath
Mar 04, 2005
David Medlock
Mar 04, 2005
David L. Davis
Mar 04, 2005
David Medlock
March 04, 2005
Maybe I missed it, but what was the problem
with just using the .length array property ?
Something about templates, or what was it ?

Where was the $ / keyword *needed*, examples ?

--anders
March 04, 2005
int[] something(int length, int init)
{
	int[] array;

	array.length = length;
	array[0 .. length - 1] = init;

	return array;
}

The "length" parameter is hidden within the array... meaning, on this line:

	array[0 .. length - 1] = init;

The "length" could mean two different things.  It's only within slices that it's a problem.

In this example, it's not a problem... but it could easily be one.

-[Unknown]


> Maybe I missed it, but what was the problem
> with just using the .length array property ?
> Something about templates, or what was it ?
> 
> Where was the $ / keyword *needed*, examples ?
> 
> --anders
March 04, 2005
On Fri, 04 Mar 2005 09:47:52 +0100, Anders F Björklund <afb@algonet.se> wrote:
> Maybe I missed it, but what was the problem
> with just using the .length array property ?
> Something about templates, or what was it ?
>
> Where was the $ / keyword *needed*, examples ?

[foo.d]
int length = 5;

void main()
{
  int length = 6;
  char[] s;

  s.length = 7;
  s[0..length-1]; // what is the value of the end index?
}

Regan
March 04, 2005
Unknown W. Brackets wrote:

> int[] something(int length, int init)
> {
>     int[] array;
> 
>     array.length = length;
>     array[0 .. length - 1] = init;
> 
>     return array;
> }
> 
> The "length" parameter is hidden within the array... meaning, on this line:
> 
>     array[0 .. length - 1] = init;
> 
> The "length" could mean two different things.  It's only within slices that it's a problem.

No, not the overloading... What I meant was:

Why wasn't the above code instead written as:

     array.length = length;
     array[0 .. array.length - 1] = init;

Where is the name of the array not known ?
(in what circumstances, I've heard "templates")

--anders
March 04, 2005
Forgive me; I indeed misunderstood.

It's not, as far as I know.  Just syntactical sugar... but, it is fairly nice sugar.  Consider:

Airport.getFromHangar(AIRPLANE_747).optionalComponents["seatbelts"][length - 5 .. length]

Okay, strange example, but it's nice to have.  That said, you're right: I don't believe there's any specific need for it.

-[Unknown]


> No, not the overloading... What I meant was:
> 
> Why wasn't the above code instead written as:
> 
>      array.length = length;
>      array[0 .. array.length - 1] = init;
> 
> Where is the name of the array not known ?
> (in what circumstances, I've heard "templates")
> 
> --anders
March 04, 2005
On Fri, 04 Mar 2005 10:14:27 +0100, Anders F Björklund wrote:


[snip]


> 
> No, not the overloading... What I meant was:
> 
> Why wasn't the above code instead written as:
> 
>       array.length = length;
>       array[0 .. array.length - 1] = init;
> 
> Where is the name of the array not known ?
> (in what circumstances, I've heard "templates")

It is not *needed*. It just makes reading and writing code easier and less costly.

Just as 'for', 'foreach', 'switch', 'while', slices, etc... are not needed, but they sure make code development less troublesome.

-- 
Derek Parnell
Melbourne, Australia
4/03/2005 10:44:05 PM
March 04, 2005
In article <d0997e$l22$1@digitaldaemon.com>, Unknown W. Brackets says...
>
>Forgive me; I indeed misunderstood.
>
>It's not, as far as I know.  Just syntactical sugar... but, it is fairly nice sugar.  Consider:
>
>Airport.getFromHangar(AIRPLANE_747).optionalComponents["seatbelts"][length - 5 .. length]
>
>Okay, strange example, but it's nice to have.  That said, you're right: I don't believe there's any specific need for it.

Given that C++ and Java have the same issue and neither of those languages have a special 'length' and I've never heard anyone request one, I don't think this use case will come up enough to make the special 'length' worth it. But that's just me...

>> No, not the overloading... What I meant was:
>> 
>> Why wasn't the above code instead written as:
>> 
>>      array.length = length;
>>      array[0 .. array.length - 1] = init;
>> 
>> Where is the name of the array not known ?
>> (in what circumstances, I've heard "templates")
>> 
>> --anders


March 04, 2005
Derek Parnell wrote:

> It is not *needed*. It just makes reading and writing code easier and less
> costly.

I'm not sure that this whole thread and Kris's problems with
"length" variables qualifies as "less costly", but anyway...

> Just as 'for', 'foreach', 'switch', 'while', slices, etc... are not needed,
> but they sure make code development less troublesome.

Yup, maybe something good can come out of this long debate ?
And that the length var hack somehow can disappear from arrays.

http://www.digitalmars.com/d/changelog.html#new099:
> What's New for D 0.99
> Aug 19, 2004
[...]
> *  Added implicit declaration of length to index and slice expressions.
> This will break existing code if length is used as a variable within [ ]. 

Until then, array.length sounds like the safe bet.
(still waiting for the reason it was introduced ?)

--anders
March 04, 2005
Unknown W. Brackets wrote:

> It's not, as far as I know.  Just syntactical sugar... but, it is fairly nice sugar.  Consider:
> 
> Airport.getFromHangar(AIRPLANE_747).optionalComponents["seatbelts"][length - 5 .. length]
> 
> Okay, strange example, but it's nice to have.

So just use a temp ? Too much sugar is not good for you, anyway :-)

--anders
March 04, 2005
Regan Heath wrote:
> On Fri, 04 Mar 2005 09:47:52 +0100, Anders F Björklund <afb@algonet.se>  wrote:
> 
>> Maybe I missed it, but what was the problem
>> with just using the .length array property ?
>> Something about templates, or what was it ?
>>
>> Where was the $ / keyword *needed*, examples ?
> 
> 
> [foo.d]
> int length = 5;
> 
> void main()
> {
>   int length = 6;
>   char[] s;
> 
>   s.length = 7;
>   s[0..length-1]; // what is the value of the end index?
> }
> 
> Regan


Its too bad the with statement doesnt work with arrays:

int []a = new int[100];
int []x;

with(a) x[0..length] = a[0..length];


Looks pretty clean to me.
« First   ‹ Prev
1 2