Re: Shared with synchronized

2019-03-03 Thread r-const-dev via Digitalmars-d-learn

On Sunday, 3 March 2019 at 22:35:54 UTC, r-const-dev wrote:
I'm trying to implement a thread safe class, guarding data 
access with synchronized and atomicOp.


Inside the class I'm using non-shared fields, such as Nullable, 
but I guarantee the thread safety using synchronized. How can I 
tell the compiler to allow using non-shared fields/methods 
inside synchronized?


Here is my example:

import core.atomic : atomicOp;
import std.typecons : Nullable;

class SharedObject {
private Object lock = new Object();
private Nullable!int sharedValue;
private int changeCount = 0;
synchronized void modifyValue(int newValue) {
sharedValue = newValue;
atomicOp!("+=")(changeCount, 1);
}
}

void main()
{
shared SharedObject data = new shared SharedObject();
data.modifyValue(3);
}

I get the error:
onlineapp.d(9): Error: template 
std.typecons.Nullable!int.Nullable.opAssign cannot deduce 
function from argument types !()(int) shared, candidates are:

/dlang/dmd/linux/bin64/../../src/phobos/std/typecons.d(2884):
   std.typecons.Nullable!int.Nullable.opAssign()(T value)


Found a solution, hope it is a recommended one: cast `this` to 
non-shared and invoke an "unsafe" method. Inside everything is 
allowed:




import std.typecons : Nullable;

class SharedObject {
private Nullable!int sharedValue;
private int changeCount = 0;
synchronized void modifyValue(int newValue) {
(cast(SharedObject)this).unsafeModifyValue(newValue);
}
private void unsafeModifyValue(int newValue) {
sharedValue = newValue;
++changeCount;
}
}

void main()
{
shared SharedObject data = new shared SharedObject();
data.modifyValue(3);
}



Shared with synchronized

2019-03-03 Thread r-const-dev via Digitalmars-d-learn
I'm trying to implement a thread safe class, guarding data access 
with synchronized and atomicOp.


Inside the class I'm using non-shared fields, such as Nullable, 
but I guarantee the thread safety using synchronized. How can I 
tell the compiler to allow using non-shared fields/methods inside 
synchronized?


Here is my example:

import core.atomic : atomicOp;
import std.typecons : Nullable;

class SharedObject {
private Object lock = new Object();
private Nullable!int sharedValue;
private int changeCount = 0;
synchronized void modifyValue(int newValue) {
sharedValue = newValue;
atomicOp!("+=")(changeCount, 1);
}
}

void main()
{
shared SharedObject data = new shared SharedObject();
data.modifyValue(3);
}

I get the error:
onlineapp.d(9): Error: template 
std.typecons.Nullable!int.Nullable.opAssign cannot deduce 
function from argument types !()(int) shared, candidates are:
/dlang/dmd/linux/bin64/../../src/phobos/std/typecons.d(2884): 
   std.typecons.Nullable!int.Nullable.opAssign()(T value)


Re: Setting immutable static data inside a struct in a module constructor?

2019-03-03 Thread aliak via Digitalmars-d-learn

On Sunday, 3 March 2019 at 20:41:36 UTC, Alex wrote:

On Sunday, 3 March 2019 at 20:10:14 UTC, aliak wrote:
Is it possible to initialize static immutable members of a 
struct like you could do for a global immutable one?


immutable string a;
struct Test {
  static immutable string b;
}

shared static this() {
  a = "foo";
  Test.b = "bar";
}

Error: cannot modify immutable expression b


Hmm... should work, I think...
But this works in any case:

immutable string a;
struct Test {
  static immutable string b;
  shared static this() {
b = "bar";
  }
}


Ah, did not know that. Thank you!


Re: Setting immutable static data inside a struct in a module constructor?

2019-03-03 Thread Alex via Digitalmars-d-learn

On Sunday, 3 March 2019 at 20:10:14 UTC, aliak wrote:
Is it possible to initialize static immutable members of a 
struct like you could do for a global immutable one?


