November 18, 2006 Re: What's left for 1.0? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote: > Walter Bright wrote: > > C can't do any of those things. > > Sure it can. > >>> * No way to initialize a static array without counting elements >>> static byte[???] imageData = [...]; // i hope you like counting! > > C has no problem with this. I do it all the time: > > static const unsigned char[] = { 1,2,3,4,5,6,7, 255 }; True, but that's only because C doesn't have dynamic arrays. In D, const char[] c = [ 1,2,3,4,5,6,7, 255 ]; works fine, though it's a dynamic array. >>> * No way to initialize a dynamic array with a known length >>> byte[] imageData; imageData.length = 5; // two steps - meh > > C has no problem with this (using malloc, its own concept of "dynamic arrays"): > > byte* imageData = (byte*)malloc(5*sizeof(byte)); D: auto imageData = new byte[5]; > A better comparison is C++, which has no problem with it's std library vector class: > > vector<int> imageData(5); Yah got me there, C++ is one character shorter. >>> * No way to initialize array of strings >>> char[][] list = ["eggs","bacon","milk","break"]; //uh uh > > C can do this: > > char *list[] = { "eggs","bacon","milk","break" }; So can D: char *list[] = [ "eggs","bacon","milk","break" ]; char[] list[] = [ "eggs","bacon","milk","break" ]; >>> * No way to initialize non-static struct >>> Point p = { x:1.0, y:2.0 }; // nope...not static > > C has no problem with that either: > > struct Point { float x, y; }; > void foo() { > Point p = {1.0,2.0}; > } True. I forgot it could (replacing "Point" with "struct Point"). |
November 18, 2006 Re: What's left for 1.0? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Ok, after reading this thread and testing some code it's obvious that I need to read again the D documentation, and after that, read it again ;-)
Probably, for most of us, there are lots of hidden little gems in D ... and the docs need some update and/or extension to show this properly. Maybe for v1.0 we could organize a collaborative project for 'The D Tutorial' ...
Samuel.
Walter Bright escribió:
> Bill Baxter wrote:
>> Walter Bright wrote:
>> > C can't do any of those things.
>>
>> Sure it can.
>>
>>>> * No way to initialize a static array without counting elements
>>>> static byte[???] imageData = [...]; // i hope you like counting!
>>
>> C has no problem with this. I do it all the time:
>>
>> static const unsigned char[] = { 1,2,3,4,5,6,7, 255 };
>
> True, but that's only because C doesn't have dynamic arrays. In D,
>
> const char[] c = [ 1,2,3,4,5,6,7, 255 ];
>
> works fine, though it's a dynamic array.
>
>>>> * No way to initialize a dynamic array with a known length
>>>> byte[] imageData; imageData.length = 5; // two steps - meh
>>
>> C has no problem with this (using malloc, its own concept of "dynamic arrays"):
>>
>> byte* imageData = (byte*)malloc(5*sizeof(byte));
>
> D: auto imageData = new byte[5];
>
>> A better comparison is C++, which has no problem with it's std library vector class:
>>
>> vector<int> imageData(5);
>
> Yah got me there, C++ is one character shorter.
>
>
>>>> * No way to initialize array of strings
>>>> char[][] list = ["eggs","bacon","milk","break"]; //uh uh
>>
>> C can do this:
>>
>> char *list[] = { "eggs","bacon","milk","break" };
>
> So can D:
> char *list[] = [ "eggs","bacon","milk","break" ];
> char[] list[] = [ "eggs","bacon","milk","break" ];
>
>
>>>> * No way to initialize non-static struct
>>>> Point p = { x:1.0, y:2.0 }; // nope...not static
>>
>> C has no problem with that either:
>>
>> struct Point { float x, y; };
>> void foo() {
>> Point p = {1.0,2.0};
>> }
>
> True. I forgot it could (replacing "Point" with "struct Point").
|
November 18, 2006 Re: What's left for 1.0? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Maybe this is interesting for you: http://www.dprogramming.com/dstring.php Alex Bill Baxter wrote: > Walter Bright wrote: >> Daniel Keep wrote: >>> Also, I think this whole discussion is highlighting a misunderstanding >>> on how strings work in D. Some people seem to be looking at D's string >>> support and thinking "Oh, it looks just like a scripting language, so >>> <X> should work the same; what the?! It doesn't?! Must be broken!" >>> They don't seem to understand *why* we have char, wchar and dchar. I >>> think it's time we had an article either in a D manual (do we even, >>> strictly speaking, HAVE a manual for D?[1]) or somewhere on the website >>> so we can say: >>> >>> "No, it's not broken; it's just different. Go here and all shall >>> become clear." >> >> Yes, I think you're right. Once one has a good handle on what UTF-8 is (and UTF-16 and UCS-4), it all makes sense. D provides several different ways of looking at characters (and strings) and none of them are quite like C++ (which essentially has no support for international characters) or like scripting languages (which hide all the details, making them inefficient). > >> I've thought more than once about writing an article about it, but got distracted by other things. > > I would like to try to use dchar[] as my standard string type, however it doesn't seem to be supported as well by the compiler and library as char[] is. For instance std.string has basically nothing for dchar[]s. > > And there doesn't seem to be a dchar string literal syntax. At least I couldn't find it. The section on StringLiterals linked to from the expressions page is non-existant. > > --bb |
November 18, 2006 Re: What's left for 1.0? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote: > Bill Baxter wrote: > >> So, what's left on everyone's lists for D1.0 must-have features? > > > For a language that has unheard of features like variadic template support (!) one thing that seems oddly immature to me is support for static initializers. Things like: > > * No way to initialize a static array without counting elements > static byte[???] imageData = [...]; // i hope you like counting! Agreed. No idea off the top of my head, though, except maybe allowing something like: # static byte[$] imageData = [...]; > * No way to initialize a dynamic array with a known length > byte[] imageData; imageData.length = 5; // two steps - meh # byte[] imageData = new byte[5]; Works right now. > * No way to initialize array of strings > char[][] list = ["eggs","bacon","milk","break"]; //uh uh There are two ways to do it, right now... but I do still agree they aren't particulary elegant. Way #1: # char[][] list = [cast(char[]) "eggs", "bacon", "milk"]; Way #2: # import cashew .utils .array ; # # auto list = array!(char[])("eggs", "bacon", "milk"); > * No way to initialize non-static struct > Point p = { x:1.0, y:2.0 }; // nope...not static This has annoyed me as well. We can get a tuple of a struct now, for goodness sakes; surely we can accomplish a simple little initializer! > * No way to initialize associative array > char[int] strTable = {"hello":5, "hi":2, "greetings":9}; // no way Ditto. The closest I can offer is Cashew again: # import cashew .utils .array ; # # auto strTable = assoc!(int, char)( # [5 , 2 , 9 ], # ["hello", "hi", "greetings"] # ); I made the thing, and yet even I cringe. > > I know things have gotten much better since the old days, when there weren't even array literals (yikes!), but it still looks pretty primitive in some ways compared to C. > > --bb Its the paradox/irony of D, it seems. Hopefully these things get taken care of in the next month or so, before its "too late"! -- Chris Nicholson-Sauls |
November 18, 2006 Re: What's left for 1.0? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote:
>> C has no problem with that either:
>>
>> struct Point { float x, y; };
>> void foo() {
>> Point p = {1.0,2.0};
>> }
>
>
> True. I forgot it could (replacing "Point" with "struct Point").
That is not C, that is C++, and gives a compiler error:
"error: `Point' undeclared (first use in this function)"
In C you need the usual typedef-on-the-go workaround:
typedef struct Point { float x, y; } Point;
--anders
|
November 18, 2006 Re: What's left for 1.0? - string class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | Anders F Björklund wrote: > Aarti_pl wrote: > >> Making size of char equal to 4 bytes, and having string class, which can optimize different encodings would allow to get rid of all magic... :-) > > I think that using dchar and the proposed "dstring" struct* would work ? (not that I think it becomes less magic if you hide it behind a curtain) > Yes, that was my original proposal: dstring should be putted in standard library. Or at least: alias char[] string; I would be already happy about it. Processing utf-8 strings in different places in std library/compiler/user code instead of one string class I would rather consider bad practice... > We need D "char" to be a 1-byte type for all future, or this won't work: > extern (C) int printf(char *, ...); Is it really not possible to solve? Maybe putting in extern(C) ubyte instead of char and allowing compiler silently translate it in this block to char for linking probably would solve problem... > > --anders > > * http://www.dprogramming.com/dstring.php -- Regards Marcin Kuszczak (Aarti_pl) |
November 18, 2006 Re: What's left for 1.0? - string class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcin Kuszczak | Marcin Kuszczak wrote: > Yes, that was my original proposal: dstring should be putted in standard > library. Or at least: alias char[] string; I would be already happy about > it. I think both should go in Phobos. Both string alias and dstring struct. > Processing utf-8 strings in different places in std library/compiler/user > code instead of one string class I would rather consider bad practice... I don't think classes should be required. Built-in UTF strings are nice. >>We need D "char" to be a 1-byte type for all future, or this won't work: >>extern (C) int printf(char *, ...); > > Is it really not possible to solve? Maybe putting in extern(C) ubyte instead > of char and allowing compiler silently translate it in this block to char > for linking probably would solve problem... It's not a technical problem. D wants the familiar C types as a feature. I think using char and char[] (as "string") as an improvement over C is OK, and then recommend dchar and dstring as the "D way" of doing it ? And then if you use a real OOP stdlib like Mango, you move to String... (using classes also offers one solution to the mutable/immutable issue) Then D covers the entire "spectrum", from printf to writef to Writers. --anders |
November 18, 2006 Re: What's left for 1.0? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote:
>> However, I don't known how to put in a BOM.
>
> One way is with Notepad - "Save As" and pick UTF-8.
I highly recommend *not* using a BOM with UTF-8,
and instead only use it with UTF-16 and UTF-32...
There are several Unix tools that choke on the BOM,
adding it will make those fail for no added value ?
I think the OP was using a non-Unicode text format,
and as we know that isn't supported at all by D...
More entries for the "D future features not planned":
Q: Will D support non-UTF text editors or consoles ?
A: No. Nein. Non. Nej. いいえ.
--anders
|
November 18, 2006 Re: What's left for 1.0? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote: > Bill Baxter wrote: >> Walter Bright wrote: >> > C can't do any of those things. >> >> Sure it can. >> >>>> * No way to initialize a static array without counting elements >>>> static byte[???] imageData = [...]; // i hope you like counting! >> >> C has no problem with this. I do it all the time: >> >> static const unsigned char[] = { 1,2,3,4,5,6,7, 255 }; > > True, but that's only because C doesn't have dynamic arrays. In D, > > const char[] c = [ 1,2,3,4,5,6,7, 255 ]; > > works fine, though it's a dynamic array. Yep it's great that D has built-in dynamic arrays, but the point is that the syntax for dynamic arrays is getting in the way of static arrays, making something that's simple in C become hard in D. If you want a static array you have no choice right now but to count up the elements, or deliberately use the wrong length to trigger compiler errors that will tell you the right length. If D has some other convenient and portable way to embed data like icons and images into one's exe, then I'd be interested in hearing about it, but for right now it seems to me like working with static data is a pain in D. Anyway, this one thing has actually been the biggest annoyance I've run across in trying to port C++ code to D. Most things become easier, but this one thing is harder in D. >>>> * No way to initialize a dynamic array with a known length >>>> byte[] imageData; imageData.length = 5; // two steps - meh >> >> C has no problem with this (using malloc, its own concept of "dynamic arrays"): >> >> byte* imageData = (byte*)malloc(5*sizeof(byte)); > > D: auto imageData = new byte[5]; Ok, that's good enough for that one. Thanks. I didn't realize new'ing an array like that was allowed (and compatible with setting .length). >>>> * No way to initialize array of strings >>>> char[][] list = ["eggs","bacon","milk","break"]; //uh uh >> >> C can do this: >> >> char *list[] = { "eggs","bacon","milk","break" }; > > So can D: > char *list[] = [ "eggs","bacon","milk","break" ]; > char[] list[] = [ "eggs","bacon","milk","break" ]; Nope, D cannot: dchar.d(12): Error: cannot implicitly convert expression ("bacon") of type char[5] to char[4] As someone pointed out though, you can make it work by making everything dynamic: char[] list[] = [ "eggs"[],"bacon","milk","break" ]; But, then you're making everything dynamic when it should be static. Correct me if I'm wrong, but in the C/C++ version of this above, basically everything is static. The strings will be embedded into the exe, and the array will just consist of pointers directly to those strings in the data segment. But in the D version you'll have that same data in the data segment, and then you'll also make dynamic copies of all the data onto the heap at runtime. I don't want two copies of all my static data, especially if one of those copies requires runtime heap allocations. >>>> * No way to initialize non-static struct >>>> Point p = { x:1.0, y:2.0 }; // nope...not static >> >> C has no problem with that either: >> >> struct Point { float x, y; }; >> void foo() { >> Point p = {1.0,2.0}; >> } > > True. I forgot it could (replacing "Point" with "struct Point"). Right -- struct Point. It's been ages since I had to deal with C's "typedef struct foo { } foo;" sillyness. --bb |
November 18, 2006 Re: What's left for 1.0? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote: > Bill Baxter wrote: >> So, what's left on everyone's lists for D1.0 must-have features? > > For a language that has unheard of features like variadic template support (!) one thing that seems oddly immature to me is support for static initializers. Things like: > > * No way to initialize a static array without counting elements > static byte[???] imageData = [...]; // i hope you like counting! Maybe: static imageData = [cast(byte) ...]; > * No way to initialize a dynamic array with a known length > byte[] imageData; imageData.length = 5; // two steps - meh byte[] imageData = new byte[5]; > * No way to initialize array of strings > char[][] list = ["eggs","bacon","milk","break"]; //uh uh > > * No way to initialize non-static struct > Point p = { x:1.0, y:2.0 }; // nope...not static > > * No way to initialize associative array > char[int] strTable = {"hello":5, "hi":2, "greetings":9}; // no way The rest would be nice to have, though. Sean |
Copyright © 1999-2021 by the D Language Foundation