Re: Question about typeof(this)

2010-09-14 Thread Don

Jacob Carlborg wrote:

On 2010-09-10 16:53, Pelle wrote:

On 09/10/2010 03:20 PM, Jacob Carlborg wrote:

On 2010-09-07 22:32, Don wrote:

Jacob Carlborg wrote:

On 2010-09-07 17:29, Don wrote:

Jacob Carlborg wrote:

I'm reading http://www.digitalmars.com/d/2.0/declaration.html#Typeof
where it says:

typeof(this) will generate the type of what this would be in a
non-static member function, even if not in a member function. 

From that I got the impression that the code below would print the
same result, but it doesn't. It prints:

main.Bar
main.Foo

instead of:

main.Foo
main.Foo

Is this a bug or have I misunderstood the docs?


typeof(this) gives the *compile-time* type of this. Inside Bar, it 
has

to return 'Bar'.
typeid(this) gives the *runtime* type of this. So it can work that
it's
Bar is actually a Foo.


I know that typeof(this) is a compile time expression but in this case
I think the compiler has all the necessary information at compile
time. Note that I'm not calling method on a base class reference,
I'm calling it on the static type Foo. In this case I think
typeof(this) would resolve to the type of the receiver, i.e. the type
offoo.


Even though in this instance it could work out which derived class is
being used, it's not allowed to use that information while compiling
method(). There is only ONE function method(), and it has to work for
Bar, and all classes derived from Bar.


I think Scala can handle this problem, the following text is a snippet
from a paper called Scalable Component Abstractions (link at the
bottom), page 4 Type selection and singleton types:

class C {
protected var x = 0;
def incr: this.type = { x = x + 1; this }
}

class D extends C {
def decr: this.type = { x = x - 1; this }
}

Then we can chain calls to the incr and decr method, as in

val d = new D; d.incr.decr;

Without the singleton type this.type, this would not have
been possible, since d.incr would be of type C, which does
not have a decr member. In that sense, this.type is similar
to (covariant uses of ) Kim Bruce's mytype construct [5].

I'm not very familiar with Scala but the above code example seems to
work as I want typeof(this) to work.

http://www.scala-lang.org/sites/default/files/odersky/ScalableComponent.pdf 






You can do this in D, but the syntax is clumsy. And it uses templates.

class C {
int x;
T incr(this T)() {
x += 1;
return cast(T)this; // clumsy, but always works (?)
}
}
class D : C {
T decr(this T)() {
x -= 1;
return cast(T)this;
}
}

void main() {
D d = new D;
d.decr.incr.decr.incr.incr;
writeln(d.x);

}


I've done some testing with this template parameters and it actually 
works as I want it to, but I don't understand why it has to be a 
template. Both templates and typeof are resolved at compile time so I 
don't understand why it can't work with typeof. I have this example now:


module main;

import std.stdio;

class Base
{
void foo (this T) ()
{
writeln(T.stringof);
writeln(typeof(this).stringof);
}
}

class Foo : Base { }

void main()
{
auto foo = new Foo;
foo.foo;
}

It will print:

Foo
Base

To me this template parameters look unnecessary, typeof should be able 
to handle this. In the above foo method I think T should be equal to 
typeof(this).



There are only two ways to have the behaviour you want:
(1) make it a template, so that the function is duplicated for every 
derived class (that's what this solution is doing); OR

(2) use runtime type information.

Both of these have unacceptable consequences in general.


Re: [SO] D support for COM

2010-09-14 Thread BLS

On 13/09/2010 20:58, Jesse Phillips wrote:

There is a question on using COM with D and how it simplifies using COM[1]. I 
haven't done it myself and don't have any examples.

http://stackoverflow.com/questions/3698910/d-support-for-com


http://www.dsource.org/projects/juno/wiki

Juno contains a TypeLibraryImport tool and set of modules making COM 
client respective server programming a piece of cake. Unfortunately Juno 
is not in sync with the latest D2 compiler.


Bjoern



Re: string to char*

2010-09-14 Thread klickverbot

On 9/11/10 3:00 PM, shd wrote:

Hello,
I'm having a problem in passing a value to char* expecting function
in D 2.0. Already tried:

to!(char*)(my string);

but it seems like there (Phobos) is no template like this. Then,
tried:

cast(char*)to!(char[])(my string)

which looked ok, but i think it's not a proper way to do that. Most
strings converted this way works properly, but once:

char* string1 = cast(char*)to!(char[])(my string 1);
char* string2 = cast(char*)to!(char[])(my string 2);

resulted:
string1 = my string 1
string2 = my string 1my string 2

I can't manage this problem, could You hint me?



Use std.string.toStringz to convert a D string to a C null-terminated one.

This asymmetry (no to!(char*)(string)) has been discussed once, but I 
can't remember the reason why it was not implemented right now.


Re: Input handling? (newbie alert!)

2010-09-14 Thread Ali Çehreli

Cavalary wrote:

 Yeah, one of the reasons why it made sense to me I guess, as the
 term's informatica in Romanian

Getting back to the tutorial question, with the remote chance that you 
know Turkish as well, there is


  http://ddili.org/ders/d/index.html

That tutorial still uses std.cstream (din.readf, etc.) though. I tried 
to convert it to stdin.readf but could not go far with 2.048. I am 
waiting for future releases with more novice-friendly standard input 
handling.


Ali



[OT] Re: Input handling? (newbie alert!)

2010-09-14 Thread Ali Çehreli

bearophile wrote:

 I think the term Informatics is better than Computer Science for this
 field we are talking about

On a related note, programmers are called software engineers in the 
US, at least in Silicon Valley.


 Computer Science is mostly misleading, both words don't fit very well
 with what this field is.

Agreed. About programming, I am pretty convinced that it is a craft more 
than anything else.


 My English grammar has some evident holes

I know that I am not kualified ;) to judge, but your English is just 
perfect.


