Re: Ini parsing library in D ?

2011-02-28 Thread Jacob Carlborg

On 2011-02-28 08:04, Tarun Ramakrishna wrote:

Hi,

Do we have a ini parser in D somewhere ? If not, is there some
documentation anywhere that tells one how to wrap a simple C++ library
like simple ini ? (Of course it isn't difficult to build a quick
parser, just that someone would done this already)

Thanks,
Tarun


There is one in Tango, tango.io.stream.Map.

--
/Jacob Carlborg


Re: Ini parsing library in D ?

2011-02-28 Thread Trass3r

http://www.dprogramming.com/ini.php


Re: Version very simple?

2011-02-28 Thread Trass3r

Note that negation and logical and can basically be simulated:

!bla ->
version(bla) {} else ...

bla && blub ->
version(bla) version(blub) {...}


Re: Initializing a class pointer

2011-02-28 Thread Tyro[a.c.edwards]

On 2/27/2011 10:39 PM, Steven Schveighoffer wrote:

On Sat, 26 Feb 2011 19:46:18 -0500, Tyro[a.c.edwards] 
wrote:


On 2/27/2011 8:52 AM, Simen kjaeraas wrote:

Tyro[a.c.edwards]  wrote:


I'm trying to convert some c++ code that defines

T func(par...)
{
Controller * pCtrl = WinGetLong (hwnd);
.
.
.
switch(msg)
{
case FirstMatch:
pCtrl = new Controller (hwnd, reinterpret_cast
(lParam));
break;
}
}

I'm not sure why I need a pointer to the class, just trying to figure
it out.


Ah. You would not need a pointer to the class in D. Instead, your
function
would look something like this:

T funct(par...)
{
auto pCtrl = WinGetLong!Controller(hwnd);
...
switch(msg)
{
case FirstMatch:
pCtrl = new Controller(hWnd, cast(CREATESTRUCT*)lParam);
break;
}
}

C++ classes are in some ways more akin to D structs, in that:

class A {};

void foo(){
A bar;
}

bar would be allocated on the stack in C++, while in D bar would be a
pointer to a class instance on the heap. (well, it would be null, but
when you set it to something, that something would reside on the heap)



Ok, that's essentially what I have, except that I used Controller
pCtrl vice auto. WinGetLong however, is a template that calls
GetWindowLongPtrA() and casts it's result (in this case) to
Controller. GetWindowLongPtrA() returns LONG_PTR (aka int) and
therefore fails miserably on the cast attempt. On the reverse, there
is a WinSetLong that attempts to cast Controller to int for use with
SetWindowLongPtrA(). Neither of these functions complain when I use
Controller* but I end up with the problem of trying to initialize a
pointer with a reference to Controller.


You almost certainly do not want a pointer to a class reference. A class
typically resides on the heap, but the reference typically does not.
Therefore, by using a pointer to a class reference, you run very high
risk of escaping stack data, leading to memory corruption.

Looking at the documentation for GetWindowLongPtr, it appears to get
data associated with a window. Likely, this information is the a pointer
to the Controller class.

I would recommend doing this:

T WinGetLong(T)(HWND hwnd)
{
return cast(T)cast(void*)GetWindowLongPtrA(hwnd, ...);
}

and

void WinSetLong(T)(HWND hwnd, T t)
{
SetWindowLongPtrA(hwnd, ..., cast(LONG_PTR)cast(void*)t);
}

where the ... is the index copied from the C++ code (guessing it's
GWLP_USERDATA?).

btw, reinterpret_cast(x) is equivalent to (T)(void *)x;

-Steve


Thank you all (Steve, Bekenn, and Simen) for your assistance on this.


Re: How do you test pre-/post-conditions and invariants?

2011-02-28 Thread Magnus Lie Hetland

On 2011-02-27 02:33:46 +0100, Jonathan M Davis said:




[snip lots of useful stuff]

Thanks for your patience, and more useful clarifications. I think I get 
it now (but I may very well be wrong ;)


- M

--
Magnus Lie Hetland
http://hetland.org



string vs. w/char*

2011-02-28 Thread Tyro[a.c.edwards]
The bellow code attempts to use LoadStringA() to initialize _buf. 
However, regardless of what form _buf takes, the body of the if 
statement is always executed. I've attempted to use every type of string 
available in D to include char* _buf[MAX_RESSTRING+1] and setting 
_buf[MAX_RESSTRING] = '\0'; What am I doing incorrectly?

Any assistance is greatly appreciated.

class ResString
{
  enum { MAX_RESSTRING = 255 }

  alias getBuffer this;
  @property string getBuffer() { return _buf; }

  this(HINSTANCE hInst, int resId)
  {
_buf.length = MAX_RESSTRING;

SetLastError(0);

if(!LoadStringA(hInst, resId, cast(char*)toStringz(_buf), 
_buf.length + 1))

{
  throw new WinException("Load String failed");
}
  }

private:
  string _buf;
}


Re: Ini parsing library in D ?

2011-02-28 Thread Tarun Ramakrishna
Hi Trass,

Wow! A pure, plain simple D module for ini parsing. Nice :))

Thanks!
Tarun


On Mon, Feb 28, 2011 at 3:34 PM, Trass3r  wrote:
> http://www.dprogramming.com/ini.php
>


Re: Named Pipes IPC in D for windows and linux ?

2011-02-28 Thread Steven Schveighoffer
On Sun, 27 Feb 2011 13:53:05 -0500, Tarun Ramakrishna   
wrote:



Is there anything in the standard library to do named pipes IPC in
both windows and linux ? I am not necessarily looking for a unified
API, anything that will allow me to setup named pipes on either OS and
read/write on them will do.


Any C functions (including system calls) are available.  You are free to  
write your own API, and maybe it will be included in Phobos!


-Steve


Re: Ini parsing library in D ?

2011-02-28 Thread Tarun Ramakrishna
Err ok, I got excited too soon. That seems to be a D1 module..anyways
I am already halfway through writing my D2 Ini parser, so I guess its
fine

On Mon, Feb 28, 2011 at 6:07 PM, Tarun Ramakrishna  wrote:
> Hi Trass,
>
> Wow! A pure, plain simple D module for ini parsing. Nice :))
>
> Thanks!
> Tarun
>
>
> On Mon, Feb 28, 2011 at 3:34 PM, Trass3r  wrote:
>> http://www.dprogramming.com/ini.php
>>
>


Re: string vs. w/char*

2011-02-28 Thread Steven Schveighoffer
On Mon, 28 Feb 2011 07:34:39 -0500, Tyro[a.c.edwards]   
wrote:


The bellow code attempts to use LoadStringA() to initialize _buf.  
However, regardless of what form _buf takes, the body of the if  
statement is always executed. I've attempted to use every type of string  
available in D to include char* _buf[MAX_RESSTRING+1] and setting  
_buf[MAX_RESSTRING] = '\0'; What am I doing incorrectly?

Any assistance is greatly appreciated.

class ResString
{
   enum { MAX_RESSTRING = 255 }

