How to compile Windows exe files from this source

2020-08-09 Thread Marc via Digitalmars-d-learn
I don't know much more about D than creating a 'hello world' exe 
file with the DMD Compiler

but I'm interested in using the eBay/tsv-utils binaries.
Unfortunately, the author didn't create any MS Windows binaries:
https://github.com/eBay/tsv-utils/releases

Does anyone know how to compile this code into MS Windows 
binaries?




Can I count the of enum's fields at compile time?

2017-11-22 Thread Marc via Digitalmars-d-learn

for example:

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = coutOfFields(A); // 5 fields



Re: Can I count the of enum's fields at compile time?

2017-11-22 Thread Marc via Digitalmars-d-learn
On Thursday, 23 November 2017 at 01:01:42 UTC, Michael V. 
Franklin wrote:

On Thursday, 23 November 2017 at 00:58:21 UTC, Marc wrote:

for example:

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = coutOfFields(A); // 5 fields


https://dlang.org/spec/traits.html#allMembers

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = __traits(allMembers, A).length; // 5 fields

static assert(countOfA == 5);

Mike


This was fast! Thanks


Re: Can I count the of enum's fields at compile time?

2017-11-22 Thread Marc via Digitalmars-d-learn
On Thursday, 23 November 2017 at 01:04:29 UTC, Jonathan M Davis 
wrote:
On Thursday, November 23, 2017 00:58:21 Marc via 
Digitalmars-d-learn wrote:

for example:

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = coutOfFields(A); // 5 fields


import std.traits;

enum countOfA = EnumMembers!A.length;

- Jonathna M Davis


This sounds more readable. I was going to write a "function 
extension" to enum but I think it isn't really needed. Thank you 
too.


Re: Can I count the of enum's fields at compile time?

2017-11-22 Thread Marc via Digitalmars-d-learn

On Thursday, 23 November 2017 at 01:34:54 UTC, Ali Çehreli wrote:

On 11/22/2017 05:21 PM, Marc wrote:
On Thursday, 23 November 2017 at 01:04:29 UTC, Jonathan M 
Davis wrote:
On Thursday, November 23, 2017 00:58:21 Marc via 
Digitalmars-d-learn wrote:

[...]


import std.traits;

enum countOfA = EnumMembers!A.length;

- Jonathna M Davis


This sounds more readable. I was going to write a "function 
extension" to enum but I think it isn't really needed. Thank 
you too.


As an eponymous template:

enum One { a }
enum Three { a, b, c }

import std.range : EnumMembers;
enum countOf(E) = EnumMembers!E.length;

unittest {
static assert(countOf!One == 1);
static assert(countOf!Three == 3);
}

void main() {
}

Ali


whoa, this is so easy and elegant. I'm falling in love with D. 
Thank you too!


lower case only first letter of word

2017-12-05 Thread Marc via Digitalmars-d-learn
Does D have a native function to capitalize only the first letter 
of the word? (I'm asking that so I might avoid reinvent the 
wheel, which I did sometimes in D)


Re: lower case only first letter of word

2017-12-05 Thread Marc via Digitalmars-d-learn

On Tuesday, 5 December 2017 at 13:40:08 UTC, Daniel Kozak wrote:
but this will change all other uppercase to lowercase, so maybe 
it is not what you want. If you really want just change first 
char to upper, then there is nothing wrong to do it yourself


On Tue, Dec 5, 2017 at 2:37 PM, Daniel Kozak 
 wrote:


Something like this: 
https://dlang.org/phobos/std_uni.html#asCapitalized


On Tue, Dec 5, 2017 at 2:31 PM, Marc via Digitalmars-d-learn < 
digitalmars-d-learn@puremagic.com> wrote:


Does D have a native function to capitalize only the first 
letter of the word? (I'm asking that so I might avoid 
reinvent the wheel, which I did sometimes in D)


Yes, this is not what I want. I want to convert only the first 
letter of the word to lower case and left all the others 
immutable. similar to PHP's lcfirst():


http://php.net/manual/en/function.lcfirst.php


how do I read a class member's value using traits?

2017-12-15 Thread Marc via Digitalmars-d-learn
I need to give a class C, read all user-defined members of it, 
both name and value dynamically. for example:



class C {
  string a;
  string b;
  string c;
}


then


Class c = new C();
// set c members...
enum string[] members = [__traits(allMembers, C)];
foreach(string member; members) {
string value = __traits(getMember, c, member);
doSomething(member, value);
}


I get this error:


Error: variable member cannot be read at compile time
Error: string expected as second argument of __traits 
`getMember` instead of `__error`


why can't member in foreach(string member; members) be read at 
compile time? I've also tried



foreach(enum string member; members)

and

static foreach(string member; members)


but it result a lot of error messages.





get only user-defined members

2017-12-15 Thread Marc via Digitalmars-d-learn

how do I from class:


class Person {
 string name;
 int age;
}


do:


auto c = [__traits(allMembers, Person)];


then return only ["name", "age"] rather ["name, "age", "ctor", 
"toString" ... ]?


Re: get only user-defined members

2017-12-16 Thread Marc via Digitalmars-d-learn
On Saturday, 16 December 2017 at 07:23:38 UTC, Jonathan M Davis 
wrote:
On Saturday, December 16, 2017 04:01:10 Marc via 
Digitalmars-d-learn wrote:

how do I from class:
> class Person {
>
>  string name;
>  int age;
>
> }

do:
> auto c = [__traits(allMembers, Person)];

then return only ["name", "age"] rather ["name, "age", "ctor", 
"toString" ... ]?


Try __traits(derivedMembers, Person).

https://dlang.org/spec/traits.html#derivedMembers

Depending on what you want though, it's not all that uncommon 
to use a variety of traits to filter the list down to whatever 
it is that you actually want. std.traits and std.meta are your 
friends in addition to __traits.


- Jonathan M Davis


It derivedMembers worked but I didn't understand how so. It 
returned the proper array ["name", "age", "this"] but how are 
them derived? or it's D's design that every class is implicitily 
derived from a "main objet"?
Thanks for your suggeston on std.traits and std.meta, I didn't 
know about the last one.


Re: how do I read a class member's value using traits?

2017-12-16 Thread Marc via Digitalmars-d-learn
On Saturday, 16 December 2017 at 03:48:01 UTC, Jonathan M Davis 
wrote:
On Saturday, December 16, 2017 03:34:43 Marc via 
Digitalmars-d-learn wrote:

I need to give a class C, read all user-defined members of it,

both name and value dynamically. for example:
> [...]

then

>[...]

I get this error:
> [...]

why can't member in foreach(string member; members) be read at
compile time? I've also tried


You're trying to read a runtime value at compile time, and 
that's not going to work. c doesn't exist until runtime, so 
none of its members exist until runtime. You can introspect on 
the type C, but the variable c is a runtime entity.


- Jonathan M Davis


You're right. I changed to generate code to do it properly at 
runtime, something like this:


enum string[] members = [__traits(derivedMembers, Field)] [0 .. 
$ - 1];

static foreach(enum string member; members) {
doSomething(member, __traits(getMember, fields, 
member));

}

Not sure if best approach. note: I know [0 .. $ -1] is probably 
wrong. I'll fix it


Does D have class' attributes like C#'s?

2017-12-16 Thread Marc via Digitalmars-d-learn
C# has a quite nice way to store metadata about a property by a 
feature called atributes[1]. For example, I can write something 
like this:



class A {
   [TextSize(256)]
   string Name { get; set; }
}


So using runtime/reflection I can retrieve the TextSize value 
associated to A.name property.


Does D have something similar?


Re: Does D have class' attributes like C#'s?

2017-12-16 Thread Marc via Digitalmars-d-learn

On Saturday, 16 December 2017 at 20:05:15 UTC, Anonymouse wrote:

On Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:
C# has a quite nice way to store metadata about a property by 
a feature called atributes[1]. For example, I can write 
something like this:



class A {
   [TextSize(256)]
   string Name { get; set; }
}


So using runtime/reflection I can retrieve the TextSize value 
associated to A.name property.


Does D have something similar?


UDAs? User Defined Attributes.

https://dlang.org/spec/attribute.html#UserDefinedAttribute
http://ddili.org/ders/d.en/uda.html

class A {
@TextSize(256)
string name() { /* ... */ }
}


I can't "pack" an object, right? In C#, TextSize is a class and 
256 is constructor's first argument. In D it's pretty much an 
array but I guess it's close enough. Thanks!




How do I pass a type as parameter in this method?

2017-12-18 Thread Marc via Digitalmars-d-learn

Imaginary code:


