March 08, 2005
In article <d0irlm$o0s$1@digitaldaemon.com>, Walter says...
>
>I'm not too sure about $, __FILE__ or __LINE__. There might be a better way.
>
>http://www.digitalmars.com/d/changelog.html
>
I like $. Code looks very readable to me.  :-)
Thanks!

Ugis


March 08, 2005
U.Baumanis wrote:

>>I'm not too sure about $, __FILE__ or __LINE__. There might be a better way.
>>
>>http://www.digitalmars.com/d/changelog.html
> 
> I like $. Code looks very readable to me.  :-)

Good for writing gnomish programs:

  char[][] phases;
  phases.length = 3;

  phases[0 .. 1] = "Collect underpants";
  phases[1 .. 2];
  phases[2 .. $] = "Profit!";

Totally readable, I agree. :-P
But it does not work in GDC...

And I *still* just don't see what was
wrong with just using phases.length ?

--anders

PS. It's from Episode 217.
http://www.tvtome.com/tvtome/servlet/GuidePageServlet/showid-344/epid-2446
March 08, 2005
On Tue, 08 Mar 2005 10:03:55 +0100, Anders F Björklund <afb@algonet.se> wrote:
> And I *still* just don't see what was
> wrong with just using phases.length ?

char[] function() {...}
function()[0..??]  //how to express the length?

really_long_and_complicated_expression_you_dont_want_to_evaluate_twice[0..really_long_and_complicated_expression_you_dont_want_to_evaluate_twice.length] //not so easy to read.

It's really just syntactical sugar, except...

void fn(inout char[] aa) {aa.length = aa.length + 1}
int length;

length = aa.length;
if (fn(aa) && length == 5) {..} //length invalid at this point.

(of course the soln is to replace length above with aa.length.. except if I also replace aa with one of the first two examples)

Regan
March 08, 2005
Regan Heath wrote:

>> And I *still* just don't see what was
>> wrong with just using phases.length ?
> 
> char[] function() {...}
> function()[0..??]  //how to express the length?
> 
> really_long_and_complicated_expression_you_dont_want_to_evaluate_twice[0..really_long_and_complicated_expression_you_dont_want_to_evaluate_twice.length]  //not so easy to read.
> 
> It's really just syntactical sugar, except...

So just use a temp ? Compiler has to, anyway.

Besides, you can also use [] instead of [0..$] ?

Oh well. When GDC gets it, I'll think about it.

--anders
March 08, 2005
On Tue, 08 Mar 2005 10:35:06 +0100, Anders F Björklund <afb@algonet.se> wrote:
> Regan Heath wrote:
>
>>> And I *still* just don't see what was
>>> wrong with just using phases.length ?
>>  char[] function() {...}
>> function()[0..??]  //how to express the length?
>>  really_long_and_complicated_expression_you_dont_want_to_evaluate_twice[0..really_long_and_complicated_expression_you_dont_want_to_evaluate_twice.length]  //not so easy to read.
>>  It's really just syntactical sugar, except...
>
> So just use a temp ?

That's what I did, in my example:

void fn(inout char[] aa) {aa.length = aa.length + 1}
int length;

length = aa.length;
if (fn(aa) && length == 5) {..} //length invalid at this point.

using a temp could cause a nasty problem. Granted it's probably rare, but I think it would be very nasty.

> Compiler has to, anyway.

No, the compiler can access the 'length' method/member/value for the object in question.

> Besides, you can also use [] instead of [0..$] ?

This is beside the point.

Regan
March 08, 2005
Regan Heath wrote:

>>>  char[] function() {...}
>>> function()[0..??]  //how to express the length?
>>>   really_long_and_complicated_expression_you_dont_want_to_evaluate_twice[0..really_long_and_complicated_expression_you_dont_want_to_evaluate_twice.length]   //not so easy to read.
>>>  It's really just syntactical sugar, except...
>>
>> So just use a temp ?
> 
> That's what I did, in my example:

No, I meant:

char[] temp = function();

temp = really_long_and_complicated_expression;

temp[0..temp.length];

Like that.