   alias getBuffer this;
   @property string getBuffer() { return _buf; }

   this(HINSTANCE hInst, int resId)
   {
 _buf.length = MAX_RESSTRING;

 SetLastError(0);

 if(!LoadStringA(hInst, resId, cast(char*)toStringz(_buf),  
_buf.length + 1))

 {
   throw new WinException("Load String failed");
 }
   }

private:
   string _buf;
}


You should not be overwriting buf, it is immutable.  You need to make a  
new buffer each time.


this(HINSTANCE hInst, int resId)
{

  auto mybuf = new char[MAX_RESSTRING];
  auto nchars = LoadStringA(hInst, resId, mybuf.ptr, mybuf.length);
  if(!nchars)
  {
throw new WinException("Load String failed");
  }
  _buf = assumeUnique(mybuf[0..nchars]);

  SetLastError(0);
}

If this isn't working, you might consider that the string you are trying  
to load doesn't actually exist (that is a valid condition).  What is the  
error from GetLastError ?


-Steve


Re: Named Pipes IPC in D for windows and linux ?

2011-02-28 Thread Tarun Ramakrishna
Hi Steven,

Yes, I have now understood that.

Is there a guidelines/best practices page somewhere that describes how
should a good library be coded for acceptance into the standard
library:  patterns and conventions, etc ? I am very new to D, but from
the mails I have read on this list so far, I have learnt that all the
devs here prefer that a library API should preferably take power of D
ranges, slices and generics. But what about other things ?

Thanks,
Tarun

On Mon, Feb 28, 2011 at 6:19 PM, Steven Schveighoffer
 wrote:
> On Sun, 27 Feb 2011 13:53:05 -0500, Tarun Ramakrishna 
> wrote:
>
>> Is there anything in the standard library to do named pipes IPC in
>> both windows and linux ? I am not necessarily looking for a unified
>> API, anything that will allow me to setup named pipes on either OS and
>> read/write on them will do.
>
> Any C functions (including system calls) are available.  You are free to
> write your own API, and maybe it will be included in Phobos!
>
> -Steve
>


type set

2011-02-28 Thread spir

Hello,

I have a template condition that looks like this:

T check (T) () if (
is(T == DLogical) ||
is(T == DNumber) ||
is(T == DText) ||
is(T == DList) ||
is(T == DUnit)
) {
...
}

Is there a way to "factor out" such an expression using a kind of type set? If 
only for cleaning the code; but also because such a set may get long.


T check (T) () if (is (T in validTypeSet)) {
...
}

Denis
--
_
vita es estrany
spir.wikidot.com



Re: Named Pipes IPC in D for windows and linux ?

2011-02-28 Thread Steven Schveighoffer

On Mon, 28 Feb 2011 08:00:28 -0500, Tarun Ramakrishna 
wrote:


Hi Steven,

Yes, I have now understood that.

Is there a guidelines/best practices page somewhere that describes how
should a good library be coded for acceptance into the standard
library:  patterns and conventions, etc ? I am very new to D, but from
the mails I have read on this list so far, I have learnt that all the
devs here prefer that a library API should preferably take power of D
ranges, slices and generics. But what about other things ?


There is an issue with Windows system calls that is difficult to solve.  D  
uses the DMC runtime, not the MSVC runtime.  This means when you call C  
library functions (e.g. printf, fopen), it uses DMC's C library.  The  
library has very poor support for converting to/from OS handles from  
things like FILE *.


This wouldn't be such a problem, except Phobos' I/O API is based on FILE  
*.  This means any system calls you make (such as CreateNamedPipe) which  
return a HANDLE will be near impossible to wrap into a FILE *.  In writing  
std.process, I had to write my own converters between FILE * and HANDLE,  
and when that code is available, you should be able to use it (expect to  
have it done by the next release of dmd).


But for now, I would concentrate on getting it to work for your code.  One  
thing to keep in mind is that any library to be accepted into Phobos  
*should* be cross-platform if possible.  I would think named pipes should  
be able to be a cross-platform library.


-Steve


Re: string vs. w/char*

2011-02-28 Thread Tyro[a.c.edwards]

On 2/28/2011 9:58 PM, Steven Schveighoffer wrote:

On Mon, 28 Feb 2011 07:34:39 -0500, Tyro[a.c.edwards] 
wrote:


The bellow code attempts to use LoadStringA() to initialize _buf.
However, regardless of what form _buf takes, the body of the if
statement is always executed. I've attempted to use every type of
string available in D to include char* _buf[MAX_RESSTRING+1] and
setting _buf[MAX_RESSTRING] = '\0'; What am I doing incorrectly?
Any assistance is greatly appreciated.

class ResString
{
enum { MAX_RESSTRING = 255 }

alias getBuffer this;
@property string getBuffer() { return _buf; }

this(HINSTANCE hInst, int resId)
{
_buf.length = MAX_RESSTRING;

SetLastError(0);

if(!LoadStringA(hInst, resId, cast(char*)toStringz(_buf), _buf.length
+ 1))
{
throw new WinException("Load String failed");
}
}

private:
string _buf;
}


You should not be overwriting buf, it is immutable. You need to make a
new buffer each time.

this(HINSTANCE hInst, int resId)
{

auto mybuf = new char[MAX_RESSTRING];
auto nchars = LoadStringA(hInst, resId, mybuf.ptr, mybuf.length);
if(!nchars)
{
throw new WinException("Load String failed");
}
_buf = assumeUnique(mybuf[0..nchars]);

SetLastError(0);
}

If this isn't working, you might consider that the string you are trying
to load doesn't actually exist (that is a valid condition). What is the
error from GetLastError ?

-Steve


Both implementations results in error code 1812 being returned from 
GetLastError. explanation of the code reads:


 ERROR_RESOURCE_DATA_NOT_FOUND
 1812 (0x714)
 The specified image file did not contain a resource section.

The code I'm porting initially consisted of a resource.h file, a 
generic.rc file and two icons. I have not tried to include the icons and 
generic.rc file in the compilation because I do not know how to as yet 
and I've only used half of the resource.h file: didn't think I need the 
whole thing. Could this be the reason for the error? If so could you 
direct me to the explanation of how to prepare these files for inclusion 
in the compilation process?


Thanks,
Andrew


Re: type set

2011-02-28 Thread Steven Schveighoffer

On Mon, 28 Feb 2011 08:22:58 -0500, spir  wrote:


Hello,

I have a template condition that looks like this:

 T check (T) () if (
 is(T == DLogical) ||
 is(T == DNumber) ||
 is(T == DText) ||
 is(T == DList) ||
 is(T == DUnit)
 ) {
 ...
 }

Is there a way to "factor out" such an expression using a kind of type  
set? If only for cleaning the code; but also because such a set may get  
long.


 T check (T) () if (is (T in validTypeSet)) {
 ...
 }


This should probably work:

