To begin in D coming from Python

bearophile bearophileHUGS at lycos.com
Mon Jul 21 10:10:54 PDT 2008


Luis P. Mendes:
> So can I define something as this kind of list:
> [type char, type int, type float,...] ?

There are arrays of boxes/variants that allows you to used mixed types, but you generally don't want to use them in your D code (and boxes have some bugs too).

You may want to use a struct for that. There are ways to create them on the fly too (Tuple!(), toTuple() in std.typecons and in my libs), etc.


>I wanted to know if I can load values for attributes in a concise manner.<

Can you show a working Python example of what you mean? So I/we can avoid guessing many times.


>For example, right now I'm working on a project that reads and writes circa 40 values for each instance of an object and in Python I'm able to load the values on a for loop as mentioned above.<

For example objects and structs have the .tupleof that allows you to refer to attributes as items of a untyped array.
And for example in my libs you can find HasMethod!() and autoAssign(), that can be used like (I don't use them much, but they are there, I'll prune away little used stuff in the future, I'm in the inflation phase still):

assert( HasMethod!(__LINE__, MyClass, "foo", int, float) );

assert( HasMethod!(__LINE__, MyClass, "foo") );

(I'd like to remove that __LINE__).

// And:
this(int first, int second, int third) {
    mixin(autoAssign("first second third"));
}

// Is the same as:
this(int first, int second, int third) {
    this.first = first;
    this.second = second;
    this.third = third;
}

So if you know attribute names at compile time you probably need "just" some template trickery to solve your problem (but despite D templates being simpler than C++ ones it may require you some time to learn to use them to perform such tricks).

-----------------

Jarrett Billingsley:

>I'm not sure what connection bearophile was making between what OS you use and what version of D to use.<

Generally I talk only about things I know. Not knowing much about D compilers on Linux, I have restricted my suggestion to the Win case :-) Sorry for not being more explicit in my post.

Bye,
bearophile



More information about the Digitalmars-d mailing list