int index = FirstOrDefault!(int)(__traits(getAttributes, C.a));


In that case, if the tuple is empty, the value is the int's type 
default value.


The method is defined as  following:


template FirstOrDefault(X)(T...) {
static if(T.length > 0) {
enum FirstOrDefault = T[0];
} else {
enum FirstOrDefault = X.init;
}
}


Re: How do I pass a type as parameter in this method?

2017-12-19 Thread Marc via Digitalmars-d-learn

On Tuesday, 19 December 2017 at 00:01:00 UTC, Ali Çehreli wrote:

On 12/18/2017 03:54 PM, Ali Çehreli wrote:

On 12/18/2017 02:58 PM, Marc wrote:


Here's another experiment:

template FirstOf(T...) {
template otherwise(D) {
static if (T.length == 0) {
enum otherwise = D.init;
} else {
enum otherwise = T[0];
}
}
}

void main() {
static assert (FirstOf!(1.5, "hello").otherwise!int == 1.5);
static assert (FirstOf!("world", [1]).otherwise!int == 
"world");

static assert (FirstOf!().otherwise!int == 0);
}

Ali


Thanks four answer. I'll be using this one. I was messing around 
and getting multiple arguments to template function too. I like 
the naming too, it made code clear, imo.


It's possible to have overload where one take a type name and the 
other a variable (constant value actually)? something like this 
(also imaginary code):



template FirstOf(TP...) {
template otherwise(D) {
static if(TP.length > 0) {
enum otherwise = TP[0];
} else {
enum otherwise = D.init;
}
}
template otherwise(D value) {
static if(TP.length > 0) {
enum otherwise = TP[0];
} else {
enum otherwise = value;
}
}
}


So I can use like this:


int index = FirstOf!(myTuple).otherwise!int;
int value = FirstOf(myTuple2).otherwise!(10);


with my limited template knowledge, I know I can write it like 
this:



template otherwise(D, D value) {
static if(TP.length > 0) {
enum otherwise = TP[0];
} else {
enum otherwise = value;
}
}


So the call would go like this:


int value = FirstOf(myTuple2).otherwise!(int, 10);


But for simplicity I'd like to infer the type from constant value 
passed in parameter, in that case, the integer value of 10.


Re: How do I pass a type as parameter in this method?

2017-12-19 Thread Marc via Digitalmars-d-learn

On Tuesday, 19 December 2017 at 15:52:57 UTC, Dgame wrote:

On Tuesday, 19 December 2017 at 15:19:53 UTC, Marc wrote:

[...]


template FirstOf(T...) {
template otherwise(D) {
static if (T.length == 0) {
enum otherwise = D.init;
} else {
enum otherwise = T[0];
}
}

template otherwise(alias value) {
static if (T.length == 0) {
enum otherwise = value;
} else {
enum otherwise = T[0];
}
}
}

void main() {
static assert (FirstOf!(1.5, "hello").otherwise!int == 1.5);
static assert (FirstOf!("world", [1]).otherwise!int == 
"world");

static assert (FirstOf!().otherwise!int == 0);
static assert (FirstOf!(1.5, "hello").otherwise!23 == 1.5);
static assert (FirstOf!().otherwise!42 == 42);
}


Didn't know about alias as template paramter. Exactly what I 
wanted. Thank you!


Can I run this at compile time?

2017-12-20 Thread Marc via Digitalmars-d-learn

Give this function I'd like to run it at compile time:

import std.concurrency : Generator, yield;

Generator!string getNonIntegralMembers() {
return new Generator!string({
enum allMembers = __traits(derivedMembers, C);
foreach(enum string member; allMembers) {
bool isInteg = __traits(isIntegral, >__traits(getMember, C, 
member));

if(!isInteg) {
yield(member);
}
}
});
}


but when I do:


enum string[] members = getNonIntegralMembers();
writeln(members);


I get the following erros:

C:\D\dmd2\windows\bin\..\..\src\druntime\import\core\thread.d(4059): Error: 
static variable PAGESIZE cannot be read at compile time
C:\D\dmd2\windows\bin\..\..\src\phobos\std\concurrency.d(1548):   
 called from here: super.this(dg, PAGESIZE * 4u, PAGESIZE)

app.d(96):called from here: getNonIntegralMembers()



Re: Can I run this at compile time?

2017-12-20 Thread Marc via Digitalmars-d-learn

On Wednesday, 20 December 2017 at 17:16:50 UTC, Mengu wrote:

On Wednesday, 20 December 2017 at 16:54:35 UTC, Marc wrote:

Give this function I'd like to run it at compile time:

import std.concurrency : Generator, yield;

[...]


but when I do:


[...]


I get the following erros:

C:\D\dmd2\windows\bin\..\..\src\druntime\import\core\thread.d(4059): Error: 
static variable PAGESIZE cannot be read at compile time
C:\D\dmd2\windows\bin\..\..\src\phobos\std\concurrency.d(1548):
 called from here: super.this(dg, PAGESIZE * 4u, PAGESIZE)
app.d(96):called from here: getNonIntegralMembers()


if PAGESIZE is not dynamic, it will not work at compile time. 
make it an enum or const and give it a try.


the problem is, PAGESIZE part of D's library so I can't change 
it. I'm looking for a way to make Generators work at runtime 
actually. Or a replacement for it.


It's possible to declare a variable inside a static foreach()?

2017-12-21 Thread Marc via Digitalmars-d-learn
For example, I'd like to declare a variable inside a static 
foreach like in below code, just for better organization, 
otherwise, I have to use the value directly instead of the 
variable. If the value is used more than once, it might be 
inviable.



enum allMembers = __traits(derivedMembers, C);
static foreach(enum string member; allMembers) {
		enum attributes = __traits(getAttributes, __traits(getMember, 
C, member));

static foreach(C c; attributes) {
writeln(c);
}
}


I got redefinition erros of "atributes" on this. Can I have this 
only at compile time?


alias to struct method

2017-12-22 Thread Marc via Digitalmars-d-learn

How can I create a alias to a struct method?


struct S {
 string doSomething(int n) { return ""; }
}


I'd like to do something like this (imaginary code):

alias doSomething = S.doSomething;

then call it by doSomething(3)

I got the following error from this code:


Error: need 'this' for 'gen' of type 'string(int n)'


So I tried create a instance:


alias doSomething = S().doSomething;


Changes the error to:

app.d(96): Error: function declaration without return type. 
(Note that > constructors are always named this)

app.d(96): Error: semicolon expected to close alias declaration


What does scope do as storage class?

2017-12-23 Thread Marc via Digitalmars-d-learn

for example:

scope struct S {
  int x;
}

What does scope do here?


Does to!(string)(char[]) do any memory allocation on conversion?

2017-12-25 Thread Marc via Digitalmars-d-learn
Does to!(string)(char[]) do any memory allocation on conversion 
or is this similar to a cast or what else?


Can I use memoize with a non-static struct method?

2017-12-25 Thread Marc via Digitalmars-d-learn

something like this:


struct S {
  // variables...
  string doGen(int n) { return ""; }
  alias gen = memoize!doGen;
}


The error I got is:


Error: need 'this' for 'doGen' of type 'string(int n)'


I can't make doGen static because it access non-static struct 
members... can I workaround this?


Re: copy only reference rather duplicate a string in appender!string

2017-12-26 Thread Marc via Digitalmars-d-learn
of course a totally different approach to solve this is welcome, 
I came from C/C++/C# worlds so I'm in the process of slowly 
converting my thinking to the D way (which is new for me, since 
I'm even unifamiliar with python and such, which got such 
friendly syntax)


copy only reference rather duplicate a string in appender!string

2017-12-26 Thread Marc via Digitalmars-d-learn
I do build a string by coping large parts of diffrent buffers, 
all those buffers live after the functional call, so rather than 
duplicate those string I'd like to copy only references to those 
parts rather duplicate every string. I combined appender!string, 
assumeUnique() and array slices. Something like this:



auto buffer = appender!string;
auto list = cycle(buffers);
while(bufferIsFilled) {
  char[] buffer = get_buffer(); // pop a buffer from circular 
buffer
  int s = get_buffer_size(x); // determine which part of that 
buffer we need
  buffer.put(assumeUnique(buf[0 .. s])); // and here's my 
question

}
return buffer.data;


How do I set a class member value by its name in a string?

2017-12-27 Thread Marc via Digitalmars-d-learn
I'd like to set the members of a class by its name at runtime, I 
would do something like this:



__traits(getMember, myClass, name) = value;


but since name is only know at runtime, I can't use __traits(). 
What's a workaround for this?


