Sunday, December 28, 2008

Is There A Point in Using Pointers?

A few people wrote back in response to a previous blog post on the D for .NET project, some asking, well, why .NET?

Part of the answer is that .NET and D seem to be made for each other:

A common fragrance imbues both designs; for example, in D structs are not objects, but value types -- same as in C#. In D all objects inherit from a root object, which has methods such as toString , toHash and opEquals; in .NET, [mscorlib]System.Object sports ToString, GetHashCode, and Equals.

Still not convinced? How about array properties, then? In D there are properties such as sort, reverse, and dup; in .NET we have System.Array.Sort(), System.Array.Reverse(), and (tadaaa) System.Array.Clone(). Coincidence? Perhaps. Or maybe powerful memes where floating free in the air and found propitious hosts in both .NET and D (not unlike the idea of Python-scripting a debugger, which was pioneered by ZeroBUGS, and it is now being adopted by GDB).

But the cute metaphors have to stop somewhere (no honeymoon lasts forever) and so we come upon the thorny issue of pointers. D allows pointers, albeit does not encourage them. But unmanaged pointers (and even managed pointers arithmetic) does not yield verifiable code in .NET. I have experimented with both managed and unmanaged pointers, and generated textual IL that compiles and runs; PEVERIFY however refuses to put the seal of approval on such code.

And so I am very tempted to disallow pointers in class and struct members (in D, as in .NET objects are manipulated via references anyway, so what's the point of a pointer, anyway?)

4 comments:

Anonymous said...

They're fast!

Cristache said...

Sure, but remember that in D you have things such as foreach (code can be generated under the hood to traverse arrays and tuples in the most optimal way) and array slices (you can work with array ranges in a bound-checked way, fast).

Anonymous said...

I guess it depends on the final application. I figure the majority of programs will benefit from elimination of pointers, by speeding development and eliminating some types of bugs. However if the application has to be as fast as possible, then pointers might be necessary.

Cristache said...

Derek,
if that turns out to be the case we'll have to add some unsafe { } hack to the front-end, C#-style. But for now I am focused on releasing a D.NET compiler as soon as possible and refine it later as requests from users come in.