Questions about syntax decisions

2010-05-19 Thread F. Almeida
Dear all,

I once took a curious look at D in the past (circa 2004), and also
read the DM newsgroups at the time that Stroustrup frequently posted
here.

Now, in 2010, I've been giving D 2.0 a second chance and I'm happy
with it. Strings, dynamic arrays and hash tables couldn't be easier
to use and the option of metaprogramming presents way more
possibilities than C++.

However, I have a few questions about syntax.

1) Operator overloading

First of all, I find the operator overloading weird. This is because
I'm used to C++'s syntax operator+ and operator+=, for example,
instead of opAdd and opAddAssign. While I understand that it
gives plenty of room and freedom for introducing new operators in
the future, it nonetheless feels less natural, at the lack of a
better word. C++ has a reserved keyword operator for this, why not
using a modified syntax to this end in D? Why not using, for
example, operator(+), operator(+=), or operator(cast(real))?
This would be consistent with the simplified function template
syntax, and make operator declarations easier to read (I find that
reading the symbol itself is more intuitive when reading the source
code).

2) Operators that change context when unary or binary

I noticed that for example, ~this denotes a class destructor
(thank you for including GC AND making it optional, btw), meaning
that the ~ operator is a negation when used as a unary operator.
However, it becomes a binary concatenation operator when used with
arrays/strings, which is completely unrelated to the other use.

The same applies to templates: ! is a logical negation operator
when used as an unary operator, but it denotes a template when
introduced in a function declaration, for example.

This can be confusing for people learning the language, especially
if it's a first or second programming language. There are a number
of symbols that are not used at all and could just as easily be
used. Why not use @? The fact that D does not use the C macro
preprocessor means that # is available, for example.

Thank you for taking the time to read my questions.

Regards,
F. Almeida


Re: Questions about syntax decisions

2010-05-19 Thread Simen kjaeraas

F. Almeida cpp.tutor...@gmail.com wrote:


I have a few questions about syntax.

1) Operator overloading

First of all, I find the operator overloading weird. This is because
I'm used to C++'s syntax operator+ and operator+=, for example,
instead of opAdd and opAddAssign. While I understand that it
gives plenty of room and freedom for introducing new operators in
the future, it nonetheless feels less natural, at the lack of a
better word. C++ has a reserved keyword operator for this, why not
using a modified syntax to this end in D? Why not using, for
example, operator(+), operator(+=), or operator(cast(real))?
This would be consistent with the simplified function template
syntax, and make operator declarations easier to read (I find that
reading the symbol itself is more intuitive when reading the source
code).


The reason for the naming of operator overloads in D was to prevent
stupid overloading. That is, while the + operator might mean
concatenation, addition, and possibly other things, opAdd does not
have this ambiguity. The hope was, programmers would be discouraged
from overloading + to mean anything other than addition.

Now, opAdd and its ilk are scheduled for deprecation. The newest
operator overloading scheme is much more C++-like, only easier to
implement for a mass of related operators:
http://digitalmars.com/d/2.0/operatoroverloading.html

As you can see, the new syntax is a template system, with the
actual operator as a string parameter. This lets you use string
mixins to cover more bases with one less code.


2) Operators that change context when unary or binary

I noticed that for example, ~this denotes a class destructor
(thank you for including GC AND making it optional, btw), meaning
that the ~ operator is a negation when used as a unary operator.
However, it becomes a binary concatenation operator when used with
arrays/strings, which is completely unrelated to the other use.

The same applies to templates: ! is a logical negation operator
when used as an unary operator, but it denotes a template when
introduced in a function declaration, for example.

This can be confusing for people learning the language, especially
if it's a first or second programming language. There are a number
of symbols that are not used at all and could just as easily be
used. Why not use @? The fact that D does not use the C macro
preprocessor means that # is available, for example.