Re: How do I set a class member value by its name in a string?

2017-12-28 Thread Marc via Digitalmars-d-learn

Always helpful. Thank you very much guys.


take symbol as parameter

2017-12-30 Thread Marc via Digitalmars-d-learn

how do I take a symbol as parameter?

for example:


template nameof(alias S) {
import std.array : split;
enum nameof = S.stringof.split(".")[$-1];
}


Works fine for say a enum member such nameof!(myEnum.X) but this:


struct S { int v; }
S s;
writefln(nameof!(s.v)); // should return "v"


return the following error:

Error: template instance nameof!(v) cannot use local 'v' as 
parameter to > non-global template nameof(alias S)




Re: take symbol as parameter

2017-12-31 Thread Marc via Digitalmars-d-learn

On Saturday, 30 December 2017 at 23:30:02 UTC, rjframe wrote:

On Sat, 30 Dec 2017 13:07:49 +, Marc wrote:


how do I take a symbol as parameter?

for example:


template nameof(alias S) {
import std.array : split;
enum nameof = S.stringof.split(".")[$-1];
}


Works fine for say a enum member such nameof!(myEnum.X) but 
this:



struct S { int v; }
S s;
writefln(nameof!(s.v)); // should return "v"


return the following error:

Error: template instance nameof!(v) cannot use local 'v' as 
parameter

to > non-global template nameof(alias S)


You can use the name of the struct rather than the instance.

writefln(nameof!(S.v));


it doesn't work for me:

Error: template instance nameof!(v) cannot use local 'v' as 
parameter to non-global template nameof(alias S)


Re: take symbol as parameter

2017-12-31 Thread Marc via Digitalmars-d-learn

On Sunday, 31 December 2017 at 22:50:12 UTC, Marc wrote:

On Saturday, 30 December 2017 at 23:30:02 UTC, rjframe wrote:

On Sat, 30 Dec 2017 13:07:49 +, Marc wrote:


how do I take a symbol as parameter?

for example:


[...]


Works fine for say a enum member such nameof!(myEnum.X) but 
this:



[...]


return the following error:


[...]


You can use the name of the struct rather than the instance.

writefln(nameof!(S.v));


it doesn't work for me:

Error: template instance nameof!(v) cannot use local 'v' as 
parameter to non-global template nameof(alias S)


Put it at global scope. Worked fine.


Re: take symbol as parameter

2017-12-31 Thread Marc via Digitalmars-d-learn

On Saturday, 30 December 2017 at 23:30:02 UTC, rjframe wrote:

On Sat, 30 Dec 2017 13:07:49 +, Marc wrote:


how do I take a symbol as parameter?

for example:


template nameof(alias S) {
import std.array : split;
enum nameof = S.stringof.split(".")[$-1];
}


Works fine for say a enum member such nameof!(myEnum.X) but 
this:



struct S { int v; }
S s;
writefln(nameof!(s.v)); // should return "v"


return the following error:

Error: template instance nameof!(v) cannot use local 'v' as 
parameter

to > non-global template nameof(alias S)


You can use the name of the struct rather than the instance.

writefln(nameof!(S.v));


How do I make it work when the symbol is defiend as following:


class C { int a() { return 0; }}


call to nameof!(C.a)

give compiler error:


Error: need 'this' for 'a' of type 'int()'
template instance foo.nameof!(a) error instantiating


Does UDA not work for enums?

2018-01-01 Thread Marc via Digitalmars-d-learn

I got compilers errors from this:


enum E {
@("foo")
A,
@("baa")
B
}


I got:


Error: basic type expected, not @
Error: no identifier for declarator _error_
Error: type only allowed if anonymous enum and no enum type
Error: if type, there must be an initializer
Error: found @ when expecting ,

[...]



How do I use ncurses library with D? are there any wrapper?

2018-01-03 Thread Marc via Digitalmars-d-learn
Long time ago, IIRC, I read somewhere there was a ncurses for D 
but now I can't find it are there any wrapper or am I mistaken? 
anyway, I'm doing it from scratch and not porting anything so 
even a library with same functionality as ncurses for D is 
welcome.


How do you do "const nazi" in D?

2018-01-03 Thread Marc via Digitalmars-d-learn
for a safe programming, since C/C++ times I try to make thing 
const as possible. locals, parameters etc anything which isn't 
going to change.

How do you do that in D? immutable everywhere?

for example:


