D and windows console kbhit, getch etc

2012-06-02 Thread Jason King
I've seen a few messages asking how to get this to work and the 
replies that were functional involved snagging the console input 
handle and trolling for keyboard events yourself.  Since that 
code is inside the standard library (at least for DMD) I figured 
out the proper mix to make it work.


import std.c.stdio;  // I'm just using this for the printf
extern (C) void _STI_conio(); // initializes DM access ton conin, 
conout

extern (C) void _STD_conio(); // properly closes handles
extern (C) int kbhit();   // the conio function is in the DMD 
library

extern (C) int getch();   // as is his friend getch
void main() {
  int mychar;
   _STI_conio();
   while(!kbhit())
   {}
   mychar = getch();
   printf("%c\n",mychar);
   _STD_conio();
}

Works on DMD 2.059 Windows 7 64 bit.


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Jacob Carlborg

On 2012-06-02 19:59, Jonathan M Davis wrote:

On Saturday, June 02, 2012 18:40:38 Jacob Carlborg wrote:

http://d.puremagic.com/issues/show_bug.cgi?id=3345


Ah, there it is! I was _sure_ that a report for it existed, but I couldn't find
it (probably because I was searching for overload and didn't think to search
for name).

- Jonathan M Davis


In this case I search for "classinfo".

--
/Jacob Carlborg


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Andrej Mitrovic
On 6/2/12, Jonathan M Davis  wrote:
> The trick is getting Walter to agree.

The trick is getting all the people that bought TDPL to burn their
books, because by the time all these new changes are set in place the
book will have as much dead weight to it as dsource.org. It
specifically says on page 197:

"If you use an object instead of the class name when accessing a
static member, that's fine, too."


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Jonathan M Davis
On Saturday, June 02, 2012 08:41:17 H. S. Teoh wrote:
> On Sat, Jun 02, 2012 at 01:23:50AM -0700, Jonathan M Davis wrote:
> [...]
> 
> > Personally, I wish that it weren't legal to call a static function
> > with an object and that you had to explicitly use the class, but
> > that's not the way that it is in D, C++, and Java (and probably the
> > same for C#, though I'm not sure).
> 
> [...]
> 
> I've always been of the opinion that static methods should _only_ be
> callable via the class name. If an instance is not needed, then it
> shouldn't be used in the first place. Allowing the use of a class
> instance where it's not needed is needlessly confusing and prevents
> legal overloading of static methods vs. non-static methods.

The trick is getting Walter to agree. I don't know how entrenched his thinking 
on the matter is, but changing it _would_ be a breaking change, which makes it 
harder to convince him to make the change, even if he nominally agrees. And, 
of course, it's easier if someone actually goes and makes the required changes 
to the compiler and submits a pull request, but someone would obviously have 
to take the time to do that.

- Jonathan M Davis


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Jonathan M Davis
On Saturday, June 02, 2012 18:40:38 Jacob Carlborg wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=3345

Ah, there it is! I was _sure_ that a report for it existed, but I couldn't find 
it (probably because I was searching for overload and didn't think to search 
for name).

- Jonathan M Davis


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Jonathan M Davis
On Saturday, June 02, 2012 14:49:39 Dmitry Olshansky wrote:
> Generic programming benefits from it in certain scenarios

This is brought up periodically, and I don't really buy it. _Maybe_ there's a 
scenario where it would help, but typeof makes it trivial to get the type so 
that you can call a static function with the type, and I'd consider it 
questionable to insist on it with the idea that some classes would have a 
static version of the function and some a non-static version. That just seems 
like bad design.

- Jonathan M Davis


Re: delegates with references to local strings

2012-06-02 Thread Tobias Pankrath

Thank you. That works.


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Jacob Carlborg

On 2012-06-02 10:06, Namespace wrote:

Is that a joke? :D

This Code throw the error, that a call of "Load" matches both functions.
How is that possible? Even in php that works fine. Any workarounds?
I can not believe that such a simple error still exists in D.