These character were chosen because not that many are available, and
because they had no binary counterpart. A negate B makes no sense.
Of the remaining character (!...@#), # is taken by compiler special
token sequences (set line number, amongst others). That leaves ~, !
and @, and frankly, A ~ B looks a lot more like concatenation to me
than does A @ B or A ! B.

As for template instantiation, we have mostly the same choices, only
not the character chosen for concatenation. Again, a choice had to
be made, and at least to me, @ implies a connection that does not
make sense, while ! does not.


This all said, there have been discussions of adding other symbols
for operator overloading, most commonly ×, for cross product.
Needless to say, this character is not easily accessible on most
keyboards, and its addition to the language has therefore not
happened.

In this vein, I gave this example two years ago:

int a = Ø; //empty set, same as = void
int[] b = [1,2,3,4,5,6];
a = readInt();

if (a ∈ b) { // Element of - in
  float c = 2.1;
  float d =  readInt();
  writefln(c ≈ ⌈d⌉ ); // Approximately equal, ceil

  myClass c = getInstance();
  if (∃c) { // c exists, i.e. !is null
writefln(√(c.foo));
  }

  ∀element∈b { // New foreach syntax!
element *= ¼;
  }
}

I hope we don't get to see this any time soon.

--
Simen


Re: Questions about syntax decisions

2010-05-19 Thread F. Almeida
I see. The templated implementation is a big improvement. If the
intention was to prevent people to turn + into concatenation for
example, I'm afraid anybody really keen on doing so would still overload
opAdd.
As a side note, C++0x is going to add a new suffix operator to C++. This
operator would be useful in its own right. Are there any plans to add a
new opSuffix!(s) operator in D?


Re: Questions about syntax decisions

2010-05-19 Thread bearophile
F. Almeida:

 Now, in 2010, I've been giving D 2.0 a second chance and I'm happy
 with it. Strings, dynamic arrays and hash tables couldn't be easier
 to use and the option of metaprogramming presents way more
 possibilities than C++.

And there's a bit of possibility still to have AST macros in D3 (but recently 
they have got less probable).


 This can be confusing for people learning the language, especially
 if it's a first or second programming language.

I have not had problems with it, and people in D.learn seem to not complain 
about it. Currently there are far worse problems in D2 syntax.


 There are a number
 of symbols that are not used at all and could just as easily be
 used. Why not use @? The fact that D does not use the C macro
 preprocessor means that # is available, for example.

@ is now used for properties (currently there is the @property).

# is used only minimally. D tries to not add syntax that is valid C syntax with 
a different semantics.


 If the intention was to prevent people to turn +
 into concatenation for example, I'm afraid anybody really
 keen on doing so would still overload opAdd.

There was opCat plus language conventions. If programmers ignore them both, 
then they are stupid programmers. A language can't be designed to avoid really 
stupid errors like that, it's a wasted energy.


 As a side note, C++0x is going to add a new suffix operator to C++. This
 operator would be useful in its own right. Are there any plans to add a
 new opSuffix!(s) operator in D?

Quite probably it will not become part of D, I think Andrei doesn't like that. 
D has Foo!xxx(...) syntax.

Bye,
bearophile


Re: Questions about syntax decisions

2010-05-19 Thread Bill Baxter
On Wed, May 19, 2010 at 3:55 AM, bearophile bearophileh...@lycos.com wrote:

 As a side note, C++0x is going to add a new suffix operator to C++. This
 operator would be useful in its own right. Are there any plans to add a
 new opSuffix!(s) operator in D?

 Quite probably it will not become part of D, I think Andrei doesn't like 
 that. D has Foo!xxx(...) syntax.

It would also be chaos in D, since D lacks fine-grained control over namespaces.

D's module==namespace model means that every module has to be designed
with the idea that every publicly visible entity is likely to end up
in the global namespace.  That doesn't really play together nicely
with the notion of defining a bunch of what are essentially
single-letter functions.

--bb


help needed with gdb