> void fn(inout char[] aa) {aa.length = aa.length + 1}
> int length;
> 
> length = aa.length;
> if (fn(aa) && length == 5) {..} //length invalid at this point.
> 
> using a temp could cause a nasty problem. Granted it's probably rare, but  I think it would be very nasty.

Nastiest right now is that any "length" variable is sometimes hidden.

But I meant a temp for the array reference, not the length. (see above)

>> Compiler has to, anyway.
> 
> No, the compiler can access the 'length' method/member/value for the  object in question.

That was the temp I meant. (the object)

>> Besides, you can also use [] instead of [0..$] ?
> This is beside the point.

It was.

--anders
March 08, 2005
>Good for writing gnomish programs:
>
>   char[][] phases;
>   phases.length = 3;
>
>   phases[0 .. 1] = "Collect underpants";
>   phases[1 .. 2];
>   phases[2 .. $] = "Profit!";
>
>Totally readable, I agree. :-P
>But it does not work in GDC...
>
>And I *still* just don't see what was
>wrong with just using phases.length ?
>
>--anders

I like your comment, but cannot help. :-)
Good example BTW, especially "Profit!" part. :-)

Ugis


March 08, 2005
U.Baumanis wrote:

>>But it does not work in GDC...
> 
> I like your comment, but cannot help. :-)

That's OK, it's just that GDC is at
DMD level 110 now and hasn't got all
the new features just yet (but it will)

It's something to be aware of, as it has
been in the past. All D code should state
the minimum compiler version needed to compile.

Or at least the version that it was written with ?

--anders
March 08, 2005
Nice idea, but any such general approach will not scale up to multidimensional arrays. A $ or a keyword can always be given the meaning "length of the appropriate dimension". It will be hard to capture this with any general "with"-like approach.


Matthew schrieb:
> "Walter" <newshound@digitalmars.com> wrote in message news:d0irlm$o0s$1@digitaldaemon.com...
> 
>>I'm not too sure about $, __FILE__ or __LINE__. There might be a better way.
>>
>>http://www.digitalmars.com/d/changelog.html
> 
> 
> He he. I'm now inclining towards a more general approach to context dependency, following the $length, $count, etc. mentioned (by ??forgotten??; sorry) the other day.
> 
> Simply put, $ would mean 'within the context of the innermost entity', so:
> 
> 
>     int[]    a = . . .
> 
>     if( 0 != a.length &&
>         a[$length - 1] == 10)
>     {
>            ...
> 
> Thus $ is not 'wasted' on array length, and neither $len nor $length (nor anything else) are defined as some special funky kinds of keywords. Rather, $X means that X is relative. Like a deeper/funkier with.
> 
> I'll leave it to faster/sharper minds to come up with all the wild ramifications of this ...
> 
> 
> :-)
> 
> 
> 
March 08, 2005
On Tue, 08 Mar 2005 10:44:08 +0100, Anders F Björklund <afb@algonet.se> wrote:
> Regan Heath wrote:
>
>>>>  char[] function() {...}
>>>> function()[0..??]  //how to express the length?
>>>>   really_long_and_complicated_expression_you_dont_want_to_evaluate_twice[0..really_long_and_complicated_expression_you_dont_want_to_evaluate_twice.length]   //not so easy to read.
>>>>  It's really just syntactical sugar, except...
>>>
>>> So just use a temp ?
>>  That's what I did, in my example:
>
> No, I meant:
>
> char[] temp = function();
>
> temp = really_long_and_complicated_expression;
>
> temp[0..temp.length];
>
> Like that.

Oh, sorry. I see.

>> void fn(inout char[] aa) {aa.length = aa.length + 1}
>> int length;
>>  length = aa.length;
>> if (fn(aa) && length == 5) {..} //length invalid at this point.
>>  using a temp could cause a nasty problem. Granted it's probably rare, but  I think it would be very nasty.
>
> Nastiest right now is that any "length" variable is sometimes hidden.

True, I was assuming that was removed.

> But I meant a temp for the array reference, not the length. (see above)

Yep, gotcha.

Regan