www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - rationale: [] and ()

reply "Manfred_Nowak" <svv1999 hotmail.com> writes:
What is the rationale for having both: normal and square brackets?

I ask because in plain old C I interpreted the lexical difference of 
funtion calls and accesses to elements of arrays as a permanent hint for 
linear runtime in case of arrays. But because of `opIndex' this assumption 
has been invalidated a long time ago.
 
-manfred 
Dec 10 2010
next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Manfred_Nowak <svv1999 hotmail.com> wrote:

 What is the rationale for having both: normal and square brackets?

Mostly that C has both. Changing this would make the language feel rather more different from C/C++ than it already does. It is also syntactic sugar for element lookup. Rather than having to write some function name (that may or may not be the same for all collections), you can use [].
 I ask because in plain old C I interpreted the lexical difference of
 funtion calls and accesses to elements of arrays as a permanent hint for
 linear runtime in case of arrays.

 But because of `opIndex' this assumption has been invalidated a long
 time ago.

No it hasn't. opIndex should still be O(1), it just can't be enforced. -- Simen
Dec 10 2010
parent "Manfred_Nowak" <svv1999 hotmail.com> writes:
Steven Schveighoffer wrote:

 Essentially sub-linear.

BTW: did you notice, that the modell assumption of an access time independent from the total length of a used slice of memory seems to become wrong? -manfred
Dec 10 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 10 Dec 2010 09:42:51 -0500, Simen kjaeraas  
<simen.kjaras gmail.com> wrote:

 Manfred_Nowak <svv1999 hotmail.com> wrote:

 But because of `opIndex' this assumption has been invalidated a long
 time ago.

No it hasn't. opIndex should still be O(1), it just can't be enforced.

er.. make that O(lg(n)) :) Essentially sub-linear. -Steve
Dec 10 2010
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Steven Schveighoffer <schveiguy yahoo.com> wrote:

 On Fri, 10 Dec 2010 09:42:51 -0500, Simen kjaeraas  
 <simen.kjaras gmail.com> wrote:

 Manfred_Nowak <svv1999 hotmail.com> wrote:

 But because of `opIndex' this assumption has been invalidated a long
 time ago.

No it hasn't. opIndex should still be O(1), it just can't be enforced.

er.. make that O(lg(n)) :) Essentially sub-linear.

Ah, yes. Sorry. -- Simen
Dec 10 2010