D dropped in favour of C# for PSP emulator
H. S. Teoh
hsteoh at quickfur.ath.cx
Fri May 11 11:40:22 PDT 2012
On Fri, May 11, 2012 at 02:25:06PM -0400, Jonathan M Davis wrote:
> On Friday, May 11, 2012 11:22:13 H. S. Teoh wrote:
> > > 6. The 'in' operator returns a pointer (instead of a boolean).
> > > Which is fine, except that it's not what you'd expect in any
> > > languages other than C/C++. i.e. Non-intuitive
> >
> > Actually, that is a WAT even for somebody coming from C/C++.
>
> Really? That's pretty much exactly what I would have expected, and it
> would really suck if it returned a bool. It's like what you get with
> find on std::map, only it's a pointer instead of an iterator.
[...]
I was speaking for myself. My WAT stemmed from the fact that there's
already a [] lookup operator for AA's, so intuitively-speaking, I'd
expect that it should return some kind of null or sentinel value if the
key didn't exist yet. But then I saw that it threw an exception when the
key didn't exist, so I thought, yikes, better check for existence before
doing the lookup. Oh look, there's an in operator, that's probably what
I want. It returns a bool, right? WAT? It's a pointer? It just seems to
go against the grain of most of the rest of D, where pointers for the
most part don't need to be used at all.
Then there's this whole other method called get(), which is what I
would've expected to do what in does today, but instead it has this odd
second parameter for default value.
I guess what I'm trying to say is, while the _semantics_ of the various
AA lookup operations totally make sense, the choice of syntax/names is
rather counterintuitive. The thing is, AA's are built into the language,
so one would expect the compiler would be smart enough to optimize for
common usage patterns, like:
int[string] aa;
int x;
if ("abc" in aa) { // assuming in returns bool
x = aa["abc"];
// compiler should be smart enough to optimize away the
// second lookup
} else {
x = 123;
}
// compiler should know enough to rewrite this code into:
// int x = aa.get("abc", 123);
The current implementation makes sense only in retrospect. It's
definitely a WAT for newbies.
T
--
What are you when you run out of Monet? Baroque.
More information about the Digitalmars-d
mailing list