foreach(Field field; fields) {
immutable string htmlOutputfile = genHTMLFilename();


do you use immutable here?

and on:


Field fromFile(in string filename) {


do you use in here if filename isn't going to change?

I'm trying to make my own const nazi guide for D, if anyone would 
like to give your opinion on this, I would be glad.


what's the proper way to convert wchar[] to string?

2018-01-03 Thread Marc via Digitalmars-d-learn
when calling winapi functions, usually you to deal with the 
result in wchar[]. How do I convert it to string in D to be 
usable by the application? does D have a native for this?


Re: what's the proper way to convert wchar[] to string?

2018-01-03 Thread Marc via Digitalmars-d-learn

On Wednesday, 3 January 2018 at 18:59:39 UTC, Ali Çehreli wrote:

On 01/03/2018 10:50 AM, Marc wrote:
when calling winapi functions, usually you to deal with the 
result in wchar[]. How do I convert it to string in D to be 
usable by the application? does D have a native for this?


std.conv has to and text:

auto s0 = w.text;
auto s1 = w.to!string;

Ali


whoa, that simple. I've tried to!string(w) before, but I realized 
the mistake I was passing the whole buffer rather:



string s = w[0 .. wcslen(w.ptr)].to!string;


Thanks.



how do I get only static member of a class?

2018-01-03 Thread Marc via Digitalmars-d-learn
I found no way with __traits() on std.traits. I found 
isStaticFunction and isStaticArray but nothing about a member. Is 
this by desgin?


Give a class like:


class C { static int a, b, c; int d; }


I'd like to get a, b and c.

I'm using this:

__traits(allMembers, C)


Re: Passing a type as paramter to super

2018-01-04 Thread Marc via Digitalmars-d-learn

On Thursday, 4 January 2018 at 19:16:03 UTC, Marc wrote:
For code generation purposes, I'd like to pass a type name to 
base class. I'm not sure if it's supported, I didn't find 
anything at documentation for class constructor but it does 
compile:



class A {
static {
int a, b;
}

this(T)() {

}
}


then do something like this:


class B {
 this() {
   super!B;
 }
}


but I got the error:


found ! when expecting ; following statement


sorry I mean define class b as following:


class B : A {
 this() {
   super!B;
 }
}




Passing a type as paramter to super

2018-01-04 Thread Marc via Digitalmars-d-learn
For code generation purposes, I'd like to pass a type name to 
base class. I'm not sure if it's supported, I didn't find 
anything at documentation for class constructor but it does 
compile:



class A {
static {
int a, b;
}

this(T)() {

}
}


then do something like this:


class B {
 this() {
   super!B;
 }
}


but I got the error:


found ! when expecting ; following statement


Re: how do I get only static member of a class?

2018-01-04 Thread Marc via Digitalmars-d-learn

On Thursday, 4 January 2018 at 00:02:24 UTC, Ali Çehreli wrote:

On 01/03/2018 03:48 PM, Marc wrote:
I found no way with __traits() on std.traits. I found 
isStaticFunction and isStaticArray but nothing about a member. 
Is this by desgin?


Give a class like:


class C { static int a, b, c; int d; }


I'd like to get a, b and c.

I'm using this:

__traits(allMembers, C)



class C { static int a, b, c; int d; }

string[] staticMembers(T)() {
string[] statics;

foreach (m; __traits(derivedMembers, T)) {
import std.traits : hasStaticMember;
static if (hasStaticMember!(T, m)) {
statics ~= m;
}
}

return statics;
}

void main() {
pragma(msg, staticMembers!C);
}

Ali


Thanks again, Ali


what's the scope of a static variable inside a local function?

2018-01-05 Thread Marc via Digitalmars-d-learn

void foo() {
void baa() {
static int n;
writeln(n++);
}
}



void main() {
 static int x;
 foo();
 doSomething(x);
}


does x and n has same lifetime, i.e, program's execution or n is 
longer available onde foo() call reach out of scope?




Why is this valued zeroed?

2018-01-11 Thread Marc via Digitalmars-d-learn
I stuck at this and can't figure out the reason why the value of 
the variable ds is 0 when I do this: startTime = 
MonoTime.currTime; if I remove that statement, the value of ds 
isn't zeroed, it has the actual number of seconds. But I can't 
figure out, ds is of integer type and such, it is copied, right? 
or is this related to the fact I'm setting it withi a callback 
function?


here's the piece of code:

import std.net.curl;
auto http = HTTP(url);
http.method = HTTP.Method.get;

...
the relevant part:


http.onProgress = (size_t dltotal, size_t dlnow,
   size_t ultotal, size_t ulnow) {
if(dlNow > 0) {
MonoTime endTime = MonoTime.currTime;
Duration duration = endTime - startTime;
long ds = duration.total!"seconds";
writeln("duration!seconds  = ", ds);
startTime = MonoTime.currTime;


if I put startTime = MonoTime.currTime, ds is zero, otherwise, if 
I remove it, ds has the actual value.


startTime is first set right before http.perform() call:


startTime = MonoTime.currTime;
http.perform();


(My goal is define the download transfer rate, different 
approachs for this are welcome)


Re: Why is this valued zeroed?

2018-01-12 Thread Marc via Digitalmars-d-learn
On Friday, 12 January 2018 at 05:14:12 UTC, Jonathan M Davis 
wrote:
On Thursday, January 11, 2018 14:07:18 Ali Çehreli via 
Digitalmars-d-learn wrote:

[...]


And if all what you're doing is printing the value out, you 
might as well just print the Duration directly rather than 
calling total. Duration.toString returns the units in a 
human-readable format such as "2 secs, 300 msecs, and 24 μs" or 
"29 msecs".


https://dlang.org/phobos/core_time.html#.Duration.toString

And if you're doing something like adding up the time spent, 
then you'd be better off keeping it in a Duration than 
converting it to long holding seconds or milliseconds or 
whatever.


- Jonathan M Davis


I do use that value as following:

int t = fileSize / countOfByetsDownloaded * 
duration.total!"seconds";


I print that value there for testing only, if I was getting the 
values correctly.


Thank you all guys for the answers.



continue in static foreach

2018-01-12 Thread Marc via Digitalmars-d-learn

How do I use?


static foreach(enum string member; members) {
static if(isFunction!(__traits(getMember, C, m ember))) 
{
continue;
}


give error:


must use labeled continue within static foreach


then I tried:


outer:static foreach(enum string member; members) {
static if(isFunction!(__traits(getMember, C, > 
member))) {
continue outer;
}


give error:


Error: enclosing label outer for continue not found


How do I fix it?


Re: continue in static foreach

2018-01-12 Thread Marc via Digitalmars-d-learn

On Friday, 12 January 2018 at 22:03:53 UTC, H. S. Teoh wrote:
On Fri, Jan 12, 2018 at 10:03:40PM +, Marc via 
Digitalmars-d-learn wrote:

How do I use?

> static foreach(enum string member; members) {
>static if(isFunction!(__traits(getMember, C, member))) {
>continue;
>}

give error:

> must use labeled continue within static foreach

then I tried:

> outer:static foreach(enum string member; members) {
>static if(isFunction!(__traits(getMember, C, member))) {
>continue outer;
>}

give error:

> Error: enclosing label outer for continue not found

How do I fix it?


Unfortunately, this is currently not supported. You'll have to 
use an else-clause to handle the case when the condition is 
false.



T


thanks


How do I get class member type?

2018-01-14 Thread Marc via Digitalmars-d-learn
I didn't find how using traits I could get a class member type? I 
need to test if give class member is not immutable, I find 
isMutable but not how get a type from give class member to pass 
to it.


Re: How do I get class member type?

2018-01-14 Thread Marc via Digitalmars-d-learn

On Sunday, 14 January 2018 at 18:18:50 UTC, Marc wrote:
I didn't find how using traits I could get a class member type? 
I need to test if give class member is not immutable, I find 
isMutable but not how get a type from give class member to pass 
to it.


for clarify, I want all this at compile time, imaginary code 
example:



static foreach(field; FieldNameTuple!C) {
		static if(isFunction!(__traits(getMember, C, field)) &&  
isMutable(typeof(__traits(getMember, C, field {

// do something
}
}


Re: How do I get class member type?

2018-01-14 Thread Marc via Digitalmars-d-learn

On Sunday, 14 January 2018 at 19:08:44 UTC, Marc wrote:

On Sunday, 14 January 2018 at 18:18:50 UTC, Marc wrote:
I didn't find how using traits I could get a class member 
type? I need to test if give class member is not immutable, I 
find isMutable but not how get a type from give class member 
to pass to it.


for clarify, I want all this at compile time, imaginary code 
example:



static foreach(field; FieldNameTuple!C) {
		static if(isFunction!(__traits(getMember, C, field)) &&  
isMutable(typeof(__traits(getMember, C, field {

// do something
}
}


I solved with this:

		enum isMutableString(string field) = 
is(typeof(__traits(getMember, Field, field)) == string);




What's equivalent to C#'s select?

2018-01-14 Thread Marc via Digitalmars-d-learn
give a list, how can I select only the elements of a range 
according to a condition give by a lamba function?


something like this:


auto l = myList.select(e => e.id < 300);


it would return a range. Similar to C#'s select:

https://msdn.microsoft.com/en-us/library/bb548891(v=vs.110).aspx


Re: What's equivalent to C#'s select?

2018-01-14 Thread Marc via Digitalmars-d-learn

On Sunday, 14 January 2018 at 21:59:26 UTC, Seb wrote:

On Sunday, 14 January 2018 at 21:21:52 UTC, Marc wrote:
give a list, how can I select only the elements of a range 
according to a condition give by a lamba function?


something like this:


auto l = myList.select(e => e.id < 300);


it would return a range. Similar to C#'s select:

https://msdn.microsoft.com/en-us/library/bb548891(v=vs.110).aspx


Shameless self-plug - you might like this (incomplete) 
comparison between LINQ and D ranges:


https://github.com/wilzbach/linq


Sounds pretty interesting, I'll give a try! Thanks


Re: What's equivalent to C#'s select?

2018-01-14 Thread Marc via Digitalmars-d-learn

On Sunday, 14 January 2018 at 21:38:39 UTC, drug wrote:

15.01.2018 00:21, Marc пишет:
give a list, how can I select only the elements of a range 
according to a condition give by a lamba function?


something like this:


auto l = myList.select(e => e.id < 300);


it would return a range. Similar to C#'s select:

https://msdn.microsoft.com/en-us/library/bb548891(v=vs.110).aspx

import std.algorithm : filter;

auto l = myList.filter!(e => e.id < 300);


thanks, can i use it at compile time as well?

	enum isMutableString(string field) = 
is(typeof(__traits(getMember, >C, field)) == string);
	static foreach(field; [FieldNameTuple!C].filter!(f => 

isMutableString!(f))) {

writeln(field);
}


give error:


Error: variable f cannot be read at compile time


How do I get the value cache()?

2018-01-14 Thread Marc via Digitalmars-d-learn

let's assume I have


class C {
  static string foo() { writeln("got called!"); // }
}


then I want to cache foo at some point:


import std.algorithm;
auto v = cache(c.foo);


I call do:


for(int i = 0; i <10; i++) {
writeln(v);
}


then it'll print "got called"  only once, which is what I want 
but something obvious is how do I get the returned value as a 
string?
here's why I'm confused more often than I should: I geeeting to 
D's way to do thing and the auto keyword even in documentation 
confused me a bit as I'm used to C++/C# world where the 
struct/class returned is explicity so I just navigate to 
aggregate type's documentation page.




Re: What's equivalent to C#'s select?

2018-01-15 Thread Marc via Digitalmars-d-learn

On Monday, 15 January 2018 at 07:37:42 UTC, Simen Kjærås wrote:

On Sunday, 14 January 2018 at 22:07:22 UTC, Marc wrote:

thanks, can i use it at compile time as well?

	enum isMutableString(string field) = 
is(typeof(__traits(getMember, >C, field)) == string);

static foreach(field; [FieldNameTuple!C].filter!(f =>

isMutableString!(f))) {

writeln(field);
}


You're mixing compile-time and run-time logic here in a way 
that D doesn't allow. In particular, isMutableString requires 
the passed string to be a compile-time constant, and filter 
works on run-time values.


I just thought that filter() could be evaluated at compile time 
too, as others function that I've used so far. Sometimes I don't 
know if a native function can be evaluated at compile time until 
I do enum x = func();


There are a few different ways to resolve this. First, std.meta 
has the Filter template, which behaves much in the same way as 
std.algorithm.filter, but with compile-time tuples:


static foreach (field; Filter!(isMutableString, 
FieldNameTuple!C)) {

writeln(field);
}

The other option is to rewrite isMutableString to work with 
run-time values:


bool isMutableString(string field) {
switch (field) {
foreach (cField; FieldNameTuple!C) {
case cField:
return is(typeof(__traits(getMember, C, 
cField)) == string);

}
default:
return false;
}
}
static foreach(field; [FieldNameTuple!C].filter!(f => 
isMutableString(f))) {

writeln(field);
}

Both of these give the same output, and should be what you want.

--
  Simen


That's exactly it! That Filter() from std.algorithm works like I 
wanted :) nice solution also with rewrite the function to work 
with run-time so that i can use with filter() but if I want to 
have minimum runtime code to filter out immutable strings, the 
first one is better right?


class initialization

2018-01-15 Thread Marc via Digitalmars-d-learn

in C# you can initilizate the class members like this:


var foo = new Foo { a = 1, b = 2 };


I found something similar to structs in D:


myStruct S = {a:1, b:2};


But can't figure out if D does have that for classes.



Web Browser

2018-01-16 Thread Marc via Digitalmars-d-learn
I'm used to use Qt's QWebView when I need an application to show 
a HTML page and change some elements on it. This time, if 
possible, I'd like to use D instead. Are there any web browsers 
ports in D?


Any sample how to use Sqlite-d?

2018-01-17 Thread Marc via Digitalmars-d-learn
I was looking for a library to use SQLite with D, found this 
(https://code.dlang.org/packages/sqlite-d) but it has no 
documentation or code example. I looked into files in the source 
code and wrote this:



Database db = Database(name);
auto table = db.table(tableName);
auto rows = table.findRows!(format!"(id,date) => id == %s"(id));

(i'm aware of sql injection above)

but it doesnt work, it seems the library has changed. If happen 
to that library author see this, would be very helpful.


How do I solve this kind of conflict?

2018-01-21 Thread Marc via Digitalmars-d-learn
I was using a Sqlite3 library then I included another library 
that started the conflict. From what I could tell, it seems it's 
another Sqlite3 engine that the included library uses. The link 
error is:



.dub\build\application-debug-windows-x86-dmd_2076-E7D07B7BDA58325E30A3C637FC043AFE\foo.obj(ytdl)
  Offset BA31FH Record Type 00C3
Error 1: Previous Definition Different : _callback
Error: linker exited with status 1


My guess is it's symbols visibility issue? and how can I solve 
this?


But libraries has this defined:


extern(C) int callback(void*, int, char** , char**){


I've trid mark the one without private as such but it didn't 
change anything.


NOTE: Albeit I'm using dub, both those libraries were included 
directly (except the dependences of one of them).





Re: what's the point of function template declarations if they can't be defined?

2018-02-08 Thread Marc via Digitalmars-d-learn
On Thursday, 8 February 2018 at 07:21:05 UTC, Jonathan M Davis 
wrote:
On Wednesday, February 07, 2018 13:39:55 Timothee Cour via 
Digitalmars-d- learn wrote:

[...]


It's useful with stuff like version(Ddoc).


[...]




What's a di file? (sorry google didn't help with that)
It's been my understanding that it's always been illegal to 
provide a definition for a function that was declared 
previously unless it was declared in a .di file, in which case, 
you're not really both declaring and defining it, but the .d 
file is used when the module is compiled, and the .di file is 
used by other modules which use that module, so the declaration 
and definition are not seen by the same run of the compiler.


- Jonathan M Davis




Re: String to binary conversation

2018-02-08 Thread Marc via Digitalmars-d-learn
On Monday, 5 February 2018 at 18:40:40 UTC, Steven Schveighoffer 
wrote:

On 2/5/18 1:27 PM, Vino wrote:

Hi All,

Request your help on how to convert a string to binary,eg 
"test" to 01110100 01100101 01110011 01110100.


import std.stdio, std.string;
writefln("%(%b %)", "test".representation);

-Steve


whoa, what isn't built-in in D...


How can I get the new stdout which changes periodically from a process?

2018-02-10 Thread Marc via Digitalmars-d-learn
I do call a program from my application which changes its stdout 
perdiodically (with ncurses library, I guess), where I'd to get 
somehow "notified" when some change happen (I need to new data 
from that changes and change my application accordingly). 
Currently, I do use spawnProcess() which runs the app in paralel 
but I have no idea how to capture periodically changes in its 
stdout.
It doesn't need to be exact; like a notification for every single 
change. If it checks for changes and report if there's any, every 
say, x seconds is enough.




How does this error from dub mean "dlang Non-selected Y package is available with version X"?

2018-02-12 Thread Marc via Digitalmars-d-learn

the warning is:


Non-selected package lnk is available with version ~>0.2.1.


What does it mean by *Non-selected* package lnk is available?
from what I could tell from the page, it's highest version.
But I've tried low versions anyway to see if it Works 0.2.0, 
0.1.1 etc and none did.


I noticied I put "lnk": "~>0.2.1" on cachedUpgrades section so I 
moved there to just created "dependencies" section then the dub's 
error is gone but it fail to find the lnk library:



Error: module lnk is in file 'lnk.d' which cannot be read


some info:

The package I'm trying to include is: 
https://code.dlang.org/packages/lnk

DUB version 1.5.0, built on Sep  1 2017
DMD32 D Compiler v2.076.0
Windows 10/64bit

What am I missing? I'm new to dub so everything is new to me, by 
now.


Re: How does this error from dub mean "dlang Non-selected Y package is available with version X"?

2018-02-12 Thread Marc via Digitalmars-d-learn

On Monday, 12 February 2018 at 15:51:58 UTC, Marc wrote:

the warning is:


Non-selected package lnk is available with version ~>0.2.1.


What does it mean by *Non-selected* package lnk is available?
from what I could tell from the page, it's highest version.
But I've tried low versions anyway to see if it Works 0.2.0, 
0.1.1 etc and none did.


I noticied I put "lnk": "~>0.2.1" on cachedUpgrades section so 
I moved there to just created "dependencies" section then the 
dub's error is gone but it fail to find the lnk library:



Error: module lnk is in file 'lnk.d' which cannot be read


some info:

The package I'm trying to include is: 
https://code.dlang.org/packages/lnk

DUB version 1.5.0, built on Sep  1 2017
DMD32 D Compiler v2.076.0
Windows 10/64bit

What am I missing? I'm new to dub so everything is new to me, 
by now.


so I find the issue. I'm ashamed to say: I was editing the 
dub.json inside the dub folder not the one in the my 
application's root.





print enum value rather name from enum X : string

2018-02-12 Thread Marc via Digitalmars-d-learn

If I have an enum like this:


enum S  : string {
foo = "a",
baa = "b"
}


when I printed it, to my surprise I get the enum field name 
rather value:



writefln("%s v%s", S.foo, S.baa); 


output:


foo vbaa


instead of


a vb


a cast solves it but without cast everywhere I need that enum 
member, is there any other way to do it? otherwise I'll have to 
switch to a class with static immutable strings, right?




What should I use for concat string into array in loop?

2018-02-12 Thread Marc via Digitalmars-d-learn

appender doesn't support string[] so in such case:


string[] output;
for(...) {
   if(...) {
 output ~= str;
}
}


Looking for avoid as many immediate allocations as possible, what 
should I use?


Re: print enum value rather name from enum X : string

2018-02-12 Thread Marc via Digitalmars-d-learn
On Monday, 12 February 2018 at 17:29:47 UTC, Jonathan M Davis 
wrote:
On Monday, February 12, 2018 17:07:50 Marc via 
Digitalmars-d-learn wrote:

[...]


If you actually use the enum values anywhere other than with 
anything from std.conv, std.format, or std.stdio, then when 
they get converted to strings, you get their actual values. 
It's just that those modules specifically grab the names of the 
enum members when converting enums to strings, since in all 
cases other than with strings, it's generally desirable that 
when converting an enum member to string, you get the name - 
and with enums with a base type of string, whether you want the 
name or the value depends on what you're trying to do. Both can 
be useful.


So, when dealing with std.format, std.conv, and std.stdio, if 
you want an enum of base type string to be treated as a string, 
then you'll have to force it with a cast. Anywhere else, if 
they get converted to string, then you'll get their values. If 
that is unacceptable for your use case for whatever reason, 
then you'll have to try a different solution. What solution 
would then work best would presumably depend on whatever it is 
you're actually trying to do.


- Jonathan M Davis


Thanks for you always well-thought-out answer. I was going to 
print it with writefln() calls more than anywhere else so to 
avoid casts in all those places, which would make it ugly, I just 
used



enum foo = "a";
enum baa = "b";


which I found to be more common D-idiomatic.


Could that bug be catch using D's way?

2018-02-19 Thread Marc via Digitalmars-d-learn
I'm pretty sure something could be done with Ada's type range but 
what we could do using D?


How do I trace that memory error?

2018-02-26 Thread Marc via Digitalmars-d-learn
I've tried both gdb and windbg debugger both it either get a 
"received signal ?" from gdb or crash the GUI application 
(windbg).

The error is:

core.exception.OutOfMemoryError@src\core\exception.d(696): 
Memory allocation failed


How do I find out the source of the error?


Re: How do I trace that memory error?

2018-02-26 Thread Marc via Digitalmars-d-learn

On Monday, 26 February 2018 at 18:01:07 UTC, Marc wrote:
I've tried both gdb and windbg debugger both it either get a 
"received signal ?" from gdb or crash the GUI application 
(windbg).

The error is:

core.exception.OutOfMemoryError@src\core\exception.d(696): 
Memory allocation failed


How do I find out the source of the error?


I'm allocation a class and returning it in a yield. Can it be 
related?


Something like this:


Generator!Field fromCSVFile(in string csvFilename) {
return new Generator!Field({
string line;
while((line = csvFile.readln()) !is null) {
Field field = deserializeLine(line);
yield(field);
writeln("");
}
});
}


Re: How do I trace that memory error?

2018-02-27 Thread Marc via Digitalmars-d-learn
On Tuesday, 27 February 2018 at 14:06:19 UTC, Nicholas Wilson 
wrote:

On Monday, 26 February 2018 at 18:01:07 UTC, Marc wrote:
I've tried both gdb and windbg debugger both it either get a 
"received signal ?" from gdb or crash the GUI application 
(windbg).

The error is:

core.exception.OutOfMemoryError@src\core\exception.d(696): 
Memory allocation failed


How do I find out the source of the error?


"received signal ?" seems like the one of the GC signals used 
for stopping threads.

just get gdb to ignore those.


An attempt to continue results in crash.

I also added in my foreach's body:


scope(exit) {
import core.memory : GC;
GC.collect();
   }


But also doesn't solve the issue.


Re: How do I trace that memory error?

2018-02-27 Thread Marc via Digitalmars-d-learn

On Tuesday, 27 February 2018 at 15:08:23 UTC, Stefan Koch wrote:

On Monday, 26 February 2018 at 18:01:07 UTC, Marc wrote:
I've tried both gdb and windbg debugger both it either get a 
"received signal ?" from gdb or crash the GUI application 
(windbg).

The error is:

core.exception.OutOfMemoryError@src\core\exception.d(696): 
Memory allocation failed


How do I find out the source of the error?


I'd say allocating in a loop is a bad idea :)

perhaps you should start with posting the code that leads to 
this.


The code is that one on my second post. The deserializeLine() 
create allocate instance of Field then return. So yeah, I'm 
allocating on loop but I don't doing too much. I was watching the 
memory usage of the application (with win10 taskbar memory usage 
feature) and I saw it uses only about 4mb on 8gb machine. I've 
tried build a 64bit executable but I got same error.


Re: How do I trace that memory error?

2018-02-27 Thread Marc via Digitalmars-d-learn

On Tuesday, 27 February 2018 at 15:08:23 UTC, Stefan Koch wrote:

On Monday, 26 February 2018 at 18:01:07 UTC, Marc wrote:
I've tried both gdb and windbg debugger both it either get a 
"received signal ?" from gdb or crash the GUI application 
(windbg).

The error is:

core.exception.OutOfMemoryError@src\core\exception.d(696): 
Memory allocation failed


How do I find out the source of the error?


I'd say allocating in a loop is a bad idea :)

perhaps you should start with posting the code that leads to 
this.


I've also tried to create only one instance: create a local 
variable inside the method then reuse it. I get same error. No 
debugg tool has helped so far.




Re: How do I trace that memory error?

2018-02-27 Thread Marc via Digitalmars-d-learn

On Tuesday, 27 February 2018 at 16:55:27 UTC, Marc wrote:

[...]


So deep down the error is in that method which I call from 
deserializeLine() function:




		void setValue(T, V)(auto ref T aggregate, string field, V 
value) {

writeln("setting {", field, "} to {", value, "}");
import std.traits : FieldNameTuple;
import std.meta : Alias;
switch (field) {
foreach (fieldName; FieldNameTuple!T) {
case fieldName:
		static if (is(typeof(__traits(getMember, 
aggregate, fieldName) = value))) {
		__traits(getMember, aggregate, fieldName) 
= value;

return;
} else {
			assert(false, T.stringof ~ "."~field~" cannot 
be assigned from a "~V.stringof~".");

}
}
default:
		assert(false, T.stringof ~ " has no field named 
"~field~".");

}
}



the function is used like this:


Field field = new Field();
foreach(CSVLinkedIndex linkedIndex; linkedIndexes) {
string value = 
values[linkedIndex.csv_column_index];
setValue(field, linkedIndex.field_member, 
value);
}


which eventually (in about 8 calls) return the error:

core.exception.OutOfMemoryError@src\core\exception.d(702): 
Memory allocation failed


it uses about 4MB in a 8GB machine then crashs. It's return by 
the top function using yield:



yield(field);


But I can't find out why gc fails to allocate at some point. In 
fact, I've forced the gc to do so in my top loop:



foreach(Field field; deserializeFile()) {
scope(exit) {
import core.memory : GC;
   GC.collect();
}
}


Which didn't solve the issue.


What's the proper way to add a local file dependence to dub?

2018-03-04 Thread Marc via Digitalmars-d-learn

then copy it to sources folder?

let's say I have a small library folder at C:\mylibrary\D where I 
want to use dir.d from it. How do I add that file dependence to 
dub? But I do not want to that file be passed directly to dmd, I 
want to that file be copied to application's source folder (so 
it's easy to distribuite, with the dependences together as 
possible) then compiled. So, what I want to some extension is dub 
work with loca files.
Is this possible to do solely with dub? I know I can easily write 
a script to run before dub which copies the dependence files from 
C:\mylibrary to application's source but I'm looking for a more 
elegant
 approach as possible; I'm afraid of rewriting a makefile-like 
soon (I find cmake/make/makefiles just ugly).




Any crash reporter for D?

2018-03-04 Thread Marc via Digitalmars-d-learn
Does anyone knows about any crash repórter library for D? there's 
nothing I couldn't find at code packages. Something as simple and 
easy to use like this C#'s 
https://archive.codeplex.com/?p=crashreporterdotnet


Re: Speed of math function atan: comparison D and C++

2018-03-05 Thread Marc via Digitalmars-d-learn

On Monday, 5 March 2018 at 05:35:28 UTC, J-S Caux wrote:
I'm considering shifting a large existing C++ codebase into D 
(it's a scientific code making much use of functions like atan, 
log etc).


I've compared the raw speed of atan between C++ (Apple LLVM 
version 7.3.0 (clang-703.0.29)) and D (dmd v2.079.0, also ldc2 
1.7.0) by doing long loops of such functions.


I can't get the D to run faster than about half the speed of 
C++.


Are there benchmarks for such scientific functions published 
somewhere?


What compiled flags did you used to compile both C++ and D 
versions?


Re: How to use globals correctly?

2018-03-05 Thread Marc via Digitalmars-d-learn
On Monday, 5 March 2018 at 18:57:01 UTC, Steven Schveighoffer 
wrote:

On 3/5/18 1:35 PM, Robert M. Münch wrote:

If I use VisualD and add a watch on myObj, I don't see 
anything just a "identifier myObj is undefined". Not sure if 
this is because of some threads running (using the D RX 
framework).


Can't answer your visual D questions...



So, some questions:

1. Are myMemb1..N TLS or __gshared as well?


No, they are on the heap. Only the reference is __gshared. But 
effectively it is __gshared, since you can reach those items 
via the global `myObj`.


2. How to best implement a simple global to keep track of 
values (with support for threads)?


shared is the best mechanism, and you don't have to make it a 
class, it can be a struct.


This is only if you are using it as POD (plain old data). If 
you want to have methods, shared kind of sucks.


But this at least tells the type system that it's shared 
between threads. __gshared does not, it just sticks it in 
global space, but pretends it's not shared data.


-Steve


Can __gshared be used instead of static in the singleton pattern? 
I, comming from C++, ignorantly, have never used _gshared so I 
went to static instead of (being static also means thread-safe, 
as far I know)...


Are files in dub's "copyFiles" copied every time dub is run?

2018-03-05 Thread Marc via Digitalmars-d-learn

if so, can I somehow make it copy only if newest?


dub preBuildCommand not working

2018-03-06 Thread Marc via Digitalmars-d-learn

I added the followiing to my dub.json:


"preBuildCommand": [
"C:\\Users\\user003\\Desktop\\Nova pasta\\a.exe",
"C:\\dm\\bin\\rcc.exe -32 -D__NT__ res.rc -o sources\\res.res"
]


But neither command is executed. 
"C:\\Users\\user003\\Desktop\\Nova pasta\\a.exe" is a D 
application created by myself to check if that command is being 
run. It creates a text file when it does run.

What possibily can be?
I've tried:


dub
dub --build=debug
dub --build-release

and

dub clear
then all the build attempts again but none worked. I getting to 
know dub so I still have no idea what can be.




Setting executable's information by resources

2018-03-06 Thread Marc via Digitalmars-d-learn
I'm trying to use this resource to set executable's 
information/metadata but it fail to set anything but the icon, 
without any error message. Could anyone point out if there's 
anything wrong with my approach?
(if anyone use a different approach/toolset to set those data, 
your suggestion is very welcome)


commands:


C:\dm\bin\rcc.exe -32 -D__NT__ res.rc
dmd app.d res.res


resource file


IDI_ICON1   ICONDISCARDABLE "ico.ico"
#include "version.h"
VS_VERSION_INFO VERSIONINFO
FILEVERSION VER_FILEVERSION
PRODUCTVERSION  VER_PRODUCTVERSION
BEGIN
  BLOCK "StringFileInfo"
  BEGIN
  BLOCK "040904E4"
  BEGIN
  VALUE "CompanyName",VER_COMPANYNAME_STR
  VALUE "FileDescription",VER_FILEDESCRIPTION_STR
  VALUE "FileVersion",VER_FILEVERSION_STR
  VALUE "InternalName",   VER_INTERNALNAME_STR
  VALUE "LegalCopyright", VER_LEGALCOPYRIGHT_STR
  VALUE "LegalTrademarks1",   VER_LEGALTRADEMARKS1_STR
  VALUE "LegalTrademarks2",   VER_LEGALTRADEMARKS2_STR
  VALUE "OriginalFilename",   VER_ORIGINALFILENAME_STR
  VALUE "ProductName",VER_PRODUCTNAME_STR
  VALUE "ProductVersion", VER_PRODUCTVERSION_STR
  END
  END
  BLOCK "VarFileInfo"
  BEGIN
  VALUE "Translation", 0x409, 1252
  END
END




How can I get notified when an process created by spawnShell() has exit?

2018-03-07 Thread Marc via Digitalmars-d-learn
I do need to start (up to 4 a time) processes in parallel but I'd 
like to get notified (similar to C#'s Process.Exited Event) when 
the process exits. How can I do that in D?




Re: issue with inf from exp function

2018-03-07 Thread Marc via Digitalmars-d-learn

On Wednesday, 7 March 2018 at 16:06:26 UTC, Matt Gamble wrote:

On Wednesday, 7 March 2018 at 16:00:39 UTC, Alex wrote:

On Wednesday, 7 March 2018 at 15:44:28 UTC, Matt Gamble wrote:

[...]


works for me as expected.

ln(largest double) = 709.783
e^710 = inf
ln(largest real) = 11356.5
e^710 = 2.23399e+308


Really? Is it my system?
I'm running windows 10, with an intel Core i7-4650, 64bit 
operating system

I compiled with -m64 -g -unittest -main


Did notice you were using -m64 flag before. Well, now I used -m64 
the same code doesn't work as expected. I also got inf.


Re: issue with inf from exp function

2018-03-07 Thread Marc via Digitalmars-d-learn

On Wednesday, 7 March 2018 at 16:00:39 UTC, Alex wrote:

On Wednesday, 7 March 2018 at 15:44:28 UTC, Matt Gamble wrote:
I don't understand why I'm getting an 'inf' by raising E to a 
real number, e^^710.0L. See below.


import std.stdio;
import std.math;

unittest
{
	writefln("ln(largest double) = %s", log(double.max)); // 
709.783

writefln("e^710 = %s", exp(710.0));// inf, makes sense

writefln("ln(largest real) = %s", log(real.max)); // 11356.6
real t = 710.0;
writefln("e^710 = %s", exp(t)); //why is this inf???
}

Any help would be greatly appreciated. I have functions in an 
actual program that are giving me a headache due to this issue.


works for me as expected.

ln(largest double) = 709.783
e^710 = inf
ln(largest real) = 11356.5
e^710 = 2.23399e+308


it works for me too.

https://imgur.com/bLGTv1l



Re: issue with inf from exp function

2018-03-07 Thread Marc via Digitalmars-d-learn

On Wednesday, 7 March 2018 at 16:12:28 UTC, Matt Gamble wrote:

On Wednesday, 7 March 2018 at 16:10:15 UTC, Marc wrote:

On Wednesday, 7 March 2018 at 16:00:39 UTC, Alex wrote:

On Wednesday, 7 March 2018 at 15:44:28 UTC, Matt Gamble wrote:

[...]


works for me as expected.

ln(largest double) = 709.783
e^710 = inf
ln(largest real) = 11356.5
e^710 = 2.23399e+308


it works for me too.

https://imgur.com/bLGTv1l


Wow, Now I'm really confused, I'm using DMD v2.076.1. Any 
thoughts I why I'm getting this result?


See my previous post. It's something related to generating 64bit 
code.




how do I pass this callback?

2018-03-08 Thread Marc via Digitalmars-d-learn
How do I define the callback so that it can be used in 
RegisterWaitForSingleObject()?


I've tried pass as argument:

myFunc
&myFunc
myFunc.ptr

none worked. Here's my code:


extern (C) void OnExited(void* context, BOOLEAN isTimeOut);

extern(Windows):
BOOL RegisterWaitForSingleObject(
  PHANDLE phNewWaitObject,
  HANDLE  hObject,
  WAITORTIMERCALLBACK Callback,
  PVOID   Context,
  ULONG   dwMilliseconds,
  ULONG   dwFlags
);


Function call:

RegisterWaitForSingleObject(&hWait, hProcess, &OnExited, NULL, 
INFINITE, WT_EXECUTEONLYONCE);


Error:

function a.RegisterWaitForSingleObject(void** phNewWaitObject, 
void* hObject, extern (Windows) void function(void*, bool) 
Callback, void* Context, uint dwMilliseconds, uint dwFlags) is 
not callable using argument types (void**, void*, extern (C) 
void function(void* context, bool isTimeOut), typeof(null), 
uint, uint)
a.d(38):cannot pass argument & OnExited of type extern 
(C) void function(void* context, bool isTimeOut) to parameter 
extern (Windows) void function(void*, bool) Callback




Re: how do I pass this callback?

2018-03-08 Thread Marc via Digitalmars-d-learn

On Thursday, 8 March 2018 at 17:06:05 UTC, Marc wrote:
How do I define the callback so that it can be used in 
RegisterWaitForSingleObject()?


I've tried pass as argument:

myFunc
&myFunc
myFunc.ptr

none worked. Here's my code:


[...]


Function call:


[...]


Error:


[...]


Solved! I shall rather use extern (Windows) not extern(C):


extern (Windows) void OnExited(void* context, BOOLEAN isTimeOut);


overload binary + operator to work with different types

2018-03-13 Thread Marc via Digitalmars-d-learn

I want to basically make this work:


auto l = new List();
l += 5;


I managed to do this:


class List
{
int[] items;
ref List opBinary(string op)(int rhs) if(op == "+")
{
items ~= rhs;
return *this;
}

}

Note the ref in the fucntion return, I also want to return a 
reference to the class so that this Works:



l += 5 + 8 + 9 ... ;


Could someone point out how do that/what's wrong with my attempy?


Re: overload binary + operator to work with different types

2018-03-13 Thread Marc via Digitalmars-d-learn

On Tuesday, 13 March 2018 at 19:05:00 UTC, Jonathan M Davis wrote:
On Tuesday, March 13, 2018 18:55:35 Marc via 
Digitalmars-d-learn wrote:

I want to basically make this work:
>[...]

I managed to do this:
> [...]

}

Note the ref in the fucntion return, I also want to return a

reference to the class so that this Works:
> [...]

Could someone point out how do that/what's wrong with my 
attempy?


+ is overloaded with opBinary, and += is overloaded using 
opOpAssign:


https://dlang.org/spec/operatoroverloading.html#op-assign 
http://ddili.org/ders/d.en/operator_overloading.html


- Jonathan M Davis


Soon as I posted I found out opOpAssign.
I've had got close than I thought:


List opOpAssign(string op)(int rhs) if(op == "+")
{
items ~= rhs;
return this;
}


Thanks!


Why does file order matters when using -run option?

2018-03-14 Thread Marc via Digitalmars-d-learn

assume the files:

app.d


void main() {
  import myModule : foo;
   writeln(foo(...));

}


myModule.d

module myModule;

int foo(int n) {  }


the following fail:


dmd -run app.d mymodule.d


give error like this:

Error: module `myModule` is in file 'myModule.d' which cannot 
be read


but this doesn't fail:


dmd app.d mymodule.d && app


Why does -run fail here? I thought it was a shorthand to this 
batch:



dmd app.d mymodule.d
app.exe
del app.exe




How do I make only the array itself immutable?

2018-03-14 Thread Marc via Digitalmars-d-learn

Can I make it work?


struct S
{
int[] l;
}


then


auto s = S();
s.l ~= 1; // ok
s.l = []; // error


Re: Mir blas keeps giving an error

2025-08-12 Thread Marc via Digitalmars-d-learn
I have updated the examples by adding a subconfiguration of 
mi-blas. Now it works.


Re: Mir blas keeps giving an error

2025-08-12 Thread Marc via Digitalmars-d-learn

On Tuesday, 12 August 2025 at 07:13:58 UTC, Serg Gini wrote:

BLAS can be a tricky-to-install library.
Please provide your OS, dub.json/sdl and check that BLAS is 
installed.


The examples dub.sdl is available here: 
https://github.com/istmarc/mir-examples/blob/main/dub.sdl . I've 
Archlinux installed with openblas (`/usr/lib/libopenblas.so`) and 
lapack (`/usr/lib/liblapack.so`). I can use them from c++ simply 
by linking `clang++ -lbopenblas main.cxx` and both work fine.


Re: How to plot a simple scatter plot using ggplotd

2025-08-12 Thread Marc via Digitalmars-d-learn

On Tuesday, 12 August 2025 at 07:18:45 UTC, Serg Gini wrote:

```d
auto gg = xs.zip(ys)
.map!((t) => aes!("x","y")(t[0], t[1]))
.geomPoint.putIn(GGPlotD());
```

Some examples can be found here:
https://blackedder.github.io/ggplotd/example.html
https://blackedder.github.io/ggplotd/ggplotd/ggplotd/GGPlotD.html


Thanks for your reply. Finally I got it to work. The code is the 
following

```d
import std.stdio;
import std.array;

import mir.ndslice;

import ggplotd.aes: aes, Aes;
import ggplotd.axes: xaxisLabel, yaxisLabel;
import ggplotd.ggplotd: GGPlotD, putIn;
import ggplotd.geom: geomPoint, geomBox, geomLine;

void main()
{
	auto x = [1.47f, 1.50f, 1.52f, 1.55f, 1.57f, 1.60f, 1.63f, 
1.65f, 1.68f, 1.70f, 1.73f, 1.75f, 1.78f, 1.80f, 
1.83f].sliced(15);
	auto y = [52.21f, 53.12f, 54.48f, 55.84f, 57.20f, 58.57f, 
59.93f, 61.29f, 63.11f, 64.47f, 66.28f, 68.10f, 69.92f, 72.19f, 
74.46f].sliced(15);


auto xs = x.array;
auto ys = y.array;

auto a = Aes!(float[], "x", float[], "y")(xs, ys);
auto gg = geomPoint(a).putIn(GGPlotD());
gg.save("scatter.png");
}
```


Re: Mir blas keeps giving an error

2025-08-12 Thread Marc via Digitalmars-d-learn

On Tuesday, 12 August 2025 at 10:05:59 UTC, Serg Gini wrote:

On Tuesday, 12 August 2025 at 09:44:07 UTC,

Nice.
I've checked your repos - you probably also could be interested 
in projects:
- Mojo lang, where tensors are integrated into the language 
https://docs.modular.com/max/api/c/tensor/

- GGML C++ library for tensors, used in llama.cpp
https://github.com/ggml-org/ggml


Those are great resources. Thanks a lot for your reply. I started 
writting a tensor library mainly to extend Eigen c++ library that 
has an unsupported tensor module. It’s great for numerical stuffs 
but doesn’t support for example slicing like numpy and Mir except 
for rows and columns of a matrix. Im not sure about how they 
approached this so that might be very interesting thing I could 
check.




How to plot a simple scatter plot using ggplotd

2025-08-11 Thread Marc via Digitalmars-d-learn

Hello everyone,
I am trying to use ggplotd to visualize a data set before and 
after fitting a linear model. how ever i'm struggling to get it 
to work with a simple example. I tried the following and i get 
errors for boths:


```d
import mir.ndslice;
import ggplotd.aes: aes, Aes;
import ggplotd.axes: xaxisLabel, yaxisLabel;
import ggplotd.ggplotd: GGPlotD, putIn;
import ggplotd.geom: geomPoint, geomBox, geomLine;

void main(){
   auto x = [1.47f, 1.50f, 1.52f].sliced(3);
   auto y = [52.21f, 53.12f, 54.48f].sliced(3);
   auto xs = x.array;
   auto ys = y.array;
   auto gg = aes!("x", "y")(xs, ys).geomPoint;
   // or
   auto gs = aes!("x", "y")(xs, ys).array.geomPoint;
}
```
I would appreciate any help or advice on getting it to work.


Mir blas keeps giving an error

2025-08-11 Thread Marc via Digitalmars-d-learn
Using mir-blas is fundamental for implementing linear models in 
D. However it keeps giving an error, other mir libraries work 
fine.


I get the following error

```shell
/usr/bin/ld: -f may not be used without -shared
clang: error: linker command failed with exit code 1 (use -v to 
see invocation)

Error: linker exited with status 1
```

I would appreciate if anyone can help me setting it up.


Re: Template instance does not match template declaration

2025-08-15 Thread Marc via Digitalmars-d-learn

On Friday, 15 August 2025 at 12:20:16 UTC, Sergey wrote:

On Friday, 15 August 2025 at 12:02:19 UTC, Marc wrote:

Hello,
I'm trying to declare a templated member function that takes a 
value of size_t N. A simple example to reproduce what Im 
trying to do is the following:


```d
import std.stdio;
void getCol(N: size_t)() {
   return N;
}

void main() {
   // Call
   writeln(getCol!0());
}
```
I get the following error: `Error: template instance 
`getCol!0` does not match template declaration `getCol(N : 
ulong)()`

   writeln(getCol!0());
   ^
Error dmd failed with exit code 1.`

Can anyone please help me getting it to work?


D doesn't use ":" for types
```d
import std;

size_t getCol(size_t N)() {
   return N;
}

void main() {
   // Call
   writeln(getCol!0());
}
```


Thanks for your reply. It solved my problem.


Re: Template instance does not match template declaration

2025-08-15 Thread Marc via Digitalmars-d-learn

On Friday, 15 August 2025 at 12:18:30 UTC, 0xEAB wrote:

On Friday, 15 August 2025 at 12:02:19 UTC, Marc wrote:

Can anyone please help me getting it to work?


It seems like you’ve accidentally used the wrong syntax for the 
template parameter.

To make the parameter a value parameter of type `size_t`, write:

```
size_t getCol(size_t N)() {
   return N;
}
```

Also, your return type was off. Can’t return data from a `void` 
function.


Thanks. It work now.


Template instance does not match template declaration

2025-08-15 Thread Marc via Digitalmars-d-learn

Hello,
I'm trying to declare a templated member function that takes a 
value of size_t N. A simple example to reproduce what Im trying 
to do is the following:


```d
import std.stdio;
void getCol(N: size_t)() {
   return N;
}

void main() {
   // Call
   writeln(getCol!0());
}
```
I get the following error: `Error: template instance `getCol!0` 
does not match template declaration `getCol(N : ulong)()`

   writeln(getCol!0());
   ^
Error dmd failed with exit code 1.`

Can anyone please help me getting it to work?


Re: Typed data frames in D

2025-08-17 Thread Marc via Digitalmars-d-learn

On Saturday, 16 August 2025 at 20:51:35 UTC, Dejan Lekic wrote:
The only one I know is this one: 
https://github.com/navid-m/parallax


This one looks good overall.


Typed data frames in D

2025-08-16 Thread Marc via Digitalmars-d-learn

Hi,

I'm trying to implement data frames in D based on mir slices. I'm 
not sure if there's a better approach. Currently support just 
basic slicing and assignment.


Are there any efforts in the community toward this goal.

What features would you like to see in the data frame? Any ideas 
or feature request would be appreciated.


https://github.com/istmarc/typeddataframe



  1   2   >