template isOneOf(X, T...)
{
static if(!T.length)
   enum bool isOneOf = false;
else static if(is(X == T[0]))
   enum bool isOneOf = true;
else
   enum bool isOneOf = isOneOf!(X, T[1..$]);
}

T check(T) () if(isOneOf!(T, DLogical, DNumber, DText, TList, DUnit))
{
   ...
}

Not sure if this exists in std.traits or not, but that's where I'd look.

-Steve


Re: string vs. w/char*

2011-02-28 Thread Steven Schveighoffer
On Mon, 28 Feb 2011 08:30:02 -0500, Tyro[a.c.edwards]   
wrote:



On 2/28/2011 9:58 PM, Steven Schveighoffer wrote:

On Mon, 28 Feb 2011 07:34:39 -0500, Tyro[a.c.edwards] 
wrote:


The bellow code attempts to use LoadStringA() to initialize _buf.
However, regardless of what form _buf takes, the body of the if
statement is always executed. I've attempted to use every type of
string available in D to include char* _buf[MAX_RESSTRING+1] and
setting _buf[MAX_RESSTRING] = '\0'; What am I doing incorrectly?
Any assistance is greatly appreciated.

class ResString
{
enum { MAX_RESSTRING = 255 }

alias getBuffer this;
@property string getBuffer() { return _buf; }

this(HINSTANCE hInst, int resId)
{
_buf.length = MAX_RESSTRING;

SetLastError(0);

if(!LoadStringA(hInst, resId, cast(char*)toStringz(_buf), _buf.length
+ 1))
{
throw new WinException("Load String failed");
}
}

private:
string _buf;
}


You should not be overwriting buf, it is immutable. You need to make a
new buffer each time.

this(HINSTANCE hInst, int resId)
{

auto mybuf = new char[MAX_RESSTRING];
auto nchars = LoadStringA(hInst, resId, mybuf.ptr, mybuf.length);
if(!nchars)
{
throw new WinException("Load String failed");
}
_buf = assumeUnique(mybuf[0..nchars]);

SetLastError(0);
}

If this isn't working, you might consider that the string you are trying
to load doesn't actually exist (that is a valid condition). What is the
error from GetLastError ?

-Steve


Both implementations results in error code 1812 being returned from  
GetLastError. explanation of the code reads:


  ERROR_RESOURCE_DATA_NOT_FOUND
  1812 (0x714)
  The specified image file did not contain a resource section.

The code I'm porting initially consisted of a resource.h file, a  
generic.rc file and two icons. I have not tried to include the icons and  
generic.rc file in the compilation because I do not know how to as yet  
and I've only used half of the resource.h file: didn't think I need the  
whole thing. Could this be the reason for the error? If so could you  
direct me to the explanation of how to prepare these files for inclusion  
in the compilation process?




No clue, sorry.  I build D mostly on linux, on windows only when I have  
to.  Look on digitalmars.com for Windows programming.  Or try google.


-Steve


Re: string vs. w/char*

2011-02-28 Thread J Chapman
== Quote from Tyro[a.c.edwards] (nos...@home.com)'s article
> Both implementations results in error code 1812 being returned from
> GetLastError. explanation of the code reads:
>   ERROR_RESOURCE_DATA_NOT_FOUND
>   1812 (0x714)
>   The specified image file did not contain a resource section.
> The code I'm porting initially consisted of a resource.h file, a
> generic.rc file and two icons. I have not tried to include the icons and
> generic.rc file in the compilation because I do not know how to as yet
> and I've only used half of the resource.h file: didn't think I need the
> whole thing. Could this be the reason for the error? If so could you
> direct me to the explanation of how to prepare these files for inclusion
> in the compilation process?
> Thanks,
> Andrew

You need to compile the .rc file (see
http://www.digitalmars.com/ctg/rcc.html), then add the resulting .res file
to dmd's command line.


Re: type set

2011-02-28 Thread spir

On 02/28/2011 02:32 PM, Steven Schveighoffer wrote:

On Mon, 28 Feb 2011 08:22:58 -0500, spir  wrote:


Hello,

I have a template condition that looks like this:

T check (T) () if (
is(T == DLogical) ||
is(T == DNumber) ||
is(T == DText) ||
is(T == DList) ||
is(T == DUnit)
) {
...
}

Is there a way to "factor out" such an expression using a kind of type set?
If only for cleaning the code; but also because such a set may get long.

T check (T) () if (is (T in validTypeSet)) {
...
}


This should probably work:

template isOneOf(X, T...)
{
static if(!T.length)
enum bool isOneOf = false;
else static if(is(X == T[0]))
enum bool isOneOf = true;
else
enum bool isOneOf = isOneOf!(X, T[1..$]);
}

T check(T) () if(isOneOf!(T, DLogical, DNumber, DText, TList, DUnit))
{
...
}

Not sure if this exists in std.traits or not, but that's where I'd look.

-Steve


Waow, great anyway! Didn't even know one can write variadic type/template param 
lists.


By the way, the block of the function is a series of static if-s, one for each 
allowed type. Is there any static switch? Or any other nicer way to write it than:


T check (T) () if (
is(T == DLogical) ||
is(T == DNumber) ||
is(T == DText) ||
is(T == DList) ||
is(T == DUnit)
) {
TypeCode type;

static if (is(T == DLogical))
if (this.code == LOGICAL)
return this.logical;
else
type == LOGICAL;
static if (is(T == DNumber))
if (this.code == NUMBER)
return this.number;
else
type == NUMBER;
static if (is(T == DText))
if (this.code == TEXT)
return this.text;
else
type == TEXT;
static if (is(T == DList))
if (this.code == LOGICAL)
return this.list;
else
type == LOGICAL;
static if (is(T == DUnit))
if (this.code == UNIT)
return this.unit;
else
type == UNIT;

// type error
throw new TypeError(type, this);
}

This func type-checks and returns the current value of a tagged union. I would 
be very pleased with a mapping from types to type codes (tags). I can't do 
without the type param, I guess, because it's the return value's type... or can 
I? But the discriminating code of the union cannot be the type itself (*), 
instead it's a plain code.
I thought at using TypeInfo-s as codes, which can then be mapped from types 
using typeid(). But according to sizeof, this makes the code weigh 1 word 
instead of one byte.


Denis

(*) Indeed. Else it would be a template generating N distinct types, which is 
precisely the opposite of what a union provides.

--
_
vita es estrany
spir.wikidot.com



Re: string vs. w/char*

2011-02-28 Thread Andrej Mitrovic
I've successfully used resource files with DFL. Maybe this will help:
http://www.dsource.org/forums/viewtopic.php?t=5591&sid=bf2d804f1d5a3f9efccbf29ebb6cf723

You'll have to dig into the DFL library sources to find out exactly
how it loads a resource file though.


Re: type set

2011-02-28 Thread Steven Schveighoffer

On Mon, 28 Feb 2011 09:27:36 -0500, spir  wrote:

By the way, the block of the function is a series of static if-s, one  
for each allowed type. Is there any static switch? Or any other nicer  
way to write it than:


 T check (T) () if (
 is(T == DLogical) ||
 is(T == DNumber) ||
 is(T == DText) ||
 is(T == DList) ||
 is(T == DUnit)
 ) {
 TypeCode type;

 static if (is(T == DLogical))
 if (this.code == LOGICAL)
 return this.logical;
 else
 type == LOGICAL;
 static if (is(T == DNumber))
 if (this.code == NUMBER)
 return this.number;
 else
 type == NUMBER;
 static if (is(T == DText))
 if (this.code == TEXT)
 return this.text;
 else
 type == TEXT;
 static if (is(T == DList))
 if (this.code == LOGICAL)
 return this.list;
 else
 type == LOGICAL;
 static if (is(T == DUnit))
 if (this.code == UNIT)
 return this.unit;
 else
 type == UNIT;

 // type error
 throw new TypeError(type, this);
 }


There is a final switch, but I don't know if that works on types.  You may  
be stuck with static if.


When doing things like this, I'd recommend using a mapping template.  For  
example:


private template typeCode(T)
{
   static if(is(T == DLogical)) enum typeCode = LOGICAL;
   else static if(is(T == DNumber)) enum typeCode = NUMBER;
   ...
   else static assert(0);
}

then you almost can use this to generate the right code:

if(this.code == typeCode!T)
{
   static if(is(T == DUnit)) return this.unit;
   else static if(...
}

You can probably replace the inner static if with a mixin, if you name  
your union members properly (i.e. if you can generate the name of the  
union member based on its type name or code, like DUnit dunit_val).


But at least it gives you an idea of how this can be done efficiently.

Plus avoiding large repetitive static ifs can save you from tedious  
copy-pasting bugs like the one in your DList branch ;)


-Steve


Re: Ini parsing library in D ?

2011-02-28 Thread Jesse Phillips
Tarun Ramakrishna Wrote:

> Err ok, I got excited too soon. That seems to be a D1 module..anyways
> I am already halfway through writing my D2 Ini parser, so I guess its
> fine
> 
> On Mon, Feb 28, 2011 at 6:07 PM, Tarun Ramakrishna  wrote:
> > Hi Trass,
> >
> > Wow! A pure, plain simple D module for ini parsing. Nice :))
> >
> > Thanks!
> > Tarun
> >
> >
> > On Mon, Feb 28, 2011 at 3:34 PM, Trass3r  wrote:
> >> http://www.dprogramming.com/ini.php
> >>
> >