[code]
import std.stdio;

class Foo {
public:
static Foo Load() {
Foo f = new Foo();
f.Load();

return f;
}

void Load() {

}
}

void main() {
Foo f = Foo.Load();
}
[/code]


http://d.puremagic.com/issues/show_bug.cgi?id=3345

--
/Jacob Carlborg


std.format.formattedRead and File.readln()

2012-06-02 Thread Eyyub

Hi,

This following code won't compile :

import std.stdio;
import std.format;

void main()
{
auto f = File("myfile.txt", "r");
uint life;
formattedRead(f.readln(), "Life %s", &life); // Error 1
formattedRead(cast(string)f.readln(), "Life %s", &life); 
// Error 1

string str = f.readln();
formattedRead(str, "Life %s", &life); // works
writeln(life);  
}

Error 1 :
Error: template std.format.formattedRead does not match any 
function

template declaration

C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(502): Error: 
template std.fo
rmat.formattedRead(R,Char,S...) cannot deduce template function 
from argument ty

pes !()(string,string,uint*)

Even more weird :

import std.stdio;
import std.format;

void main()
{
auto f = File("myfile.txt", "r");
uint life;
formattedRead(f.readln(), "Life %s", &life); // Error 1
writeln(typeid(f.readln())); // Error 2 O_o 
}

However :

void main()
{
auto f = File("myfile.txt", "r");
writeln(typeid(f.readln())); // works fine  
}

Error 1 && 2(sorry):
mix.d(7): Error: template std.format.formattedRead does not match 
any function

template declaration
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(502): Error: 
template std.fo
rmat.formattedRead(R,Char,S...) cannot deduce template function 
from argument ty

pes !()(string,string,uint*)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(244): Error: 
template std.conv

.toImpl does not match any function template declaration
C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(244): Error: 
template std.conv
.toImpl cannot deduce template function from argument types 
!(string)(ubyte)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(244): Error: 
template instance

 toImpl!(string) errors instantiating template
C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(3034): Error: 
template instanc

e std.conv.to!(string).to!(ubyte) error instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(3016):
instantiated fro
m here: 
textImpl!(string,string,void*,string,int,string,int,string,char,string,u

byte,string,ubyte,string,bool,string,bool,string,bool,string,bool,string,bool,st
ring,const(char)[],string,const(char)[],string)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(1078):
instantiated f
rom here: 
text!(string,void*,string,int,string,int,string,char,string,ubyte,stri

ng,ubyte,string,bool,string,bool,string,bool,string,bool,string,bool,string,cons
t(char)[],string,const(char)[],string)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(392):
instantiated fr

om here: FormatSpec!(char)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(687):
... (1 instantia

tions, -v to show) ...
C:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1574):
instantiated fr

om here: write!(TypeInfo_Array,char)
mix.d(8):instantiated from here: writeln!(TypeInfo_Array)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(3016): Error: 
template instanc
e 
std.conv.textImpl!(string,string,void*,string,int,string,int,string,char,strin

g,ubyte,string,ubyte,string,bool,string,bool,string,bool,string,bool,string,bool
,string,const(char)[],string,const(char)[],string) error 
instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(1078):
instantiated f
rom here: 
text!(string,void*,string,int,string,int,string,char,string,ubyte,stri

ng,ubyte,string,bool,string,bool,string,bool,string,bool,string,bool,string,cons
t(char)[],string,const(char)[],string)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(392):
instantiated fr

om here: FormatSpec!(char)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(687):
instantiated fro

m here: formattedWrite!(LockingTextWriter,char,TypeInfo_Array)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1574):
instantiated fr

om here: write!(TypeInfo_Array,char)
mix.d(8):instantiated from here: writeln!(TypeInfo_Array)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(1078): Error: 
template insta
nce 
std.conv.text!(string,void*,string,int,string,int,string,char,string,ubyte,s

tring,ubyte,string,bool,string,bool,string,bool,string,bool,string,bool,string,c
onst(char)[],string,const(char)[],string) error instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(392):
instantiated fr

