Re: How to pack types with variables in one message to send it to another thread? [tuple]

2014-09-07 Thread hane via Digitalmars-d-learn
On Sunday, 7 September 2014 at 10:42:37 UTC, MarisaLovesUsAll 
wrote:

Hi!
I'm trying to make my program multithreaded, and I was stuck at 
messaging between threads.
I need to pack types and variables into one message. Will I use 
Tuples or something?


e.g.

class Sprite {};

send(tid, Sprite, create, myInt);



Also I don't understand how to use Variant. Messages can be 
different, and I don't know how to extract data from variant.


send(tid, One, Two, myInt);

receive(
(Variant args)
{
/*
args contains Tuple!(string, string, int)(One, Two, 
42);
I need simple access to data, e.g. args[0] args[1] 
args[2]

but I don't know how to do this
because `.get` method need precise type of Tuple
*/
}
);

Regards,
MarisaLovesUsAll


receive() automatically expands tuples into multiple arguments.

receive((string s, string t, int i) {  });


Re: writeln() assertion failed in hybrid x64

2014-09-05 Thread hane via Digitalmars-d-learn

On Thursday, 4 September 2014 at 15:10:22 UTC, Jorge A. S. wrote:
I'm having an error related to yours: when I call writeln 
function in a closed stdout I will get a segfault message. 
Example:


import std.stdio;

void main() {
  stdout.close();
  write(hello\n);
}

The code above will crash with segfault buf the following code 
will raise an exception instead:


import std.stdio;

void main() {
  stdout.close();
  stdout.write(hello\n);
}