Ali


Re: [OT] Re: Input handling? (newbie alert!)

2010-09-14 Thread Jonathan M Davis
On Tuesday, September 14, 2010 10:41:53 Ali Çehreli wrote:
 bearophile wrote:
   I think the term Informatics is better than Computer Science for
   this field we are talking about
 
 On a related note, programmers are called software engineers in the
 US, at least in Silicon Valley.

Well, software engineers are programmers, but programmers aren't necessarily 
software engineers - just like there is a software engineering major in 
addition 
to computer science. Their focus is different.

   Computer Science is mostly misleading, both words don't fit very well
   with what this field is.
 
 Agreed. About programming, I am pretty convinced that it is a craft more
 than anything else.

I believe that the idea is that computer science is the study of computers - 
like how political science is the study of politics. However, computer science 
only covers the software side of things, isn't entirely tied to a computer, and 
is really more like math or engineering (though when you focus on the 
engineering side of things, you start straying into software engineering rather 
than computer science) rather than science, since (aside from debugging) it 
really has nothing to do with the scientific method. So, while the basic idea 
behind the name is solid, it doesn't really fit very well when you get down to 
the details.

- Jonathan M Davis


Re: Am I doing this right? (File byChunk)

2010-09-14 Thread Jonathan M Davis
gmx.com seems to deal with mailing list e-mails correctly (by actually putting 
the ones you sent to the list in your inbox when they come from the list), and 
it has free imap and lots of disk space just like gmail (not to mention that it 
uses proper folders instead of labels), so I've switched over to it. It's much 
more pleasant to deal with (at least when using it from an e-mail client; 
whether the user interface for the site itself is better is more debatable). 
And 
switching e-mail addresses gives me a chance to reduce the spam that I get. :)

- Jonathan M Davis


Re: Formating decimal numbers with commas (1,000.00)

2010-09-14 Thread Stewart Gordon

On 14/09/2010 21:00, jicman wrote:


Greetings.

I have been trying, for more than an hour, to get information on how
to format a number (2342.23) to $2,342.23.


Just wondering, where have you been searching for this information?


I can write a little function to do this, but doesn't format already
has this builtin?

snip

If the documentation
http://www.digitalmars.com/d/1.0/phobos/std_format.html
is to go by, there doesn't seem to be any such feature, and a quick look 
through the code doesn't reveal one either.


But there are many things std.format doesn't do.  I'd imagine that 
someone's written a more powerful number formatting library module, but 
I don't know of it.  Maybe that someone'll find this thread and 
enlighten us.


Stewart.


Re: Formating decimal numbers with commas (1,000.00)

2010-09-14 Thread Jonathan M Davis
On Tuesday, September 14, 2010 13:00:22 jicman wrote:
 Greetings.
 
 I have been trying, for more than an hour, to get information on how to
 format a number (2342.23) to $2,342.23.  I can write a little function to
 do this, but doesn't format already has this builtin?  I searched for
 vsprintf(), printf, etc., and they all have a glyphic way of saying
 things.
 
 I know that I can use,
 
 real amt = 2342.23;
 char[] z = format(%.2f,amt);
 
 but, I want to do the $2,342.23.
 
 Can anyone help a poor man? :-)
 
 thanks,
 
 josé