om here: FormatSpec!(char)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(687):
instantiated fro

m here: formattedWrite!(LockingTextWriter,char,TypeInfo_Array)
C:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1574):
instantiated fr

om here: write!(TypeInfo_Array,char)
mix.d(8):instantiated from here: writeln!(TypeInfo_Array)

Any reactions ? :D

Thanks !



Re: Simplified socket creation and handling

2012-06-02 Thread Jarl André
Now the similarity to the original quickserver library in java is 
so ripped off that I had an email sent over to the author asking 
for permission to continue on the api clone or alternatively 
change the api. Comments or suggestions? sucks totally or worth a 
penny?


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread H. S. Teoh
On Sat, Jun 02, 2012 at 01:23:50AM -0700, Jonathan M Davis wrote:
[...]
> Personally, I wish that it weren't legal to call a static function
> with an object and that you had to explicitly use the class, but
> that's not the way that it is in D, C++, and Java (and probably the
> same for C#, though I'm not sure).
[...]

I've always been of the opinion that static methods should _only_ be
callable via the class name. If an instance is not needed, then it
shouldn't be used in the first place. Allowing the use of a class
instance where it's not needed is needlessly confusing and prevents
legal overloading of static methods vs. non-static methods.


T

-- 
If Java had true garbage collection, most programs would delete themselves upon 
execution. -- Robert Sewell


Re: delegates with references to local strings

2012-06-02 Thread Artur Skawina
On 06/02/12 14:01, Tobias Pankrath wrote:
> consider this:
> 
> 
> 
> import std.stdio;
> import std.string;
> 
> alias void delegate() dlgt;
> 
> int main()
> {
> dlgt[] dgs;
> string[] lines = ["line A", "line B", "line C"];
> foreach(line; lines)
> {
> writeln(line);
> dgs ~=  { writeln(line); };
> }
> 
> foreach(dg; dgs) { dg(); }
> return 0;
> }
> 
> ---
> 
> It prints every line in line and stores a delegate that does the same.
> The output is:
> 
> line A
> line B
> line C
> line C
> line C
> line C
> 
> I want it to print every line twice. How can I store the string of the 
> current iteration with a delegate? I tried dup'ing into a local, which didn't 
> help.

   dgs ~= (string l) { return { writeln(l); }; }(line);

This isn't really much different from 
http://d.puremagic.com/issues/show_bug.cgi?id=2043
but I'm not convinced the compiler should be cloning the variables here (ie if 
that "bug"
really is a bug); it certainly can be surprising though.

artur


Re: delegates with references to local strings

2012-06-02 Thread bearophile

Tobias Pankrath:

How can I store the string of the current iteration with a 
delegate?


You need to create a closure (D main returns 0 automatically):


import std.stdio, std.string;

void main() {
auto lines = ["line A", "line B", "line C"];
void delegate()[] delegates;

foreach (line; lines) {
writeln(line);
delegates ~= ((in string myLine) => { writeln(myLine); 
})(line);

}

foreach (deleg; delegates)
deleg();
}


Bye,
bearophile


delegates with references to local strings

2012-06-02 Thread Tobias Pankrath

consider this:



import std.stdio;
import std.string;

alias void delegate() dlgt;

int main()
{
dlgt[] dgs;
string[] lines = ["line A", "line B", "line C"];
foreach(line; lines)
{
writeln(line);
dgs ~=  { writeln(line); };
}

foreach(dg; dgs) { dg(); }
return 0;
}

---

It prints every line in line and stores a delegate that does the 
same.

The output is:

line A
line B
line C
line C
line C
line C

I want it to print every line twice. How can I store the string 
of the current iteration with a delegate? I tried dup'ing into a 
local, which didn't help.


Thank you for your advice.




Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Dmitry Olshansky

On 02.06.2012 14:39, Kevin Cox wrote:


On Jun 2, 2012 6:38 AM, "bearophile" mailto:bearophileh...@lycos.com>> wrote:
 >
 > Jonathan M Davis:
 >
 >
 >> Personally, I wish that it weren't legal to call a static function
with an
 >> object and that you had to explicitly use the class,
 >
 >
 > I agree.
 >
 > Bye,
 > bearophile

Same here, D 3.0?



Just rename the function. In contrast it is not easy to code up a 
workaround that will "re-enable" static as instance method back if we to 
ban it. Generic programming benefits from it in certain scenarios, while 
I personally wouldn't mind either way.


--
Dmitry Olshansky


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Kevin Cox
On Jun 2, 2012 6:38 AM, "bearophile"  wrote:
>
> Jonathan M Davis:
>
>
>> Personally, I wish that it weren't legal to call a static function with
an
>> object and that you had to explicitly use the class,
>
>
> I agree.
>
> Bye,
> bearophile

Same here, D 3.0?


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread bearophile

Jonathan M Davis:

Personally, I wish that it weren't legal to call a static 
function with an

object and that you had to explicitly use the class,


I agree.

Bye,
bearophile


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Namespace

On Saturday, 2 June 2012 at 08:24:16 UTC, Jonathan M Davis wrote:

On Saturday, June 02, 2012 10:14:51 Zhenya wrote:

I'm not sure, but it seems that this is a bug.


It's not. If nothing else, it's perfectly legal to call a 
static function with

an instance. e.g.

class C
{
static void func() {}
}

auto c = new C;
c.func();

So, that creates an ambiguity if a static and non-static 
function could have
the same name. Now, it could just assume that the instance was 
meant in this
case, but it doesn't work that way. It's just illegal to 
overload a static

function with a non-static function.

Personally, I wish that it weren't legal to call a static 
function with an
object and that you had to explicitly use the class, but that's 
not the way
that it is in D, C++, and Java (and probably the same for C#, 
though I'm not

sure).

- Jonathan M Davis


Strange thing.
But I understand, thanks. :)


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Zhenya

Understand)



Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Jonathan M Davis
On Saturday, June 02, 2012 10:14:51 Zhenya wrote:
> I'm not sure, but it seems that this is a bug.

It's not. If nothing else, it's perfectly legal to call a static function with 
an instance. e.g.

class C
{
static void func() {}
}

auto c = new C;
c.func();

So, that creates an ambiguity if a static and non-static function could have 
the same name. Now, it could just assume that the instance was meant in this 
case, but it doesn't work that way. It's just illegal to overload a static 
function with a non-static function.

Personally, I wish that it weren't legal to call a static function with an 
object and that you had to explicitly use the class, but that's not the way 
that it is in D, C++, and Java (and probably the same for C#, though I'm not 
sure).

- Jonathan M Davis


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Dmitry Olshansky

On 02.06.2012 12:06, Namespace wrote:

Is that a joke? :D

This Code throw the error, that a call of "Load" matches both functions.
How is that possible? Even in php that works fine. Any workarounds?
I can not believe that such a simple error still exists in D.

[code]
import std.stdio;

class Foo {
public:
static Foo Load() {
Foo f = new Foo();
f.Load();


In D like in Java and C++ and *ahem* unlike PHP it seems static 
functions can be called with instance variable.


Now compile may probably pick instance function here, but imagine you 
decide to refactor you instance Load function. So you rename it Create 
or OnLoad.. whatever. Now .Load suddenly calls static "overload".

So it may be another anti-hijacking thing in D.



return f;
}

void Load() {

}
}

void main() {
Foo f = Foo.Load();
}
[/code]



--
Dmitry Olshansky


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Zhenya

I'm not sure, but it seems that this is a bug.


Re: Static function conflicts with Non-Static?!

2012-06-02 Thread Zhenya

On Saturday, 2 June 2012 at 08:06:57 UTC, Namespace wrote:

Is that a joke? :D

This Code throw the error, that a call of "Load" matches both 
functions.
How is that possible? Even in php that works fine. Any 
workarounds?

I can not believe that such a simple error still exists in D.

