Re: Difference between stack-allocated class and struct

2011-05-03 Thread Sean Cavanaugh
Here is my prototype COM compile-time reflection based wrapper mixin 
(which I have abandoned in favor of alias this since it covers 95% of my 
use cases even though it isn't perfectly safe).  I am new at D so you 
have been warned, though this part of the language seems pretty 
straightforward enough.  It is possible the track down the argument 
names but takes some rather intricate parsing to do correctly (and the 
example someone linked me in another thread of mine chocked on const 
types due to parsing bugs).



http://snipt.org/xsu


The wrapped interface also needs to be a mixin so it can be created in 
the correct module, with visibility to all the types passed to the 
interface's methods.



So something like the following is going to fail miserably unless ComPtr 
is also made into a mixin and instantiated in the correct module.



struct ComPtr(T)
{
public:
T m_Pointer;
mixin(WrapComInterface!(T)(m_Pointer)
};




Four things

2011-05-03 Thread bearophile
Do you know if the following four problems are already present in Bugzilla? I 
don't remember (the only problem I am more sure is not present in Bugzilla is 
the missed priting in the first program).



import std.algorithm, std.stdio;
void main() {
int[int] hash = [1:2, 3:4];
auto vals = hash.byValue();
writeln(vals); // int delegate(int delegate(ref int))
map!q{a}(vals);
}


The writeln doesn't print the items.

And the last line causes this:

...\dmd\src\phobos\std\algorithm.d(110): Error: template instance 
Map!(result,int delegate(int delegate(ref int) dg)) does not match template 
declaration Map(alias fun,Range) if (isInputRange!(Unqual!(Range)))



void main() {
  auto a = [];
}


This is ugly.



struct Foo {
int x;
this(int xx) {
this.x = xx;
}
}
void main() {
enum f = Foo(10);
}


DMD 2.052 refuses this code that looks valid.

---

Thank you,
bye,
bearophile


Re: traits and class protection

2011-05-03 Thread Lutger Blijdestijn
Adam D.  Ruppe wrote:

 Is there a way in today's D to exclude members marked private and
 protected from processing in the __traits(allMembers) family of
 functions?
 
 I thought if I at least put it in a separate module, trying to get
 a private member would fail to compile, but I tried it and it seems
 to work anyway... my private members are both showing up and being
 called from another module.
 
 I'm open to filthy hacks too. Worst case is I can use a naming convention
 but I'd really like the public keyword to be the thing that actually
 matters.

Maybe some variation on the pimple idiom with opDispatch could work? It may 
be more trouble than its worth though. 

Most reflection mechanisms allow you to look at private symbols. This is 
probably not helpful to you, but whatever code that uses allMembers should 
just not do that and filter out private/protected members.




Re: Linux: How to statically link against system libs?

2011-05-03 Thread Lutger Blijdestijn
Alexander wrote:

 On 29.04.2011 11:41, Spacen Jasset wrote:
 
 You still *cannot* link statically to kernel32.dll. That's the
 difference. Linux glibc contains the C library functions *and* the
 syscalls, which is the bit that causes the problems.
 
   But at least I know, that no matter where I am, as long as I am using
   kernel32 only (no more dependencies), it will work on *any* Windows
   system (obviously, keeping backward compatibility in mind - something
   compiled on WinXP will work on all later
 versions) - which is not he case of Linux/glibc, unfortunately.
 
 msvcrt.dll and msvcrt.lib don't have any syscalls in them. they call
 though kernel32.dll dynamically.
 
   Actually, they do. Calling kernel32 is like making a syscall, that's the
   base of Win32 API, which is equivalent of Linux syscall.

A syscall is generally understood to be a call into the kernel for doing 
something that can't be done with user level privileges. So a call is either 
a syscall or it isn't, and none of kernel32 are. There are even functions in 
kernel32 which do not make a syscall.




Re: Linux: How to statically link against system libs?

2011-05-03 Thread Alexander
On 03.05.2011 16:29, Lutger Blijdestijn wrote:

 A syscall is generally understood to be a call into the kernel for doing 
 something that can't be done with user level privileges.

  Not really. syscalls are interface of user space to the OS kernel, and 
obviously, they can be made with user level privileges, otherwise nothing would 
be possible :)

  From the Linux syscalls manpage: The system call is the fundamental 
interface between an application and the Linux kernel.

 So a call is either a syscall or it isn't, and none of kernel32 are.

  kernel32 provides Win32 API, which is exactly (from application point of 
view) what syscalls in Linux are. In turn, kernel32 is interfacing to ntdll, 
which is near direct interface to the kernel.

 There are even functions in kernel32 which do not make a syscall.

  Sure, not all of Win32 API functions require syscalls. But again: In lots of 
cases, KERNEL32 APIs are just wrappers to NTDLL APIs.

  Unlike kernel32, though, libc is providing direct interface to syscalls - 
like open(), socket() etc. - yes, those are not libc functions, those are 
syscalls (wrapped a bit to follow C calling convention).

  Probably, nowadays libc has wrappers around syscalls, checking arguments etc 
- but those are not necessary, the difference is only in calling convention.

/Alexander


Re: Four things

2011-05-03 Thread Andrej Mitrovic
On 5/3/11, bearophile bearophileh...@lycos.com wrote:
 struct Foo {
 int x;
 this(int xx) {
 this.x = xx;
 }
 }
 void main() {
 enum f = Foo(10);
 }


