On Wednesday, 10 October 2018 at 23:26:38 UTC, Dennis wrote:
Can you give a real-world, non-foo/bar example where you want to use it? I have trouble understanding what you want to accomplish.


I don't understand why you need to be convinced that this is relevant.

Do you not realize that there are cases where one wants to select the last element of a list without having to explicitly know it?

After all, the whole point of $ is exactly to specify this "last element".

arr[$-1]

is EXACTLY shorthand for

arr[arr.length - 1]

So, you are already drinking the cool aid.

All I'm proposing is to to allow one to escape that syntax to function calls.

foo(int index)
{
   return arr[index];
}


and D can support

foo($-1);

which simply gets translated in to

arr[arr.length - 1]


All D does is look at the argument, when parsing, see's the $ and say's "Ah ah, they they want to access the last element of the array where index is used.
It then simply sets index to arr.length - 1.

It is NO different than what it already does except, because we can use it in functions, explicitly(it only works at compile time), it is just more sugar... again, $ is pure sugar.


There are many applications, I shouldn't have to justify why it would be useful... it is, because it is as useful as it is.

If you claim it is not useful then you also must make that claim about the current semantics of $.

But since you seem to need prodding, I will prod,

// returns elements from graph
GetNormalizedGraph(int index)
{
   auto Graph[1000];
   for(i = 0; i < Graph.length; i++)
      Graph[i] = i^2/100;
   return normalized(Graph[index]);
}

then

GetNormalizedGraph($-1)

always returns the last element. The point is, the caller doesn't have to know the size of Graph inside... it can change without breaking the program.

It beats having to use hacks like using negative -1 to represent the length, etc.

The only problem is that we might use index for multiple arrays all having different lengths, which would be a violation since it would make the index multi valued. This can be solved by using lengths for the arrays but setting the index to max value or an compiler error.


// returns elements from graph
GetNormalizedGraph(int index = -1)
{
   auto Graph[1000];
   for(i = 0; i < Graph.length; i++)
      Graph[i] = i^2/100;
   return normalized(Graph[index == -1 ? Graph.length : index]);
}

The problem is when we do

   GetNormalizedGraph()

it is tells us nothing

   GetNormalizedGraph(-1)

is confusing, are we indexing at -1?

   GetNormalizedGraph(int.max)

again confusing, the array isn't going to to be int.max long, is it?

but

    GetNormalizedGraph($-1)


makes perfect sense for what $ does.


The compiler just has to do a little magic, that is all... all sugar is magic anyways. I don't know why some people want to have their sugar in their coffee but the scoff at people who drink cokes.





Reply via email to