format() should work with the same types of parameters that printf() works with 
( http://www.cplusplus.com/reference/clibrary/cstdio/printf/ ). That should 
allow you to set the precision that you want, but I don't believe that it does 
anything with commas. If you want that, I believe that you're going to have to 
code it yourself.

- Jonathan M Davis


Re: Formating decimal numbers with commas (1,000.00)

2010-09-14 Thread bearophile
Jonathan M Davis:
 If you want that, I believe that you're going to have to code it yourself.

It's a common need, I have it in my dlibs1, and I think that eventually it 
needs to be added to Phobos (so far none of my patches to Phobos I have put in 
Bugzila has being used, I don't know why, so I have lost part of the drive to 
write code for Phobos).

Bye,
bearophile


Re: Formating decimal numbers with commas (1,000.00)

2010-09-14 Thread jicman
Stewart Gordon Wrote:

 On 14/09/2010 21:00, jicman wrote:
 
  Greetings.
 
  I have been trying, for more than an hour, to get information on how
  to format a number (2342.23) to $2,342.23.
 
 Just wondering, where have you been searching for this information?

Google gave me a bunch of hits, but none where useful.  I can go back and get 
them for you, but all they have is the man pages or documentation on *print*.  
PHP does have nice little functions (format_number, format_currency, etc.) that 
provide the results I want.  I was hoping that doFormat would allow somekind of 
formatting such as:

doFormat($%,.2f,1234.45);

and return $1,234,45. or something like that.  Adding the $ at the beginning is 
easy. .-)  Or, 

doFormat($10,.2f,1234.45);

would return

$1,234.56

where only 10 characters are allowed from the left side of the period back.

Anyway, you get what I am saying.

josé



Re: Formating decimal numbers with commas (1,000.00)

2010-09-14 Thread jicman
bearophile Wrote:

 Jonathan M Davis:
  If you want that, I believe that you're going to have to code it yourself.
 
 It's a common need, I have it in my dlibs1, and I think that eventually it 
 needs to be added to Phobos (so far none of my patches to Phobos I have put 
 in Bugzila has being used, I don't know why, so I have lost part of the drive 
 to write code for Phobos).
 
 Bye,
 bearophile

Where can I download your dlibs1 library?  It would be nice to have people 
continue to update phobos.  Not everyone uses tango.



Re: Formating decimal numbers with commas (1,000.00)

2010-09-14 Thread Jonathan M Davis
On Tuesday 14 September 2010 18:39:16 jicman wrote:
 bearophile Wrote:
  Jonathan M Davis:
   If you want that, I believe that you're going to have to code it
   yourself.
  
  It's a common need, I have it in my dlibs1, and I think that eventually
  it needs to be added to Phobos (so far none of my patches to Phobos I
  have put in Bugzila has being used, I don't know why, so I have lost
  part of the drive to write code for Phobos).
  
  Bye,
  bearophile
 
 Where can I download your dlibs1 library?  It would be nice to have people
 continue to update phobos.  Not everyone uses tango.

Phobos gets updated all the time. There's quite a bit of work being done on it 
actually. But there are only so many people with commit access, and they only 
have so much time. So, things only get done so fast - both the stuff that 
they're 
working on and how many patches that they're able to examine for possible 
inclusion.

- Jonathan M Davis


Re: Formating decimal numbers with commas (1,000.00)

2010-09-14 Thread bearophile
jicman:
 Where can I download your dlibs1 library?

This is D1 code, it's slow because I usually print only few numbers like this:


string thousands(TyIntegral)(TyIntegral n, string separator=_) {
static assert (IsType!(TyIntegral, byte, ubyte, short, ushort, int, uint, 
long, ulong),
   thousands() accepts only a integral numeric argument.);
string ns = toString(abs(n)).reverse;
string[] parts;
for (int i = 0; i  ns.length; i += 3)
parts ~= ns[i .. (i+3)  length ? (i+3) : length];
return (n  0 ? - : ) ~ parts.join(separator).reverse;
}

Bye,
bearophile


Re: Formating decimal numbers with commas (1,000.00)

2010-09-14 Thread jicman
bearophile Wrote:

 Jonathan M Davis:
  If you want that, I believe that you're going to have to code it yourself.
 
 It's a common need, I have it in my dlibs1, and I think that eventually it 
 needs to be added to Phobos (so far none of my patches to Phobos I have put 
 in Bugzila has being used, I don't know why, so I have lost part of the drive 
 to write code for Phobos).
 
 Bye,
 bearophile

Where can I download your dlibs1 library?  It would be nice to have people 
continue to update phobos.  Not everyone uses tango.



Re: Formating decimal numbers with commas (1,000.00)

2010-09-14 Thread jicman
bearophile Wrote:

 jicman:
  Where can I download your dlibs1 library?
 
 This is D1 code, it's slow because I usually print only few numbers like this:
 
 
 string thousands(TyIntegral)(TyIntegral n, string separator=_) {
 static assert (IsType!(TyIntegral, byte, ubyte, short, ushort, int, uint, 
 long, ulong),
thousands() accepts only a integral numeric argument.);
 string ns = toString(abs(n)).reverse;
 string[] parts;
 for (int i = 0; i  ns.length; i += 3)
 parts ~= ns[i .. (i+3)  length ? (i+3) : length];
 return (n  0 ? - : ) ~ parts.join(separator).reverse;
 }

Way over my head. :-)  Thanks, though.

josé