[code]
import std.stdio;

class Foo {
public:
static Foo Load() {
Foo f = new Foo();
f.Load();

return f;
}

void Load() {

}
}

void main() {
Foo f = Foo.Load();
}
[/code]

xDDD


Static function conflicts with Non-Static?!

2012-06-02 Thread Namespace

Is that a joke? :D

This Code throw the error, that a call of "Load" matches both 
functions.
How is that possible? Even in php that works fine. Any 
workarounds?

I can not believe that such a simple error still exists in D.

[code]
import std.stdio;

class Foo {
public:
static Foo Load() {
Foo f = new Foo();
f.Load();

return f;
}

void Load() {

}
}

void main() {
Foo f = Foo.Load();
}
[/code]


Re: problem with template arguments deduction

2012-06-02 Thread Dmitry Olshansky

On 02.06.2012 11:30, Zhenya wrote:

Куда отписать-то?)


http://d.puremagic.com/issues/

--
Dmitry Olshansky


Re: problem with template arguments deduction

2012-06-02 Thread Zhenya

Куда отписать-то?)



Re: problem with template arguments deduction

2012-06-02 Thread Dmitry Olshansky

On 02.06.2012 11:23, Zhenya wrote:

Дмитрий,не подскажите как я бы мог проверить не баг ли это?


Как я и говорил отписать что это баг. Дальше это дело экспертов по 
компилятору )


Вот например последние что ты отипсывал, вполне катит для bug-репорта:
(только сократи лишний "внешний" код)

this doesn't work:

template bind(alias indeces)
{
static if(is(typeof(indeces) : int[]))
{
auto bind(D,V...)(D dg,V values)
{
static if(is(D d : R delegate(U), R, U...) &&
  is(V == Combination!(indeces,ParameterTypeTuple!D)))
{
static if(indeces.length > 1)
return
bind!(update!(indeces,indeces[0])[1..$]).bind(Curry!(indeces[0])(dg,values[0]),values[1..$]);
else static if(indeces.length == 1)
return Curry!(indeces[0])(dg,values[0]);
}
}
}
}

while this works :

class Bind(alias indeces)
{
static auto opCall(D,V...)(D dg,V values)
{
static if(is(D d : R delegate(U), R, U...) &&
  is(V == Combination!(indeces,ParameterTypeTuple!D)))
{
static if(indeces.length > 1)
return 
Bind!(update!(indeces,indeces[0])[1..$])(Curry!(indeces[0])(dg,values[0]),values[1..$]);

else
return Curry!(indeces[0])(dg,values[0]);
}
}
}


--
Dmitry Olshansky


Re: problem with template arguments deduction

2012-06-02 Thread Zhenya
Дмитрий,не подскажите как я бы мог 
проверить не баг ли это?


Re: Bypassing const with a union

2012-06-02 Thread Dmitry Olshansky

On 02.06.2012 3:28, Era Scarecrow wrote:

On Friday, 1 June 2012 at 23:14:14 UTC, Dmitry Olshansky wrote:

There is also cast() that just cancels out all const/shared/immutable.


Only the first level, transitive const/immutable don't go away in my
experience. Perhaps I'm doing it wrong, or perhaps it's just a
protective feature to protect the lower levels so you don't get C++'s
const system.


Breaking the const system while your still building/preparing the new
object should be allowed (as with the slice example)


Yes in constructor. Or by constructing incrementally a mutable object,
inside pure function e.g. compiler can convert to immutable on return
(auto-magically).


Which is sometimes where I'm getting stuck. In the constructor it
complains about not convertible from const to mutable even if the object
being passed back will be const/immutable.



Mmm IRC you can assign each field in const constructor only once. After 
that it's cooked and treated as const from now on.



In my limited experience where it is emulating a slice I would need an
exact copy of the struct and then modify what I need before passing it
back; cast doesn't do the job, and manually copying const objects to
non-const is an annoyance or a pain in it's own regard.



Sure it is.

--
Dmitry Olshansky