It doesn't take much to make it work with D2. I have a working one, and has 
mention of what I changed so I should be able to redistribute it. I'll double 
check though.


range violation

2011-02-28 Thread Dr.Smith
With multidimensional arrays greater than 150x150, I get a range violation at
run time: "core.exception.RangeError@pweight(54): Range violation"
Is this a bug? Is there a work-around?



Re: string vs. w/char*

2011-02-28 Thread Tyro[a.c.edwards]

On 2/28/2011 11:08 PM, J Chapman wrote:

== Quote from Tyro[a.c.edwards] (nos...@home.com)'s article

Both implementations results in error code 1812 being returned from
GetLastError. explanation of the code reads:
   ERROR_RESOURCE_DATA_NOT_FOUND
   1812 (0x714)
   The specified image file did not contain a resource section.
The code I'm porting initially consisted of a resource.h file, a
generic.rc file and two icons. I have not tried to include the icons and
generic.rc file in the compilation because I do not know how to as yet
and I've only used half of the resource.h file: didn't think I need the
whole thing. Could this be the reason for the error? If so could you
direct me to the explanation of how to prepare these files for inclusion
in the compilation process?
Thanks,
Andrew


You need to compile the .rc file (see
http://www.digitalmars.com/ctg/rcc.html), then add the resulting .res file
to dmd's command line.


Awesome, this does the trick. However I get get a "GP Fault"? during 
execution. Using windbg, I tracked it down to this piece of code:


void Create()
{
  _hwnd = CreateWindowExA(
_exStyle,
cast(const(char*))_wc.GetName(), // returns string
cast(const(char*))_windowName,   // string variable
_style,
_x,
_y,
_width,
_height,
_hWndParent,
_hMenu,
_wc.GetInstance(),
_data);

assert(_hwnd, "Internal error: Window Creation Failed.");
}

The program craps at assert() but the error is generated. It just 
displays a dialog box with the message: "test.exe has stopped working, 
Windows is checking for a solution to the problem..."


I'm thinking that _hwnd was never initialized and that assert is access 
a null pointer but I cannot be sure. Any suggestions or ideas?


Re: array idioms

2011-02-28 Thread Nick Treleaven
On Fri, 25 Feb 2011 23:48:19 +0100, spir wrote:

> we cannot even use something like memcopy because the source & target
> mem areas overlap

In those cases, use memmove.

But maybe there's a D-ish way that's just as efficient, not sure.


Re: range violation

2011-02-28 Thread Nick Treleaven
On Mon, 28 Feb 2011 16:07:41 +, Dr.Smith wrote:

> With multidimensional arrays greater than 150x150, I get a range
> violation at run time: "core.exception.RangeError@pweight(54): Range
> violation" Is this a bug? Is there a work-around?

Can you show some code or a test case?

My sample below seems to work fine (but I'm still using an older dmd - 
v2.049):

int[151][151] x;

x[150][150] = 7;
writeln(x[150][150]);


Re: type set

2011-02-28 Thread spir

On 02/28/2011 03:50 PM, Steven Schveighoffer wrote:

On Mon, 28 Feb 2011 09:27:36 -0500, spir  wrote:


By the way, the block of the function is a series of static if-s, one for
each allowed type. Is there any static switch? Or any other nicer way to
write it than:

T check (T) () if (
is(T == DLogical) ||
is(T == DNumber) ||
is(T == DText) ||
is(T == DList) ||
is(T == DUnit)
) {
TypeCode type;

static if (is(T == DLogical))
if (this.code == LOGICAL)
return this.logical;
else
type == LOGICAL;
static if (is(T == DNumber))
if (this.code == NUMBER)
return this.number;
else
type == NUMBER;
static if (is(T == DText))
if (this.code == TEXT)
return this.text;
else
type == TEXT;
static if (is(T == DList))
if (this.code == LOGICAL)
return this.list;
else
type == LOGICAL;
static if (is(T == DUnit))
if (this.code == UNIT)
return this.unit;
else
type == UNIT;

// type error
throw new TypeError(type, this);
}


There is a final switch, but I don't know if that works on types. You may be
stuck with static if.

When doing things like this, I'd recommend using a mapping template. For 
example:

private template typeCode(T)
{
static if(is(T == DLogical)) enum typeCode = LOGICAL;
else static if(is(T == DNumber)) enum typeCode = NUMBER;
...
else static assert(0);
}

then you almost can use this to generate the right code:

if(this.code == typeCode!T)
{
static if(is(T == DUnit)) return this.unit;
else static if(...
}


That's it! This actually builds an the equivalent of an AA which keys are 
types. I was looking for such a functionality for a while already.
I am in fact *contantly* annoyed in D by the fact there are no types (I mean at 
runtime). On the other hand, this forces me looking for workaround, to express 
my models in "distorted" ways, which lets me discover unusual features and 
idioms in D's semantic dark corners ;-)



You can probably replace the inner static if with a mixin, if you name your
union members properly (i.e. if you can generate the name of the union member
based on its type name or code, like DUnit dunit_val).


Yeah, I could, as shown by the code above: lang type Xyz <--> code XYZ <--> D 
impl type DXyz <--> union member xyz.

Could not be more regular, I guess ;-)
But as you may know, I 100% against string mixins. I prefere keeping the 
mapping explicite without string sorcery.
(Precisely, in the toy lang I'm starting to realise, one could do that 
trivially by manipulating the AST, without any dark magic. But this is another 
story...)



But at least it gives you an idea of how this can be done efficiently.

Plus avoiding large repetitive static ifs can save you from tedious
copy-pasting bugs like the one in your DList branch ;)


