Thursday, October 7, 2010

A $ comes for free

A $ comes for free 

It took me a while (about 1.5 year of working with SV), but I’ve just realised that dynamic arrays are just plain useless: Whatever a dynamic array can do, a queue can do much better. A queue could be resized using new[] just like an array, but it would also be resized automatically when you add an element, or a list of elements. You could use two variables to slice a queue, which you can’t do with an array, and this alone is a good enough reason to forget about dynamic arrays for eternity. And, in general, every function or language structure that would work with a dynamic array, would also work with a queue. You could also randomize queues of course. 
And why did it take me such a long time to figure this out? Obviously, because no one in his right mind expects a language to have some outright redundant constructs. I was assuming that if dynamic arrays are there, then there must be some reason, some hidden super-power that I haven’t come-across yet. 
So, the bottom line is, whenever you write something like that: 
int a[]; 
Just go ahead and place that $ inside: 
int a[$]

No comments:

Post a Comment