First Impressions

Geoff Carlton gcarlton at iinet.net.au
Thu Sep 28 20:53:17 PDT 2006


Jarrett Billingsley wrote:

> It does look nicer.  I suppose the counterargument would be that having an 
> alias char[] string might not be portable -- what about wchar[] and dchar[]? 
> Would they be wstring and dstring?  Or would we choose wchar[] or dchar[] to 
> be string to be more forward-thinking (since languages like Java and C# 
> already use UTF-16 as the default string type)?
> 

I'm a fan of utf-8 so it would seem natural to have string, wstring, and 
dstring.  IMO utf-16 is backward thinking, and has the dubious property 
of being mostly fixed width, except when its not.  And even utf-32 isn't 
one-to-one in terms of glyphs rendered on screen.

Anyway, as a low level programmer, I appreciate that its all based on 
very powerful and flexible arrays.  But as a high level programmer, I 
don't want to be reminded of that fact every time I need a to use a string.


> Unfortunately the way Lua does "foreach" iteration is exactly the inverse of 
> how D does it.  Lua gets an iterator and keeps calling it in the loop; D 
> gives the loop (the entire body!) to the iterator function, which runs the 
> loop.  So it's something like a "true" iterator as described in the Lua 
> book:
>


Ok, although the advantage of the first method is that you write the 
iterator once, and then its easy to use for all clients.  Wrapping up 
the loop in a function is just backward, although it is much more 
palatable in the inline format than a clunky out of line functor or 
using _1, _2 hackery magic.

As an example, I love the fact that I can do this in lua:

for r1 in rooms_in_level(lvl) do
   for r2 in rooms_in_level(lvl) do
     for c in connections(r1, r2) do
       print("got connection " .. c)
     end
   end
end

I wrote Floyd's algorithm in lua in the time it would take me in C++ to 
not even finish thinking about what structures, classes, vectors I would 
use.  I imagine D would be as easy, although not as nice as the above style.

> 
> D does it this way I guess to make it easier to write iterators.  Since 
> you're limited to one return value, it's simpler to make the iterator a 
> callback and pass the indices into the foreach body than it is to make the 
> iterator return multiple parameters through "out" parameters.  That, and 
> it's easier to keep track of state with a callback iterator.  (I'm going 
> through which to use in a Lua-like language that I'm designing too!)
>

Multiple returns would be tricky.  C++ looks like its getting there with 
std::tuple and std::tie, but as always the downside is the sheer 
clunkiness.  As hetrogenous arrays aren't in the core language for 
either C++ or D, its tricky to come up with a clean solution.

Designing a language would be great fun, and I think lua has done a 
great many things right.  Not sure about the typeless state though, it 
gets messy with large projects. Still, no templates (or rather, every 
function is like a template).




More information about the Digitalmars-d mailing list