Good catch, Steve! And thank you again.

PS: Is it your email client that eats whitespace (see my code above)?

Denis
--
_
vita es estrany
spir.wikidot.com



Some weird crashes

2011-02-28 Thread simendsjo
I'm trying to wrap the newest mysql c connector, but I get some weird 
bugs. I don't know any assembly, so I don't even know if I've included 
enough info.. I hope this is a small enough test case so someone can 
understand the issue.

I've used implib on the included dll and rdmd and dmd 2.051 to compile.

// CORRECT
auto res = mysql_library_init(0, null, null);

auto cn = mysql_init(null);
auto oldcn = cn;

writeln(mysql_errno(cn));
assert(cn == oldcn);

auto err = mysql_errno(cn);
//assert(cn == oldcn); // notice this is commented out

mysql_close(cn);
mysql_library_end();


0040201A  |. E8 F5B30300CALL 
0040201F  |. 6A 00  PUSH 0
00402021  |. E8 E8B30300CALL 
00402026  |. 8945 F8MOV DWORD PTR SS:[EBP-8],EAX
00402029  |. 8945 FCMOV DWORD PTR SS:[EBP-4],EAX
0040202C  |. FF75 F8PUSH DWORD PTR SS:[EBP-8]
0040202F  |. E8 D4B30300CALL 
00402034  |. 83C4 04ADD ESP,4
00402037  |. E8 4C00CALL mytest_w.00402088
0040203C  |. 8B45 F8MOV EAX,DWORD PTR SS:[EBP-8]
0040203F  |. 3B45 FCCMP EAX,DWORD PTR SS:[EBP-4]
00402042  |. B9 0100MOV ECX,1
00402047  |. 74 02  JE SHORT mytest_w.0040204B
00402049  |. 8ACD   MOV CL,CH
0040204B  |> 894D F4MOV DWORD PTR SS:[EBP-C],ECX
0040204E  |. 74 0A  JE SHORT mytest_w.0040205A
00402050  |. B8 1A00MOV EAX,1A
00402055  |. E8 662ACALL mytest_w.00404AC0
0040205A  |> FF75 F8PUSH DWORD PTR SS:[EBP-8]
0040205D  |. E8 A6B30300CALL 
00402062  |. 807D F4 00 CMP BYTE PTR SS:[EBP-C],0
00402066  |. 75 0A  JNZ SHORT mytest_w.00402072
00402068  |. B8 1D00MOV EAX,1D
0040206D  |. E8 4E2ACALL mytest_w.00404AC0
00402072  |> FF75 F8PUSH DWORD PTR SS:[EBP-8]
00402075  |. E8 88B30300CALL 
0040207A  |. E8 7DB30300CALL 
0040207F  |. 31C0   XOR EAX,EAX
00402081  |. 83C4 18ADD ESP,18
00402084  |. C9 LEAVE
00402085  \. C3 RETN



// ERROR
auto res = mysql_library_init(0, null, null);

auto cn = mysql_init(null);
auto oldcn = cn;

writeln(mysql_errno(cn));
assert(cn == oldcn); // when the last assert is active, the above line 
changes cn and thus fails.


auto err = mysql_errno(cn);
assert(cn == oldcn);

mysql_close(cn);
mysql_library_end();
0040201A  |. E8 D5B30300CALL 
0040201F  |. 6A 00  PUSH 0
00402021  |. E8 C8B30300CALL 
00402026  |. 8945 F8MOV DWORD PTR SS:[EBP-8],EAX
00402029  |. 8945 FCMOV DWORD PTR SS:[EBP-4],EAX
0040202C  |. FF75 F8PUSH DWORD PTR SS:[EBP-8]
0040202F  |. E8 B4B30300CALL 
00402034  |. 83C4 04ADD ESP,4
00402037  |. E8 3000CALL mytest_f.0040206C
0040203C  |. 8B45 F8MOV EAX,DWORD PTR SS:[EBP-8]
0040203F  |. 3B45 FCCMP EAX,DWORD PTR SS:[EBP-4]
00402042  |. 74 0A  JE SHORT mytest_f.0040204E
00402044  |. B8 1A00MOV EAX,1A
00402049  |. E8 562ACALL mytest_f.00404AA4
0040204E  |> FF75 F8PUSH DWORD PTR SS:[EBP-8]
00402051  |. E8 92B30300CALL 
00402056  |. FF75 F8PUSH DWORD PTR SS:[EBP-8]
00402059  |. E8 84B30300CALL 
0040205E  |. E8 79B30300CALL 
00402063  |. 31C0   XOR EAX,EAX
00402065  |. 83C4 18ADD ESP,18
00402068  |. C9 LEAVE
00402069  \. C3 RETN



Re: string vs. w/char*

2011-02-28 Thread Denis Koroskin
On Mon, 28 Feb 2011 19:35:47 +0300, Tyro[a.c.edwards]   
wrote:



On 2/28/2011 11:08 PM, J Chapman wrote:

== Quote from Tyro[a.c.edwards] (nos...@home.com)'s article

Both implementations results in error code 1812 being returned from
GetLastError. explanation of the code reads:
   ERROR_RESOURCE_DATA_NOT_FOUND
   1812 (0x714)
   The specified image file did not contain a resource section.
The code I'm porting initially consisted of a resource.h file, a
generic.rc file and two icons. I have not tried to include the icons  
and

generic.rc file in the compilation because I do not know how to as yet
and I've only used half of the resource.h file: didn't think I need the
whole thing. Could this be the reason for the error? If so could you
direct me to the explanation of how to prepare these files for  
inclusion

in the compilation process?
Thanks,
Andrew