In one of the specializations of the write function in the 
std.stdio (the call site that you showed in your post) no check 
for closed stdout (when stdout._p is null) is done. I can't say 
if this is a bug in the write function or the desired behaviour 
(I'm a novice here).




With DMD of git master, the latter code seems to raise Exception 
(stdio.d(2247): Enforcement failed) instead of segfault.


Re: writeln() assertion failed in hybrid x64

2014-09-05 Thread hane via Digitalmars-d-learn

On Friday, 5 September 2014 at 07:22:23 UTC, hane wrote:
On Thursday, 4 September 2014 at 15:10:22 UTC, Jorge A. S. 
wrote:
I'm having an error related to yours: when I call writeln 
function in a closed stdout I will get a segfault message. 
Example:


import std.stdio;

void main() {
 stdout.close();
 write(hello\n);
}

The code above will crash with segfault buf the following code 
will raise an exception instead:


import std.stdio;

void main() {
 stdout.close();
 stdout.write(hello\n);
}

In one of the specializations of the write function in the 
std.stdio (the call site that you showed in your post) no 
check for closed stdout (when stdout._p is null) is done. I 
can't say if this is a bug in the write function or the 
desired behaviour (I'm a novice here).




With DMD of git master, the latter code seems to raise 
Exception (stdio.d(2247): Enforcement failed) instead of 
segfault.


Oops, I meant 'former' of course.


Re: Cross-module inlining with separate compilation?

2014-08-25 Thread hane via Digitalmars-d-learn
On Tuesday, 26 August 2014 at 04:34:39 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Mon, 25 Aug 2014 21:07:06 +
ponce via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com wrote:


Is there a way to have cross-module inlining but with separate 
compilation?

Like with link-time generation in C++ compilers.
i think that turning your functions into templates should do 
the trick.

i.e. int myfunc (int  n) -- int myfunc() (int n).

but this is a hack, of course.


No template hack needed now.
https://issues.dlang.org/show_bug.cgi?id=10985


Re: 'idiomatic' porting of c and or c++ code that does NULL checking

2014-08-23 Thread hane via Digitalmars-d-learn

On Saturday, 23 August 2014 at 10:19:59 UTC, nikki wrote:
I am learning SDL by following the lazyfoo SDL2 tuorials, I am 
alos new to D so I have a question:


I the lazyfoo tutorials there are many functions that have a 
bool success whiich gets set at various places when something 
goes wrong to be returned afterwards.


http://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php
for example:
[code]

bool init()
{
//Initialization flag
bool success = true;

//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO )  0 )
{
printf( SDL could not initialize! SDL_Error: %s\n, 
SDL_GetError() );

success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow( SDL Tutorial, 
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, 
SCREEN_HEIGHT, SDL_WINDOW_SHOWN );

if( gWindow == NULL )
{
printf( Window could not be created! SDL_Error: 
%s\n, SDL_GetError() );

success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}

return success;
}


[/code]


in my D code I just change the NULL into null and the printf to 
a fitting writeln.
But I am wondering if D offers me much cleaner ways of writing 
this, I am guessing the scope() but what would be the best/D 
way of writing this function?


How would you write it?


How about this?


import std.exception; // enforce() is declared in std.exception

void init()
{
// enforce() throws an exception if condition is false
	enforce(SDL_Init( SDL_INIT_VIDEO ) = 0, SDL could not 
initialize!);


// enforce() throws if SDL_CreateWindow returns null
	gWindow = SDL_CreateWindow(/* ... */).enforce(Window could not 
be created!);


// ditto
	gScreenSurface = SDL_GetWindowSurface(gWindow).enforce(Surface 
could not be created!);

}



Re: How to realize isSortedRange?

2014-08-20 Thread hane via Digitalmars-d-learn

On Wednesday, 20 August 2014 at 07:18:12 UTC, Kagamin wrote:

http://dlang.org/phobos/std_traits.html#TemplateOf


Or isInstanceOf.

static if (__traits(isSame, TemplateOf!R, SortedRange))
static if (isInstanceOf!(SortedRange, R))


Re: delegates GC allocations

2014-08-20 Thread hane via Digitalmars-d-learn

On Wednesday, 20 August 2014 at 14:44:39 UTC, Etienne wrote:
I've been hearing that delegates get a context pointer which 
will be allocated on the GC. Is this also true for delegates 
which stay in scope?


e.g.

void addThree() {
int val;
void addOne() {
val++;
}

addOne();
addOne();
addOne();

return val;
}

Will the above function allocate on the GC?


int getInt1() @nogc
{
int val;
int func() @nogc { return val; }
return func();
}

int delegate() getInt2() //@nogc - thie one allocates!
{
int val;
int func() @nogc { return val; }
return func;
}

int delegate() getInt3() @nogc
{
int val;
int func() @nogc { return 0; }
return func;
}


Re: Member access of __gshared global object

2014-08-06 Thread hane via Digitalmars-d-learn

On Wednesday, 6 August 2014 at 04:14:51 UTC, Puming wrote:
1. The only way that I can initialize it is to assign a value. 
But I want to initialize an empty AA, is that possible?


workaround:

string[string] aa;
assert(aa is null);

aa[] = ;
aa.remove();
assert(aa !is null);


Re: extern (C++, N) is it implemented?

2014-08-06 Thread hane via Digitalmars-d-learn
On Wednesday, 6 August 2014 at 06:50:59 UTC, Alexandr Druzhinin 
wrote:

This dlang.org/cpp_interface.html says I can do the following

// c++
namespace N {
void someCppFunction();
}

// d
extern (C++, N) void someCppFunction();

but this http://dpaste.dzfl.pl/e2242263e1dc says I can't

Is this feature implemented?


dpaste provides DMD2.063 as DMD 2.x Git!
http://dpaste.dzfl.pl/8dd68f98dbd73


Re: [dmd 2.066] Is scope with nothrow regression?

2014-07-06 Thread hane via Digitalmars-d-learn

On Sunday, 6 July 2014 at 12:31:42 UTC, NCrashed wrote:


```
void bar()
{
throw new Exception();
}

void foo() nothrow
{
scope(failure) {}
bar();
}

void main() {}
```
Doesn't compile with 2.066:
```
source/app.d(9): Error: 'app.bar' is not nothrow
source/app.d(6): Error: function 'app.foo' is nothrow yet may 
throw

```


This is a bug which was already fixed.
Instead, you can use this:
scope(failure) assert(0);

See: https://issues.dlang.org/show_bug.cgi?id=11542


Re: [dmd 2.066] Is scope with nothrow regression?

2014-07-06 Thread hane via Digitalmars-d-learn

On Sunday, 6 July 2014 at 22:03:21 UTC, hane wrote:

On Sunday, 6 July 2014 at 12:31:42 UTC, NCrashed wrote:


```
void bar()
{
throw new Exception();
}

void foo() nothrow
{
scope(failure) {}
bar();
}

void main() {}
```
Doesn't compile with 2.066:
```
source/app.d(9): Error: 'app.bar' is not nothrow
source/app.d(6): Error: function 'app.foo' is nothrow yet may 
throw

```


This is a bug which was already fixed.
Instead, you can use this:
scope(failure) assert(0);

See: https://issues.dlang.org/show_bug.cgi?id=11542


To be exact: a bug I said is that scope(failure) { } breaks 
nothrow checking.


Re: Why does this work?

2014-06-23 Thread hane via Digitalmars-d-learn

On Monday, 23 June 2014 at 08:30:44 UTC, h_zet wrote:

import std.typecons;

auto foo2(R)(R foopara){
return tuple(foopara, is(R==int));
}

void main(){
auto tuple(a,b) = foo2(1);
}


I'm expecting some error such as can not act as left value but 
when I compiled this, no error occured. DMD version is DMD64 
v2.065.(ldc2 exited with error function declaration without 
return type)


Why does this work? Or it is a bug?


You declared a variable template named tuple (with unused type 
parameters a, b) on that line.

http://dlang.org/template.html#variable-template

I think this is very confusable syntax...

void main()
{
auto tuple(a, b) = foo2(1);

writeln(tuple!(int, int)); // writes Tuple!(int, bool)(1, 
true)


tuple!(int, int) = foo2(20);
writeln(tuple!(int, int)); // writes Tuple!(int, bool)(20, 
true)


}


Re: Create const regex?

2014-06-07 Thread hane via Digitalmars-d-learn

On Saturday, 7 June 2014 at 11:33:35 UTC, Meta wrote:

On Saturday, 7 June 2014 at 00:48:59 UTC, hane wrote:

On Friday, 6 June 2014 at 15:42:41 UTC, Meta wrote:
You should not do this, as it will create a new regex 
everywhere you use it. Unlike const or immutable, enum in 
this situation is more or less like a C macro.


#define r1 regex(bla)


I see. Thanks.


Do you remember where you saw this in Phobos? It was probably 
unintended, and should be removed.


At std.regex.


BTW, I found that immutable regex can be created with enum.
enum r1_ = regex(bla);
immutable r1 = r1_;
Regex struct created during compiling can be immutable?


Re: Create const regex?

2014-06-06 Thread hane via Digitalmars-d-learn

On Friday, 6 June 2014 at 12:01:55 UTC, AntonSotov wrote:

const r1 = regex(bla);
matchFirst( big string, r1 );  //  ERROR!

immutable r2 = regex(bla); //  ERROR!

Why can I not use const/immutable regex?


I think it's a Phobos bug that can't use regex as immutable.

You can use const regex with getting rid of const attribute 
with cast().

matchFirst( big string, cast()r1 );

Or using enum seems better way. This style appears in Phobos's 
code.

enum r1 = regex(bla);


Re: Create const regex?

2014-06-06 Thread hane via Digitalmars-d-learn

On Friday, 6 June 2014 at 15:42:41 UTC, Meta wrote:
You should not do this, as it will create a new regex 
everywhere you use it. Unlike const or immutable, enum in this 
situation is more or less like a C macro.


#define r1 regex(bla)


I see. Thanks.


Re: Why std.algorithm.sort can't be applied to char[]?

2014-05-13 Thread hane via Digitalmars-d-learn

On Monday, 12 May 2014 at 14:56:46 UTC, John Colvin wrote:
char[] is a rather special type of array: the language has 
unicode support and iterates over it by code-point (i.e. not 
guaranteed to be a single char per iteration).


If you want to sort chars and are assuming ASCII, you can just 
use std.string.representation to work with them as integer 
types:


On Monday, 12 May 2014 at 16:30:12 UTC, Jonathan M Davis via 
Digitalmars-d-learn wrote:
All strings in D are treated as ranges of dchar, not their 
element type. This
has to with the fact that a char or wchar are only part of a 
character. If you

want to sort arrays of characters, you need to use dchar[].


I understand the reason. Thanks!


Why std.algorithm.sort can't be applied to char[]?

2014-05-12 Thread hane via Digitalmars-d-learn

and is there any way to sort char array with algorithm.sort?
---
import std.algorithm;
import std.range;

void main()
{
  int[] arr = [5, 3, 7];
  sort(arr); // OK

  char[] arr2 = ['z', 'g', 'c'];
  sort(arr2); // error
  sort!q{ a[0]  b[0] }(zip(arr, arr2)); // error
}
---
I don't know what's difference between int[] and char[] in D, but 
it's very unnatural.


Re: AES encryption with openssl bindings

2014-04-26 Thread hane via Digitalmars-d-learn

AES_set_decrypt_key is needed before AES_decrypt.

AES_set_decrypt_key(chunk.ptr, 128, wctx);