immutable string a;
struct Test {
  static immutable string b;
}

shared static this() {
  a = "foo";
  Test.b = "bar";
}

Error: cannot modify immutable expression b


Hmm... should work, I think...
But this works in any case:

immutable string a;
struct Test {
  static immutable string b;
  shared static this() {
b = "bar";
  }
}

shared static this() {
  a = "foo";
}

void main(){
assert(a == "foo");
assert(Test.b == "bar");
}


Setting immutable static data inside a struct in a module constructor?

2019-03-03 Thread aliak via Digitalmars-d-learn
Is it possible to initialize static immutable members of a struct 
like you could do for a global immutable one?


immutable string a;
struct Test {
  static immutable string b;
}

shared static this() {
  a = "foo";
  Test.b = "bar";
}

Error: cannot modify immutable expression b


Re: Help with Regular Expressions (std.regex)

2019-03-03 Thread user1234 via Digitalmars-d-learn

On Sunday, 3 March 2019 at 18:32:14 UTC, user1234 wrote:

On Sunday, 3 March 2019 at 18:07:57 UTC, Samir wrote:
or  // sorry i don't have the regex API in mind

  import std.array: array;
  import std.alogrithm.iteration : map;
  auto allMatches = matchAll(line, pattern).map(a => 
a.hit).array;


oops forgot the bang

  auto allMatches = matchAll(line, pattern).map!(a => 
a.hit).array;





static if, compiler bug?

2019-03-03 Thread Andre Pany via Digitalmars-d-learn

Hi,

in druntime 
https://github.com/dlang/druntime/blob/master/src/core/sys/posix/sys/types.d#L113

there is following definiton:

else version (CRuntime_Musl)
{
alias long  blksize_t;
alias ulong nlink_t;
alias long  dev_t;
alias long  blkcnt_t;
alias ulong ino_t;
alias long  off_t;
alias long  _Addr;
alias int   pid_t;
alias uint  uid_t;
alias uint  gid_t;
alias long  time_t;
alias long  clock_t;
alias ulong pthread_t;
alias _Addr ssize_t;
}

If I replace the line "alias long off_t;" with:

  static if ( __USE_FILE_OFFSET64 )
  {
alias long  off_t;
  }
  else
  {
alias slong_t  off_t;
  }

the I got following compiler error:
/mnt/c/D/ldc-1.14.0-src/runtime/druntime/src/core/stdc/stdio.d(1676): Error: 
module `core.sys.posix.sys.types` import off_t not found

I cannot explain this error, because off_t is defined in both 
cases of the static if.

Even if I change it to:

  static if ( true )
alias long off_t;

I get the same error. Is this a compiler bug?

Kind regards
André




Re: Help with Regular Expressions (std.regex)

2019-03-03 Thread user1234 via Digitalmars-d-learn

On Sunday, 3 March 2019 at 18:07:57 UTC, Samir wrote:
I am belatedly working my way through the 2018 edition of the 
Advent of Code[1] programming challenges using D and am stumped 
on Problem 3[2].  The challenge requires you to parse a set of 
lines in the format:

#99 @ 652,39: 24x23
#100 @ 61,13: 15x24
#101 @ 31,646: 16x28

I would like to store each number (match) as an element in an 
array so that I can refer to them by index.  For example, for 
the first line:


m = [99, 652, 39, 24, 23]
assert(m[0] == 99);
assert(m[1] == 652);
// ...
assert(m[4] == 23);

What is the best way to do this?  (I will worry about 
converting characters to integers later.)


I have the following solution so far based on reading Dmitry 
Olshansky's article on std.regex[3] and the std.regex 
documention[4]:


import std.stdio;
import std.regex;

void main() {
auto line= "#99 @ 652,39: 24x23";
auto pattern = regex(r"\d+");
auto m   = matchAll(line, pattern);
writeln(m);
}

which results in:
[["99"], ["652"], ["39"], ["24"], ["23"]]

But this doesn't seem to be an iterable array as changing 
writeln(m) to writeln(m[0]) yields