You need to compile the .rc file (see
http://www.digitalmars.com/ctg/rcc.html), then add the resulting .res  
file

to dmd's command line.


Awesome, this does the trick. However I get get a "GP Fault"? during  
execution. Using windbg, I tracked it down to this piece of code:


void Create()
{
   _hwnd = CreateWindowExA(
 _exStyle,
 cast(const(char*))_wc.GetName(), // returns string
 cast(const(char*))_windowName,   // string variable
 _style,
 _x,
 _y,
 _width,
 _height,
 _hWndParent,
 _hMenu,
 _wc.GetInstance(),
 _data);

 assert(_hwnd, "Internal error: Window Creation Failed.");
}

The program craps at assert() but the error is generated. It just  
displays a dialog box with the message: "test.exe has stopped working,  
Windows is checking for a solution to the problem..."


I'm thinking that _hwnd was never initialized and that assert is access  
a null pointer but I cannot be sure. Any suggestions or ideas?


The


 cast(const(char*))_wc.GetName()


line look *very* suspicious. You can't get a string and just cast it to  
const(char)*. Most importantly, the string (most likely) is not  
null-terminated.


What you need to do here is the following:

auto className = toStringz(_ws.GetName());
auto caption = toStringz(_windowName);

and pass those 2 to the function.

Alternatively, you could make sure your strings are null-terminated and  
pass the pointer directly (e.g. _windowName.ptr):


string _windowName = "foo"; // null-terminated automatically
string _caption = ("Hello, World" ~ "\0")[0..$-1]; // append trailing zero  
to an existing string but exclude it from result (so that it's not  
included in _caption.length)


Re: type set

2011-02-28 Thread Steven Schveighoffer

On Mon, 28 Feb 2011 12:50:21 -0500, spir  wrote:


PS: Is it your email client that eats whitespace (see my code above)?


Only in your reply does the code appear indented improperly, so I think  
it's your client, not mine.


-steve


Re: type set

2011-02-28 Thread bearophile
Steven Schveighoffer:

> Not sure if this exists in std.traits or not, but that's where I'd look.

std.typetuple.anySatisfy helps here.

---

spir:

> Is there any static switch?

foreach done on a typetuple is a static foreach.


> Or any other nicer way to write it than:

I suggest to put the types in a typetuple and use a foreach(i, T, 
TypeTuple!(t1, t2, ...)), put your values in another typetuple and use the i 
index to access and return the values.

import std.stdio, std.typetuple;
int mapper(TX)() {
alias TypeTuple!(int, float, string) Tkeys;
enum values = [100, 5, 2];
//alias TypeTuple!(100, 5, 2) Tvalues; // alternative

foreach (i, T; Tkeys)
if (is(T == TX))
return values[i];
assert(0, "Can't happen");
}
enum r = mapper!float();
static assert(r == 5);
void main() {}

Bye,
bearophile


How-to manipulate a C array with D2 vector operations?

2011-02-28 Thread Lars Holowko
Hi

I am trying to implement a D2 function that has this C signature (it
gets called from a C module and I cannot change the caller):

extern(C) read_into(char *buffer, size_t buf_len);

I would like to use D's vector (or array-wise according to TDPL)
operations on buffer but I cannot find a way, how I could initialize a
(static?) D array and tell it to use buffer as its memory. Obviously I
don't want to copy buffer in a regular D array to be able to do the
manipulations and than having to copy everything back into buffer.

Is there any way to get this accomplished?


Thanks,

Lars


Re: How-to manipulate a C array with D2 vector operations?

2011-02-28 Thread Denis Koroskin
On Mon, 28 Feb 2011 19:51:28 +0300, Lars Holowko   
wrote:



Hi

I am trying to implement a D2 function that has this C signature (it
gets called from a C module and I cannot change the caller):

extern(C) read_into(char *buffer, size_t buf_len);

I would like to use D's vector (or array-wise according to TDPL)
operations on buffer but I cannot find a way, how I could initialize a
(static?) D array and tell it to use buffer as its memory. Obviously I
don't want to copy buffer in a regular D array to be able to do the
manipulations and than having to copy everything back into buffer.

Is there any way to get this accomplished?


Thanks,

Lars


Here you go:

auto arr = buffer[0..buf_len];

Now you can operate on this array however you like. E.g.

arr[] = 0; // initialize with zeros


Re: How-to manipulate a C array with D2 vector operations?

2011-02-28 Thread Trass3r

I am trying to implement a D2 function that has this C signature (it
gets called from a C module and I cannot change the caller):

extern(C) read_into(char *buffer, size_t buf_len);

I would like to use D's vector (or array-wise according to TDPL)
operations on buffer but I cannot find a way, how I could initialize a


auto darray = buffer[0 .. buf_len]


Re: Ini parsing library in D ?

2011-02-28 Thread Jesse Phillips
Jesse Phillips Wrote:

> It doesn't take much to make it work with D2. I have a working one, and has 
> mention of what I changed so I should be able to redistribute it. I'll double 
> check though.

Here it is:

https://github.com/he-the-great/ini.d/tree/D2

The master branch should also work with D1 and D2 compilers before 2.052. Maybe 
I should just update the catch block to capture Throwable there. The D2 branch 
has annotations too.


Re: Some weird crashes

2011-02-28 Thread simendsjo

On 28.02.2011 18:52, simendsjo wrote:


// ERROR
auto res = mysql_library_init(0, null, null);

auto cn = mysql_init(null);
auto oldcn = cn;

writeln(mysql_errno(cn));
assert(cn == oldcn); // when the last assert is active, the above line
changes cn and thus fails.

auto err = mysql_errno(cn);
assert(cn == oldcn);


Btw, if I don't use writeln it doesn't fail..


Re: Some weird crashes

2011-02-28 Thread Denis Koroskin
On Mon, 28 Feb 2011 22:04:44 +0300, simendsjo   
wrote:



On 28.02.2011 18:52, simendsjo wrote:


// ERROR
auto res = mysql_library_init(0, null, null);

auto cn = mysql_init(null);
auto oldcn = cn;

writeln(mysql_errno(cn));
assert(cn == oldcn); // when the last assert is active, the above line
changes cn and thus fails.

auto err = mysql_errno(cn);
assert(cn == oldcn);


Btw, if I don't use writeln it doesn't fail..


I think you have a bug at line 42.

On a serious note, it might have helped if you'd attached source code, or  
at least binaries.


Is std.regex.match completely broken?

2011-02-28 Thread Jacob Carlborg

The following code will result in an AssertError or RangeError when run.

import std.regex;
import std.stdio;

void main ()
{
auto m = "abc".match(`a(\w)b`);

writeln(m.hit); // AssertError in regex.d:1795
writeln(m.captures); // RangeError in regex.d:1719
}

Should I report this as a bug?

--
/Jacob Carlborg


Re: Some weird crashes

2011-02-28 Thread simendsjo

On 28.02.2011 20:24, Denis Koroskin wrote:

I think you have a bug at line 42.

On a serious note, it might have helped if you'd attached source code,
or at least binaries.


The file was too large to be attached. Here's a link to a public hosting 
service: http://share1t.com/4xgt2l. Everything (including mysql 
libraries) is included.


Not sure what line you are referring to though.


Re: Ini parsing library in D ?

2011-02-28 Thread Tarun Ramakrishna
Hi Jesse,