Perhaps a relevant bug is where a struct-typed enum doesn't call the
struct ctor if it has one, but uses field-by-field initialization
instead. See http://d.puremagic.com/issues/show_bug.cgi?id=5460


Coroutines in D

2011-05-03 Thread Andrej Mitrovic
I'm trying to figure out how to use coroutines in D.

First, I think I've run into some kind of bug. I've followed this C++ example: 
http://www.subatomicglue.com/secret/coro/readme.html, and came up with this:

import std.stdio;
import core.thread;

void fiberFunc(size_t arg)
{
int i = 0;

foreach (x; 0 .. 1000)
{
writefln(fiber waiting (id = %d), arg);
Fiber.yield();
}

foreach (x; 0 .. 1000)
{
writefln(fiber running (iter = %d id = %d), ++i, arg);
Fiber.yield();
}

while (1)
{
writefln(fiber waiting (id = %d), arg);
Fiber.yield();
}
}

void main()
{
Fiber[200] fibers;

foreach (ref fiber; fibers)
{
fiber = new Fiber(fiberFunc);
}

while (true)
{
foreach (fiber; fibers)
{
fiber.call();
}
}
}

Only after I've translated the C++ example to D I've realized that fiberFunc 
actually takes an argument. But from what I can tell core.thread.Fiber never 
calls this function with any arguments. I have a hunch that arg ends up 
having an uninitialized garbage value, so it looks like this is a bug?

Ok, that put aside what I really want to emulate is this Python example of a 
coroutine (perhaps bearophile is familiar with this):

def coroutine(func):
def start(*args,**kwargs):
cr = func(*args,**kwargs)
cr.next()
return cr
return start

@coroutine
def unwrap_protocol(header='\x61',
footer='\x62',
dle='\xAB',
after_dle_func=lambda x: x,
target=None):
 Simplified framing (protocol unwrapping)
co-routine.

# Outer loop looking for a frame header
#
while True:
byte = (yield)
frame = ''

if byte == header:
# Capture the full frame
#
while True:
byte = (yield)
if byte == footer:
target.send(frame)
break
elif byte == dle:
byte = (yield)
frame += after_dle_func(byte)
else:
frame += byte

@coroutine
def frame_receiver():
 A simple co-routine sink for receiving
full frames.

while True:
frame = (yield)
print 'Got frame:', frame.encode('hex')


bytes = ''.join(chr(b) for b in
[0x70, 0x24,
 0x61, 0x99, 0xAF, 0xD1, 0x62,
 0x56, 0x62,
 0x61, 0xAB, 0xAB, 0x14, 0x62,
 0x7
])

unwrapper = unwrap_protocol(target=frame_receiver())

for byte in bytes:
unwrapper.send(byte)

This was taken from Eli's blog: 
http://eli.thegreenplace.net/2009/08/29/co-routines-as-an-alternative-to-state-machines/

I'm not seeing any way of yielding a value back from a Fiber or even sending 
it, so I don't see how I can use fibers to implement coroutines which send or 
return values. But I'm new to the concept so maybe I'm missing something 
obvious?





Re: Coroutines in D

2011-05-03 Thread Andrej Mitrovic
Ok,I've found a post that could be useful:
http://www.digitalmars.com/d/archives/digitalmars/D/yield_C_etc_74821.html

and there's a Generator mixin in generators.d, from the libs_d
library: http://www.fantascienza.net/leonardo/so/index.html

I'll give these a try soon.


std.parallism

2011-05-03 Thread jdrewsen

Hi,

   Does anyone know how unshared parameters are handled when executing 
a task in std.parallelism.


Most of the examples uses them e.g. when iterating over an unshared 
array (parallelSort example).


I did give the source a quick look but couldn't find a cast to shared 
for parameters anywhere.


Any insights?

Thx.
Jonas


Re: Coroutines in D

2011-05-03 Thread Robert Clipsham

On 03/05/2011 19:06, Andrej Mitrovic wrote:

I'm trying to figure out how to use coroutines in D.

First, I think I've run into some kind of bug. I've followed this C++ example: 
http://www.subatomicglue.com/secret/coro/readme.html, and came up with this:


I'm not entirely sure what it is you want to be able to do (I'm rather 
tired and didn't want to read through the whole example, I'll take 
another look tomorrow unless someone else beats me to it), but from what 
I can gather you want to pass values to the fiber between calling and 
yielding?


The way to do this is to derive your fiber rather than composing it:

class Derived : Fiber
{
size_t arg;
this()
{
super(run);
}
void func()
{
writefln(val: %s, arg);
Fiber.yield();
writefln(val: %s, arg);
}
}

auto fiber = new Derived;
fiber.arg = 6;
fiber.call();
fiber.arg = 7;
fiber.call();

You have, however, encountered a bug - Fiber should not accept a 
function with parameters, you should file a bug report for this.


Hope this helps.

--
Robert
http://octarineparrot.com/


AA.get() problem

2011-05-03 Thread bearophile
This gives me compilation errors, is this a bug, or is my code wrong?


import std.stdio;
void main() {
string[char] tab = ['e': red, 'b': blue];
string r;
foreach (c; aba) {
// if (c in tab) r ~= tab[c]; // OK
r ~= tab.get(c, ); // ERR
}
writeln(r);
}


Thank you, bye,
bearophile