Error: no [] operator overload for type RegexMatch!string

Changing the line to writeln(m.front[0]) yields
99

but m.front doesn't allow me to access other elements (i.e. 
m.front[1]):

requested submatch number 1 is out of range

??:? _d_assert_msg [0x4dc27a]
??:? inout pure nothrow @trusted inout(immutable(char)[]) 
std.regex.Captures!(immutable(char)[]).Captures.opIndex!().opIndex(ulong) [0x4d8d57]

??:? _Dmain [0x49ffc8]

I've tried something like
foreach (m; matchAll(line, pattern))
writeln(m.hit);

which is close but doesn't result in an array.  Do I need to 
use matchFirst?


Thanks in advance.
Samir

[1] https://adventofcode.com/2018
[2] https://adventofcode.com/2018/day/3
[3] https://dlang.org/articles/regular-expression.html
[4] https://dlang.org/phobos/std_regex.html


Hello, Something like this should work:

  import std.array: array;
  auto allMatches = matchAll(line, pattern).array;

or  // sorry i don't have the regex API in mind

  import std.array: array;
  import std.alogrithm.iteration : map;
  auto allMatches = matchAll(line, pattern).map(a => a.hit).array;


What happened with `writeln` is that it iterates the `matchAll` 
results which is an input range, which is lazy. `.array` stores 
the results in an array.


Help with Regular Expressions (std.regex)

2019-03-03 Thread Samir via Digitalmars-d-learn
I am belatedly working my way through the 2018 edition of the 
Advent of Code[1] programming challenges using D and am stumped 
on Problem 3[2].  The challenge requires you to parse a set of 
lines in the format:

#99 @ 652,39: 24x23
#100 @ 61,13: 15x24
#101 @ 31,646: 16x28

I would like to store each number (match) as an element in an 
array so that I can refer to them by index.  For example, for the 
first line:


m = [99, 652, 39, 24, 23]
assert(m[0] == 99);
assert(m[1] == 652);
// ...
assert(m[4] == 23);

What is the best way to do this?  (I will worry about converting 
characters to integers later.)


I have the following solution so far based on reading Dmitry 
Olshansky's article on std.regex[3] and the std.regex 
documention[4]:


import std.stdio;
import std.regex;

void main() {
auto line= "#99 @ 652,39: 24x23";
auto pattern = regex(r"\d+");
auto m   = matchAll(line, pattern);
writeln(m);
}

which results in:
[["99"], ["652"], ["39"], ["24"], ["23"]]

But this doesn't seem to be an iterable array as changing 
writeln(m) to writeln(m[0]) yields

Error: no [] operator overload for type RegexMatch!string

Changing the line to writeln(m.front[0]) yields
99

but m.front doesn't allow me to access other elements (i.e. 
m.front[1]):

requested submatch number 1 is out of range

??:? _d_assert_msg [0x4dc27a]
??:? inout pure nothrow @trusted inout(immutable(char)[]) 
std.regex.Captures!(immutable(char)[]).Captures.opIndex!().opIndex(ulong) [0x4d8d57]

??:? _Dmain [0x49ffc8]

I've tried something like
foreach (m; matchAll(line, pattern))
writeln(m.hit);

which is close but doesn't result in an array.  Do I need to use 
matchFirst?


Thanks in advance.
Samir

[1] https://adventofcode.com/2018
[2] https://adventofcode.com/2018/day/3
[3] https://dlang.org/articles/regular-expression.html
[4] https://dlang.org/phobos/std_regex.html


Re: Concatenating compile time sequences

2019-03-03 Thread aliak via Digitalmars-d-learn

On Saturday, 2 March 2019 at 02:38:09 UTC, H. S. Teoh wrote:
On Sat, Mar 02, 2019 at 02:16:22AM +, Victor Porton via 
Digitalmars-d-learn wrote:

[...]


Keep in mind that sequences produced by AliasSeq are 
auto-expanding, meaning the above construct will automatically 
flatten into a flat AliasSeq!(int, "x", float, "y", double, 
"z").  If that's not what you want, you need to wrap your 
subsequences in a separate, non-eponymous template.