This list is filled with terrific people and you are one of them :)

Thanks!
Tarun

On Tue, Mar 1, 2011 at 12:01 AM, Jesse Phillips
 wrote:
> Jesse Phillips Wrote:
>
>> It doesn't take much to make it work with D2. I have a working one, and has 
>> mention of what I changed so I should be able to redistribute it. I'll 
>> double check though.
>
> Here it is:
>
> https://github.com/he-the-great/ini.d/tree/D2
>
> The master branch should also work with D1 and D2 compilers before 2.052. 
> Maybe I should just update the catch block to capture Throwable there. The D2 
> branch has annotations too.
>


Re: Some weird crashes

2011-02-28 Thread bearophile
simendsjo:

> Not sure what line you are referring to though.

http://en.wikipedia.org/wiki/Answer_to_Life,_the_Universe,_and_Everything#The_number_42

Bye,
bearophile


RPM Package management

2011-02-28 Thread Kai Meyer
I would like to get involved with the rpm build process. Who can I 
contact to get more information on what help I can provide?


Re: Defining type coercion

2011-02-28 Thread Simen kjaeraas

Jonathan M Davis  wrote:


struct Ordinal {
 private int representation;
 char getChar( ) {
 return representation + 'a'-1;
 }
 alias representation this;
 alias getChar this;
}

But like I said, it currently does not work.


Would "alias getChar this" really be legal? I thought that this had to be
aliased to a type.


Alias function this; works.


--
Simen


Re: range violation

2011-02-28 Thread Dr.Smith
For example:

double [string] data;
double [200][1000] data2;

for(int i = 0; i < 200; i++) {
for(int j = 0; j < 1000; j++) {

  // fake multi-dim works
  string str = to!string(i) ~ "," ~ to!string(j);
  data[str] = someNumber;

  // real multi-dim does not work
  data2[i][j] = someNumber;
}
}


Re: range violation

2011-02-28 Thread bearophile
Dr.Smith:

> For example:
> 
> double [string] data;
> double [200][1000] data2;
> 
> for(int i = 0; i < 200; i++) {
> for(int j = 0; j < 1000; j++) {
> 
>   // fake multi-dim works
>   string str = to!string(i) ~ "," ~ to!string(j);
>   data[str] = someNumber;
> 
>   // real multi-dim does not work
>   data2[i][j] = someNumber;
> }
> }

You receive the same stack overflow error with this simpler code:

void main() {
double[200][1000] a;
}

Keep in mind this is a fixed-sized array, so it's allocated on the stack.

On Windows with DMD if you add a switch like this, to increase max stack size, 
that code works:
-L/STACK:1000

Bye,
bearophile


Re: range violation

2011-02-28 Thread Andrej Mitrovic
That's because data2 has length 1000, each of which has an array of
length 200. Not the other way around. This works:

double[200][1000] data2;

void main()
{
for(int i = 0; i < 1000; i++) {
for(int j = 0; j < 200; j++) {
 data2[i][j] = 4.0;
   }
}
}


Re: Ini parsing library in D ?

2011-02-28 Thread Jérôme M. Berger
Tarun Ramakrishna wrote:
> Hi,
> 
> Do we have a ini parser in D somewhere ? If not, is there some
> documentation anywhere that tells one how to wrap a simple C++ library
> like simple ini ? (Of course it isn't difficult to build a quick
> parser, just that someone would done this already)
> 
Here (D1+phobos):
https://bitbucket.org/jmb/spacecuted/src/tip/src/scd/settings.d

Jerome
-- 
mailto:jeber...@free.fr
http://jeberger.free.fr
Jabber: jeber...@jabber.fr



signature.asc
Description: OpenPGP digital signature


Re: How-to manipulate a C array with D2 vector operations?

2011-02-28 Thread Lars Holowko

On 2/28/2011 10:15 AM, Denis Koroskin wrote:

On Mon, 28 Feb 2011 19:51:28 +0300, Lars Holowko
 wrote:


gets called from a C module and I cannot change the caller):

extern(C) read_into(char *buffer, size_t buf_len);

I would like to use D's vector (or array-wise according to TDPL)
operations on buffer but I cannot find a way, how I could initialize a
(static?) D array and tell it to use buffer as its memory. Obviously I


Here you go:

auto arr = buffer[0..buf_len];

Now you can operate on this array however you like. E.g.

arr[] = 0; // initialize with zeros


Thanks Denis and Trass3r,

that was embarrasingly easy ;-)


Re: string vs. w/char*

2011-02-28 Thread Tyro[a.c.edwards]
== Quote from Denis Koroskin (2kor...@gmail.com)'s article
> On Mon, 28 Feb 2011 19:35:47 +0300, Tyro[a.c.edwards]

