Biggest problems w/ D

Kirk McDonald kirklin.mcdonald at gmail.com
Thu Aug 9 19:27:45 PDT 2007


C. Dunn wrote:
> 4) Not enough help for converting between D strings and C char*. 
> There must be conversion functions which work regardless of whether
> the D string is dynamic or not, and regardless of whether the C char*
> is null terminated.  I'm not sure what the answer is, but this has
> lead to a large number of runtime bugs for me as a novice.
> 

The std.string module has the toStringz and toString functions.

The toString function simply returns a slice over the C string:

char[] toString(char* ptr) {
	return ptr[0 .. strlen(ptr)];
}

The toStringz function simply appends a null character (\0) to the end
of the D string:

char* toStringz(char[] str) {
	return (str ~ \0).ptr;
}

These are very simple operations, and it is fairly easy to adapt
them to whatever needs you have.

> 5) Same syntax structs and classes. This is minor problem, but it is
> extremely confusing to the novice.  A struct and a class are
> completely different in D, but the fact that I can write code the
> same to use either one implies too much similarity.  It leads to
> programming errors which easily result in seg-fault.
> 
> Also, it is too difficult to switch a set of objects from structs to
> classes or vice versa.  I need to be able to do that in order to
> compare runtimes for various implementations.  Again, I don't know
> the best answer.
> 

Structs and classes are very different beasts in D, even if they look 
the same. However, there are some important differences in their use 
which should clue you in:

Using 'new' on a class gives you a reference, using it on a struct gives 
you a pointer.

Structs are plain ol' data. Saying "SomeStruct s;" gives you an instance 
you can immediately behind using. You have to use 'new' to get a class 
instance.

Classes have inheritance and all those other object-oriented features, 
while structs do not.

What I am getting at is that whether something should be a class or a 
struct seems like an important design decision which shouldn't be 
expected to change after development is well underway. Also, code 
doesn't really look all that similar when it uses structs vs. classes.

> 
> I love the language as an evolution of C++, so I really hope these
> problems get ironed out.  I could list a *lot* of great things about
> D!
> 

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org



More information about the Digitalmars-d mailing list