[...]


I'm not sure what "alias enum" is supposed to mean; is that a 
typo? Surely you mean just "alias"?




[...]


This line doesn't do what you think it does, because of 
auto-expansion. It's essentially exactly the same thing as:


private alias processFields(T, name, Fields...) =
AliasSeq!(T, name, processFields!(Fields));

i.e., the nested AliasSeq has no effect.



[...]


If you want anything that retains a nested structure, you 
cannot use AliasSeq because of auto-expansion.  You need to 
define your own, non-eponymous template container, e.g.:


template MySeq(T...) {
alias data = T;
}

alias processFields(T, name, Fields...) =
AliasSeq!(MySeq!(T, name), MySeq!(processFields!(Fields)));

The MySeq!(...) "protect" their contents from flattening into 
the outer list, while the outer AliasSeq causes individual 
MySeq!(...)'s to be promoted to the top level sequence rather 
than producing a tree-like structure.


Note that to access the data inside a MySeq, you'll have to use 
.data, for example:


alias fields = processFields!(int, "x", float, "y");

alias type0 = fields[0].data[0]; // int
string name0 = fields[0].data[1]; // "x"
alias type1 = fields[1].data[0]; // float
string name1 = fields[1].data[1]; // "y"

Hope this helps.


T


There's a package called bolts that has an AliasPack defined that 
could allow you to do something like:


template split(seq...) if (seq.length % 2 == 0) {
static if (seq.length >= 2) {
alias Pair = AliasPack!(seq[0], seq[1]);
alias split = AliasSeq!(Pair, .split!(seq[2..$]));  
} else {
alias split = AliasSeq!();
}
}

Running code: https://run.dlang.io/is/lfTOBz

Cheers,
- Ali


Re: How can I build dynamic library with ldc in termux?

2019-03-03 Thread Domain via Digitalmars-d-learn

On Sunday, 3 March 2019 at 01:51:49 UTC, Domain wrote:

[...]


Sorry, my mistake.
I can build dynamic library now.


Re: How can I build dynamic library with ldc in termux?

2019-03-03 Thread Domain via Digitalmars-d-learn

On Sunday, 3 March 2019 at 12:25:28 UTC, kinke wrote:

On Sunday, 3 March 2019 at 01:47:50 UTC, Domain wrote:
/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
cannot find -lphobos2-ldc-shared
/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
cannot find -ldruntime-ldc-shared


Looks like that package isn't set-up 100% correctly - there 
should be a `-link-defaultlib-shared=false` as default switch 
in the etc/ldc2.conf config file if the package doesn't contain 
shared druntime/Phobos.


It seems already there:
// default switches injected before all explicit comma>
  switches = [
  "-defaultlib=phobos2-ldc,druntime-ldc",
  "-link-defaultlib-shared=false",
  ];


Re: How can I build dynamic library with ldc in termux?

2019-03-03 Thread kinke via Digitalmars-d-learn

On Sunday, 3 March 2019 at 01:47:50 UTC, Domain wrote:
/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
cannot find -lphobos2-ldc-shared
/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
cannot find -ldruntime-ldc-shared


Looks like that package isn't set-up 100% correctly - there 
should be a `-link-defaultlib-shared=false` as default switch in 
the etc/ldc2.conf config file if the package doesn't contain 
shared druntime/Phobos.


Re: How can I build dynamic library with ldc in termux?

2019-03-03 Thread Guillaume Piolat via Digitalmars-d-learn

On Sunday, 3 March 2019 at 01:51:49 UTC, Domain wrote:

On Sunday, 3 March 2019 at 01:47:50 UTC, Domain wrote:
/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
cannot find -lphobos2-ldc-shared
/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
cannot find -ldruntime-ldc-shared


Any dub config example?


Perhaps this:

add this flag to your dub.json:

"dflags-linux-dmd": ["-defaultlib=libphobos2.a"],

and if you are using SDLang, convert it to JSON before :)