> wrote:
> > On 2/28/2011 11:08 PM, J Chapman wrote:
> >> == Quote from Tyro[a.c.edwards] (nos...@home.com)'s article
> >>> Both implementations results in error code 1812 being
returned from
> >>> GetLastError. explanation of the code reads:
> >>>ERROR_RESOURCE_DATA_NOT_FOUND
> >>>1812 (0x714)
> >>>The specified image file did not contain a resource
section.
> >>> The code I'm porting initially consisted of a resource.h
file, a
> >>> generic.rc file and two icons. I have not tried to include
the icons
> >>> and
> >>> generic.rc file in the compilation because I do not know how
to as yet
> >>> and I've only used half of the resource.h file: didn't think
I need the
> >>> whole thing. Could this be the reason for the error? If so
could you
> >>> direct me to the explanation of how to prepare these files
for
> >>> inclusion
> >>> in the compilation process?
> >>> Thanks,
> >>> Andrew
> >>
> >> You need to compile the .rc file (see
> >> http://www.digitalmars.com/ctg/rcc.html), then add the
resulting .res
> >> file
> >> to dmd's command line.
> >
> > Awesome, this does the trick. However I get get a "GP Fault"?
during
> > execution. Using windbg, I tracked it down to this piece of
code:
> >
> > void Create()
> > {
> >_hwnd = CreateWindowExA(
> >  _exStyle,
> >  cast(const(char*))_wc.GetName(), // returns string
> >  cast(const(char*))_windowName,   // string variable
> >  _style,
> >  _x,
> >  _y,
> >  _width,
> >  _height,
> >  _hWndParent,
> >  _hMenu,
> >  _wc.GetInstance(),
> >  _data);
> >
> >  assert(_hwnd, "Internal error: Window Creation Failed.");
> > }
> >
> > The program craps at assert() but the error is generated. It
just
> > displays a dialog box with the message: "test.exe has stopped
working,
> > Windows is checking for a solution to the problem..."
> >
> > I'm thinking that _hwnd was never initialized and that assert
is access
> > a null pointer but I cannot be sure. Any suggestions or ideas?
> The
> >  cast(const(char*))_wc.GetName()
> line look *very* suspicious. You can't get a string and just
cast it to
> const(char)*. Most importantly, the string (most likely) is not
> null-terminated.
> What you need to do here is the following:
> auto className = toStringz(_ws.GetName());
> auto caption = toStringz(_windowName);
> and pass those 2 to the function.

Actually I've already tried that, it has no effect on the outcome.
>From your suggestion though, I've gone back and replace all the
cast(const(char*)) usage throughout the program. Final verdict:
the program still crashes it the same location. It actually never
returns from CreateWindowExA().

> Alternatively, you could make sure your strings are null-
terminated and
> pass the pointer directly (e.g. _windowName.ptr):
> string _windowName = "foo"; // null-terminated automatically
> string _caption = ("Hello, World" ~ "\0")[0..$-1]; // append
trailing zero
> to an existing string but exclude it from result (so that it's
not
> included in _caption.length)



Template argument deduction

2011-02-28 Thread Tom

I have...

import std.stdio;

int main(string[] args) {
foo([[1,2],[3,4],[5,6]]); // ERROR [1]
bar([[1,2],[3,4],[5,6]]); // OK
foo!int([[1,2],[3,4],[5,6]]); // OK

return 0;
}

void foo(T)(T[2][] t) {
writeln(typeid(t));
}

void bar(T)(T[][] t) {
writeln(typeid(t));
}

[1]
src\main.d(4): Error: template main.foo(T) does not match any function 
template declaration
src\main.d(4): Error: template main.foo(T) cannot deduce template 
function from argument types !()(int[][])



Why can't compiler deduce template parameters from arguments in the 
first instantiation?


Thanks in advance,
Tom;


Re: Template argument deduction

2011-02-28 Thread Ali Çehreli

On 02/28/2011 07:39 PM, Tom wrote:

I have...

import std.stdio;

int main(string[] args) {
foo([[1,2],[3,4],[5,6]]); // ERROR [1]
bar([[1,2],[3,4],[5,6]]); // OK
foo!int([[1,2],[3,4],[5,6]]); // OK

return 0;
}

void foo(T)(T[2][] t) {
writeln(typeid(t));
}

void bar(T)(T[][] t) {
writeln(typeid(t));
}

[1]
src\main.d(4): Error: template main.foo(T) does not match any function
template declaration
src\main.d(4): Error: template main.foo(T) cannot deduce template
function from argument types !()(int[][])


Why can't compiler deduce template parameters from arguments in the
first instantiation?

Thanks in advance,
Tom;


That's because the type of literals like [1, 2] are slices (dynamic 
arrays), not fixed-sized arrays.


import std.stdio;

void main()
{
writeln(typeof([1,2]).stringof);
}

The output of that program is

int[]

Ali



Re: string vs. w/char*

2011-02-28 Thread Denis Koroskin
On Tue, 01 Mar 2011 02:08:48 +0300, Tyro[a.c.edwards]   
wrote:



== Quote from Denis Koroskin (2kor...@gmail.com)'s article

On Mon, 28 Feb 2011 19:35:47 +0300, Tyro[a.c.edwards]



wrote:
> On 2/28/2011 11:08 PM, J Chapman wrote:
>> == Quote from Tyro[a.c.edwards] (nos...@home.com)'s article
>>> Both implementations results in error code 1812 being

returned from

>>> GetLastError. explanation of the code reads:
>>>ERROR_RESOURCE_DATA_NOT_FOUND
>>>1812 (0x714)
>>>The specified image file did not contain a resource

section.

>>> The code I'm porting initially consisted of a resource.h

file, a

>>> generic.rc file and two icons. I have not tried to include

the icons

>>> and
>>> generic.rc file in the compilation because I do not know how

to as yet

>>> and I've only used half of the resource.h file: didn't think

I need the

>>> whole thing. Could this be the reason for the error? If so

could you

>>> direct me to the explanation of how to prepare these files

for

>>> inclusion
>>> in the compilation process?
>>> Thanks,
>>> Andrew
>>
>> You need to compile the .rc file (see
>> http://www.digitalmars.com/ctg/rcc.html), then add the

resulting .res

>> file
>> to dmd's command line.
>
> Awesome, this does the trick. However I get get a "GP Fault"?

during

> execution. Using windbg, I tracked it down to this piece of

code:

>
> void Create()
> {
>_hwnd = CreateWindowExA(
>  _exStyle,
>  cast(const(char*))_wc.GetName(), // returns string
>  cast(const(char*))_windowName,   // string variable
>  _style,
>  _x,
>  _y,
>  _width,
>  _height,
>  _hWndParent,
>  _hMenu,
>  _wc.GetInstance(),
>  _data);
>
>  assert(_hwnd, "Internal error: Window Creation Failed.");
> }
>
> The program craps at assert() but the error is generated. It

just

> displays a dialog box with the message: "test.exe has stopped

working,

> Windows is checking for a solution to the problem..."
>
> I'm thinking that _hwnd was never initialized and that assert

is access

> a null pointer but I cannot be sure. Any suggestions or ideas?
The
>  cast(const(char*))_wc.GetName()
line look *very* suspicious. You can't get a string and just

cast it to

const(char)*. Most importantly, the string (most likely) is not
null-terminated.
What you need to do here is the following:
auto className = toStringz(_ws.GetName());
auto caption = toStringz(_windowName);
and pass those 2 to the function.


Actually I've already tried that, it has no effect on the outcome.
From your suggestion though, I've gone back and replace all the
cast(const(char*)) usage throughout the program. Final verdict:
the program still crashes it the same location. It actually never
returns from CreateWindowExA().


Alternatively, you could make sure your strings are null-

terminated and

pass the pointer directly (e.g. _windowName.ptr):
string _windowName = "foo"; // null-terminated automatically
string _caption = ("Hello, World" ~ "\0")[0..$-1]; // append

trailing zero

to an existing string but exclude it from result (so that it's

not

included in _caption.length)




This is indeed strange, but it has nothing to do with the function itself.  
I still think the parameters you are passing might be invalid. Try setting  
them to default values and see if that helps. Also try wrapping the call  
with a try/catch block and output an exception you are getting (if any).


Mixins: to!string cannot be interpreted at compile time

2011-02-28 Thread Peter Lundgren
I'm trying to use mixins to generate an array of numbers that are coprime to a
statically known value. I've tried the following, but I receive the error:

Error: to(i) ~ ", " cannot be interpreted at compile time


string makePossibleAValues(string name, byte m) {
string result = "immutable byte[] "~name~" = [";
foreach (i; 0 .. m) {
if (coprime(i, m)) {
result ~= to!string(i) ~ ", ";
}
}
return result ~ "];";
}

bool coprime(ulong a, ulong b) {
return gcd(a, b) == 1;
}

ulong gcd(ulong a, ulong b) {
while (b) {
auto t = b;
b = a % b;
a = t;
}
return a;
}

mixin(makePossibleAValues("aValues", 26));


makePossibleAValues("aValues", 26) produces the correct result, "immutable
byte[] aValues = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25, ];", at runtime
and I know to!string can be used in mixins. Any idea as to why this particular
code is having trouble with to!string?