Re: CTFE and structs question

2009-11-07 Thread Don

g wrote:

At what point  structs are supported in CTFE.
Cause this fails in dmd 2.036:

import std.metastrings;
pragma(msg,toStringNow!(A.init.a));
struct A{
uint a;
}

with this message (note that the end seems truncated):

g...@g-desktop:~/Documentos/NCHESS$ dmd oh
oh.d(3): Error: no property 'a' for type 'A'
toStringNow!(__error)
g...@g-desktop:~/Documentos/NCHESS$

also, ¿Is there any way to generate structs with ctfe or it is obligatory to 
use templates?


You can create them without templates. std.metastrings was created 
before CTFE existed, it's rather outdated. It's intended for use with 
template metaprogramming, not for use with CTFE.


You can do stuff like:

struct Foo {
   int x;
}

enum Foo b = Foo(56);


Re: Using ANSI codes

2009-11-07 Thread Stewart Gordon

Daniel Keep wrote:

http://github.com/DanielKeep/gb/blob/master/src/gb/io/Ansi.d



I noticed this comment:

/* We only need to override write for Windows, since it's the only OS
 * that doesn't understand ANSI. The ONE spot where 9x is better than
 * NT...
 */

This is incorrect - ANSI.SYS is still included in modern Windows 
versions, at least up to Vista; the only difference is whether it's 
switched on by default.


But libraries such as yours at least mean that the codes will work 
regardless of whether the end user has ANSI.SYS switched on.  But 
disp.h, which I've already mentioned, also has the same advantage.  So 
unless the OP is porting an app that already uses ANSI codes, or writing 
a terminal emulator or something


Stewart.


CTFE and structs question

2009-11-07 Thread g
At what point  structs are supported in CTFE.
Cause this fails in dmd 2.036:

import std.metastrings;
pragma(msg,toStringNow!(A.init.a));
struct A{
uint a;
}

with this message (note that the end seems truncated):

g...@g-desktop:~/Documentos/NCHESS$ dmd oh
oh.d(3): Error: no property 'a' for type 'A'
toStringNow!(__error)
g...@g-desktop:~/Documentos/NCHESS$

also, ¿Is there any way to generate structs with ctfe or it is obligatory to 
use templates?


Re: 2 File IO Questions (Phobos)

2009-11-07 Thread Zane
Frank Benoit Wrote:

> Zane schrieb:
> > Doh! I still need help with number 2, but for number 1, all I needed
> > was to use 'writeString' instead of 'write'.  Like I said, still
> > getting used to Phobos.  Like I said, I still need help on the
> > destructor question (#2).
> > 
> > Thanks, Zane
> 
> desctructors are called in not defined order. The GC puts your class
> instance 'sw' and the File instance 'sw.file' onto the list for
> destruction. So the 'sw.file' might be destructed before 'sw' is.
> Deferencing sw.file from withing sw.~this then fails.
> 
> A general rule: in a dtor, never dereference member variables.
> 
> You have to rely on the File dtor, to close himself if still open.

I understand, thanks very much!


Re: 2 File IO Questions (Phobos)

2009-11-07 Thread Frank Benoit
Zane schrieb:
> Doh! I still need help with number 2, but for number 1, all I needed
> was to use 'writeString' instead of 'write'.  Like I said, still
> getting used to Phobos.  Like I said, I still need help on the
> destructor question (#2).
> 
> Thanks, Zane

desctructors are called in not defined order. The GC puts your class
instance 'sw' and the File instance 'sw.file' onto the list for
destruction. So the 'sw.file' might be destructed before 'sw' is.
Deferencing sw.file from withing sw.~this then fails.

A general rule: in a dtor, never dereference member variables.

You have to rely on the File dtor, to close himself if still open.


Re: 2 File IO Questions (Phobos)

2009-11-07 Thread Zane
Zane Wrote:

> Hello all,
> 
> I have been looking at Phobos lately, and currently I am having some trouble 
> understanding what is going on with the following 2 trivial examples.  First 
> of all, I am using dmd v1.050.
> 
> This one prints "[‼   This is some stuff!]".  Where do the 4 prepended 
> bytes come from?
> (1) >
> 
> import std.stream;
> import std.stdio;
> 
> int main()
> {
>   char[] stuff = "This is some stuff!";
> 
>   File f = new File("stuff.txt", FileMode.Out | FileMode.In);
>   f.write(stuff);
>   f.seekSet(0);
>   stuff = f.readLine();
>   writef("[%s]", stuff);
>   f.close;
> 
>   return 0;
> }
> 
> 
> This one I wanted to have a class open a file upon initialization of an 
> instance, and close a file when the destructor is called.  I get an "Error: 
> Access Violation" unless I comment out the file.close line.  Why?  (of course 
> this example also has the same problem as the first example, but I kept the 
> first one simpler to narrow down things)
> (2) >
> 
> import std.stream;
> 
> public class StuffWriter
> {
>   File file;
> 
>   this(char[] filename)
>   {
>   file = new File(filename, FileMode.Out);
>   }
> 
>   ~this()
>   {
>   file.close; //this causes an access violation???
>   }
> 
>   public void write(char[] stuff)
>   {
>   file.write(stuff);
>   }
> }
> 
> int main()
> {
>   StuffWriter sw = new StuffWriter("stuff.txt");
>   sw.write("This is some stuff!");
> 
>   return 0;
> }
> 
> 
> Thanks!
> Zane

Doh! I still need help with number 2, but for number 1, all I needed was to use 
'writeString' instead of 'write'.  Like I said, still getting used to Phobos.  
Like I said, I still need help on the destructor question (#2).

Thanks,
Zane


2 File IO Questions (Phobos)

2009-11-07 Thread Zane
Hello all,

I have been looking at Phobos lately, and currently I am having some trouble 
understanding what is going on with the following 2 trivial examples.  First of 
all, I am using dmd v1.050.

This one prints "[‼   This is some stuff!]".  Where do the 4 prepended 
bytes come from?
(1) >

import std.stream;
import std.stdio;

int main()
{
char[] stuff = "This is some stuff!";

File f = new File("stuff.txt", FileMode.Out | FileMode.In);
f.write(stuff);
f.seekSet(0);
stuff = f.readLine();
writef("[%s]", stuff);
f.close;

return 0;
}


This one I wanted to have a class open a file upon initialization of an 
instance, and close a file when the destructor is called.  I get an "Error: 
Access Violation" unless I comment out the file.close line.  Why?  (of course 
this example also has the same problem as the first example, but I kept the 
first one simpler to narrow down things)
(2) >

import std.stream;

public class StuffWriter
{
File file;

this(char[] filename)
{
file = new File(filename, FileMode.Out);
}

~this()
{
file.close; //this causes an access violation???
}

public void write(char[] stuff)
{
file.write(stuff);
}
}

int main()
{
StuffWriter sw = new StuffWriter("stuff.txt");
sw.write("This is some stuff!");

return 0;
}


Thanks!
Zane