2010-05-19 Thread eles
hello everybody,

here is my setup:
Linux system 2.6.32-22-generic #33-Ubuntu SMP Wed Apr 28 13:28:05 UTC 2010
x86_64 GNU/Linux

and here is gdbtest.d

import std.stdio;

int x=0;
string t=hello!;

int main(){
writefln(x=%d,x);
writefln(t=%s,t);
return 0;
}

I compiled the latest weekly buid of gdb (7.1.50.20100519) and installed dmd
2.046 via the deb file (including the multilib tools).

then I tried to debug gdbtest. I received the errors below.

u...@system:~/Desktop/temp$ dmd gdbtest.d -g
u...@system:~/Desktop/temp$ ./gdbtest
x=0
t=hello!
u...@system:~/Desktop/temp$ gdb --version
GNU gdb (GDB) 7.1.50.20100519
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type show copying
and show warranty for details.
This GDB was configured as x86_64-unknown-linux-gnu.
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/.
u...@system:~/Desktop/temp$ gdb ./gdbtest
GNU gdb (GDB) 7.1.50.20100519
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type show copying
and show warranty for details.
This GDB was configured as x86_64-unknown-linux-gnu.
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/...
Reading symbols from /home/felix/Desktop/temp/gdbtest...done.
(gdb) break main
Breakpoint 1 at 0x804efd7
(gdb) run
Starting program: /home/felix/Desktop/temp/gdbtest
warning: the debug information found in /lib/ld-2.11.1.so does not match /
lib/ld-linux.so.2 (CRC mismatch).

[Thread debugging using libthread_db enabled]

Breakpoint 1, 0x0804efd7 in main ()
(gdb) next
Single stepping until exit from function main,
which has no line number information.
x=0
t=hello!
0xf7e45bd6 in __libc_start_main () from /lib32/libc.so.6
(gdb)
Single stepping until exit from function __libc_start_main,
which has no line number information.

Program exited normally.
(gdb)

as you see, the debug session was unsuccesful. can anybody enlighten me why?
what is the line warning: the debug information found in /lib/ld-2.11.1.so
does not match /lib/ld-linux.so.2 (CRC mismatch).?

thank you

eles



Re: Loop optimization

2010-05-19 Thread Joseph Wakeling
On 05/17/2010 01:15 AM, Walter Bright wrote:
 bearophile wrote:
 DMD compiler doesn't perform many optimizations,
 
 This is simply false. DMD does an excellent job with integer and pointer
 operations. It does a so-so job with floating point.

Interesting to note, relative to my earlier experience with D vs. C++ speed:
http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.comgroup=digitalmars.D.learnartnum=19567

I'll have to try and put together a no-floating-point bit of code to
make a comparison.

Best wishes,

-- Joe


Re: help needed with gdb

2010-05-19 Thread Robert Clipsham

On 19/05/10 20:45, eles wrote:

as you see, the debug session was unsuccesful. can anybody enlighten me why?
what is the line warning: the debug information found in /lib/ld-2.11.1.so
does not match /lib/ld-linux.so.2 (CRC mismatch).?

thank you

eles


You'd need to talk to the gdb devs about this, I'm not sure what this 
error means, given that it's to do with .so's that aren't part of the D 
distribution I'd guess it's a general gdb error. Try the following C 
code and see if you get the same error when trying to debug it:


int main()
{
  return 0;
}

$ gcc -g test.c -o test
$ gdb ./test
run the same commands as before in gdb

If you get the same errors it's either a gdb problem or a problem with 
your set up. As a side note, if you want to break at the main() function 
in your D file, you need to do 'break _Dmain'.


template mixin in class is virtual function?

2010-05-19 Thread strtr
Will template-mixin-functions always be virtual in classes?

Mixins can add virtual functions to a class
http://www.digitalmars.com/d/1.0/template-mixin.html

related:
http://odefu.blogspot.com/2008/12/composite-oriented-programming.html