Other language features you'd want in D
dominik
aha at aha.com
Fri Feb 22 16:00:59 PST 2008
"Denton Cockburn" <diboss at hotmail.com> wrote in message
news:pan.2008.02.22.21.00.40.605571 at hotmail.com...
> What are some features found in other languages that you believe would be
> really good to have in D?
Since I'm still learning, I can't say on much more advanced stuff.
But, the most problematic aspect of the language I've seen yet (I use D1
still) has got to be array initialization, struct initialization and
god-forbid both in the same context.
for example, though this might've been solved for D2 already:
char[][int] foo = [1:"barbar", 2:"bar"]; - this won't work with dmd
compiler,
but this will:
char[][int] foo = [1:"barbar"[], 2:"bar"];
then, as a newcomer, I seem to never quite understand how shall I initialize
my array.. so I got into a habit of instead modeling an array in head like I
did with C, I use this little trick with pragma where I actually model data
in order to get an array definition out of it:
so, for example this little line where it is OBVIOUS to me what I wan't
data to be modeled:
pragma(msg, typeof([["foo"[]: "bar"[]]: 5]).stringof);
gives me: int[char[][char[]]] - which is something rather UGH to model in my
mind, since words fail me here :)
ok, so I thought to myself, no big deal, right? I'll use
.typeof(..).stringof to get the right definition since I'm retarded and can
model data immidiately, while I can't seem to model the definition.. nice
trick there with that line.. moving on..
AND then.. while working on something I had to make a static struct
definitions which consists of its own substructs and arrays of different
sorts (I have tried to model my incoming data as precisely as possible to
avoid possible translation issues later on).
So, first things first - I have tried to write a simple test, and it worked:
file: test.d ?? more text below code
---
module test;
import tango.io.Stdout;
align(1)
struct DST {
private int test;
private char[] secondtest;
}
public static DST[char[]] AA;
static this()
{
AA["Africa/Asmara"] = DST(5, "bla");
AA["Africabambata/Asmara"] = DST(6, "blabla");
}
void main() {
foreach(v; AA) {
Stdout.formatln("{} : {}", v.test, v.secondtest);
}
Stdout.formatln("{}", AA["Africa/Asmara"].test);
Stdout.formatln("{}", AA["Africa/Asmara"].secondtest);
}
---
sure, confident with this little test, I wrote one with static or dynamic
initialization of "pointers" to static data, so it looked like this:
file: test2.d ?? more text below code
---
module test2;
import tango.io.Stdout;
struct Foo {
int x;
char[] y;
char[] key;
}
static Foo[] data =
[
{0, "blah", "key1"},
{10, "foo", "key2"}
];
Foo*[char[]] aa;
void init() {
foreach (inout foo; data) {
aa[foo.key] = &foo;
}
}
static this(){
init(); // initialize compile time
}
void main() {
//init(); // initialize run-time
foreach(v; aa) {
Stdout.formatln("{} - {} - {}", v.x, v.y, v.key);
}
Stdout.formatln("{}", aa["key2"].x);
}
---
alrighty then, it works, now onto the real thing - where TROUBLE, for me,
starts:
first let me post a definition of struct itself:
---
struct DST {
char[] tz_key;
char[] tz_version;
char[] timestamp;
//-aliases
char[] geo_coordinates;
//-Annual Transition Policy
annualTransitionPolicy[] annualTransitionPolicies;
//-Initial Annual Transition Policy
initialAnnualTransitionPolicy[] initialAnnualTransitionPolicies;
}
struct annualTransitionPolicy {
intraYearTransition[] intraYearTransitions;
endOfYearTransition[] endOfYearTransitions;
int effectiveYear;
char[] effectiveYearCalendar;
char[] effectiveYearRelativity;
bool isSymmetric;
}
struct intraYearTransition {
char[] defaultAbbreviation;
uint offsetSecondsFromUT;
uint stdTimeOffsetSecondsFromUT;
char[] relativity;
uint transitionSecondsSinceStartOfDay;
transitionAnnualDate[] transitionAnnualDates;
}
struct transitionAnnualDate {
char[] calendar;
month[] months;
ushort minDayOfMonth;
dayOfWeek[] dayOfWeeks;
ushort daysOffset;
}
struct month {
ushort ordial;
}
struct dayOfWeek {
char[] key;
}
struct endOfYearTransition {
char[] defaultAbbreviation;
uint offsetSecondsFromUT;
uint stdTimeOffsetSecondsFromUT;
}
struct initialAnnualTransitionPolicy {
endOfYearTransition[] endOfYearTransitions;
}
---
which in lame-mans graph would look like this (hopefully indentation will be
right in NG reader, if not follow this url for this particular paste:
http://rafb.net/p/5hcyWq21.html)
---
struct DST {
char[] tz_key;
char[] tz_version;
char[] timestamp;
char[] geo_coordinates;
annualTransitionPolicy[] annualTransitionPolicies;
intraYearTransition[] intraYearTransitions;
char[] defaultAbbreviation;
uint offsetSecondsFromUT;
uint stdTimeOffsetSecondsFromUT;
char[] relativity;
uint transitionSecondsSinceStartOfDay;
transitionAnnualDate[] transitionAnnualDates;
char[] calendar;
month[] months;
ushort ordial;
ushort minDayOfMonth;
dayOfWeek[] dayOfWeeks;
char[] key;
ushort daysOffset;
endOfYearTransition[] endOfYearTransitions;
int effectiveYear;
char[] effectiveYearCalendar;
char[] effectiveYearRelativity;
bool isSymmetric;
initialAnnualTransitionPolicy[] initialAnnualTransitionPolicies;
endOfYearTransition[] endOfYearTransitions;
char[] defaultAbbreviation;
uint offsetSecondsFromUT;
uint stdTimeOffsetSecondsFromUT;
}
---
and now I have tried to manually initialize static data so I can make a
working model in order to feed it to my custom parser/generator for that
kind of data (paste if indentation is wrong:
http://rafb.net/p/ZhpxOw98.html:
---
static DST[] DSTData_build =
[
{
"bla",
"bla",
"bla",
"bla",
//annualTransitionPolicy[] annualTransitionPolicies;
{[
//intraYearTransition{} intraYearTransitions;
[
"bla",
0,
0,
"bla",
0,
//transitionAnnualDate{} transitionAnnualDates;
[
"bla",
//month{} months;
[
0
],
0,
//dayOfWeek{} dayOfWeeks;
[
"bla"
],
0
]
],
//endOfYearTransition{} endOfYearTransitions;
[
0,
"bla",
"bla",
true
]
]},
//initialAnnualTransitionPolicy{} initialAnnualTransitionPolicies;
{[
//endOfYearTransition{} endOfYearTransitions;
[
"bla",
0,
0
]
]}
}
];
---
full source is here: http://rafb.net/p/j33uH170.html so that I don't spam NG
with any more of text..
now as you can see, as a newcomer I simply can't get along with this
initializer things, particulary arrays and structs - and combinations of
both.
What I would like to see is some kind of a switch to a compiler or some tool
that reads in the definition of a struct like I've shown and generates a
dummy initializer upon which I can work with.
Since working on this for last couple of days on and off, I still haven't
been able to properly initialize my static data, I just can't do it - yes, I
may be retarded, I even considered to split out structs and possibly arrays
and hash their keys with their parent struct identifier, but data is too
complex to be split off in that way for now - and in the end, this SHOULD BE
EASY TO DO - all other things in D are easy, even for me, but this is
something that needs to be either better explained or reimplemented in a
more simpler fashion OR that kind of tool/switch thing that makes a dummy
initializer upon which one can work.
Thank you and my apologies for a huge rant.
More information about the Digitalmars-d
mailing list