Re: Is std.variant.visit not @nogc?

2018-04-09 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 10 April 2018 at 00:22:18 UTC, helxi wrote:

On Monday, 9 April 2018 at 15:59:32 UTC, Paul Backus wrote:

On Monday, 9 April 2018 at 07:07:58 UTC, Chris Katko wrote:

[...]


I agree in general, but in this case it's actually completely 
doable. In fact, I've done it myself: check out 'sumtype' on 
code.dlang.org. You can replace 'Algebraic' with 'SumType' and 
'visit' with 'match' in helxi's example, and everything Just 
Works™:


[...]


This isn't boxed by any chance, is it?


Nope! It's just a tagged union, almost exactly the same as what 
you'd write by hand in C. You can take a look at the source 
yourself, if you're curious---it's actually pretty simple:


https://github.com/pbackus/sumtype/blob/master/src/sumtype.d#L27


Re: Should the "front" range primitive be "const" ?

2018-04-09 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, April 09, 2018 23:58:02 Drone1h via Digitalmars-d-learn wrote:
> On Monday, 19 March 2018 at 00:50:06 UTC, Jonathan M Davis wrote:
> > [...]
> > http://jmdavisprog.com/articles/why-const-sucks.html
>
> I have just read the reply and the article.
>
> I cannot believe you have written this article in response to
> this thread (perhaps I am mis-interpreting the date of the
> article). But even if not so, thank you for clarifications and
> for describing root causes.

I did not write the article in response to any thread. I'd been meaning to
write it for a while. It's just that it's relevant to this thread, and
pointing to the article rather than trying to explain everything that's in
there again saves me time.

- Jonathan M Davis



Re: Strange Thread Causing Duplicating `writeln`

2018-04-09 Thread Jonathan via Digitalmars-d-learn

On Monday, 9 April 2018 at 22:56:33 UTC, Jonathan wrote:

On Monday, 9 April 2018 at 22:53:31 UTC, Jonathan wrote:

On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote:
I don't know, but I can't reproduce either with dmd or ldc. 
What was your compilation line?


dmd -run file.d


I am on Window 10 btw.


Hum, LDC does not do it for me?


Re: Is std.variant.visit not @nogc?

2018-04-09 Thread helxi via Digitalmars-d-learn

On Monday, 9 April 2018 at 15:59:32 UTC, Paul Backus wrote:

On Monday, 9 April 2018 at 07:07:58 UTC, Chris Katko wrote:

[...]


I agree in general, but in this case it's actually completely 
doable. In fact, I've done it myself: check out 'sumtype' on 
code.dlang.org. You can replace 'Algebraic' with 'SumType' and 
'visit' with 'match' in helxi's example, and everything Just 
Works™:


[...]


This isn't boxed by any chance, is it?


Re: Should the "front" range primitive be "const" ?

2018-04-09 Thread Drone1h via Digitalmars-d-learn

On Monday, 19 March 2018 at 00:50:06 UTC, Jonathan M Davis wrote:


[...]
http://jmdavisprog.com/articles/why-const-sucks.html


I have just read the reply and the article.

I cannot believe you have written this article in response to 
this thread (perhaps I am mis-interpreting the date of the 
article). But even if not so, thank you for clarifications and 
for describing root causes.


I will have to read it again, and then read the other replies to 
my question, because I am just beginning to understand what 
"head-const" and "tail-const" mean and to get an idea of the 
problems.


By this reply, I just wish to let you all know that your answers 
are really appreciated and that I believe they are very helpful 
for many programmers, even if they need time to understand them. 
Thank you Jonathan. Thank you all.


Re: Strange Thread Causing Duplicating `writeln`

2018-04-09 Thread Jonathan via Digitalmars-d-learn

On Monday, 9 April 2018 at 22:53:31 UTC, Jonathan wrote:

On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote:
I don't know, but I can't reproduce either with dmd or ldc. 
What was your compilation line?


dmd -run file.d


I am on Window 10 btw.


Re: Strange Thread Causing Duplicating `writeln`

2018-04-09 Thread Jonathan via Digitalmars-d-learn

On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote:
I don't know, but I can't reproduce either with dmd or ldc. 
What was your compilation line?


dmd -run file.d


Re: Strange Thread Causing Duplicating `writeln`

2018-04-09 Thread Cym13 via Digitalmars-d-learn

On Monday, 9 April 2018 at 22:28:45 UTC, Jonathan wrote:

I am totally lost on why this is happening.

I stripped the code down to what appears to be the most minimal 
code that still causes the problem.


---
import core.sync.mutex;
import core.thread;
import std.stdio;

__gshared Mutex m;//__gshared just for testing (;

void thread1() {
foreach (i;0..8) {
synchronized(m) {
writeln("a1-",i);
}
writeln("a2-",i);
}
}
void thread2() {
foreach (i;0..8) {
synchronized(m) {
writeln("b1-",i);
}
writeln("b2-",i);
}
}


void main() {
m = new Mutex();

new Thread().start;
new Thread().start;
}
---
The beginning of the output for this code is:
a1-0
a2-0
a2-0
b1-0
b2-0
b2-0
a1-1
a2-1
a2-1

Why is the "a2" and "b2" writeln being repeated?!


I don't know, but I can't reproduce either with dmd or ldc. What 
was your compilation line?


Strange Thread Causing Duplicating `writeln`

2018-04-09 Thread Jonathan via Digitalmars-d-learn

I am totally lost on why this is happening.

I stripped the code down to what appears to be the most minimal 
code that still causes the problem.


---
import core.sync.mutex;
import core.thread;
import std.stdio;

__gshared Mutex m;//__gshared just for testing (;

void thread1() {
foreach (i;0..8) {
synchronized(m) {
writeln("a1-",i);
}
writeln("a2-",i);
}
}
void thread2() {
foreach (i;0..8) {
synchronized(m) {
writeln("b1-",i);
}
writeln("b2-",i);
}
}


void main() {
m = new Mutex();

new Thread().start;
new Thread().start;
}
---
The beginning of the output for this code is:
a1-0
a2-0
a2-0
b1-0
b2-0
b2-0
a1-1
a2-1
a2-1

Why is the "a2" and "b2" writeln being repeated?!


Re: Source expression passed to a lazy parameter

2018-04-09 Thread Seb via Digitalmars-d-learn

On Monday, 9 April 2018 at 13:03:38 UTC, Simen Kjærås wrote:

On Monday, 9 April 2018 at 11:33:56 UTC, Alex wrote:

On Monday, 9 April 2018 at 09:20:42 UTC, Simen Kjærås wrote:
Nope. Something along the lines of __traits(getSource, arg) 
has been discussed occasionally.


Is this available somehow? And/or do you have a link to the 
discussion, maybe?


https://forum.dlang.org/post/huyqfcoosgzfneswn...@forum.dlang.org

https://github.com/dlang/dmd/pull/953


I'm pretty sure there have been other discussions too, but a 
brief search yielded too many irrelevant results.


--
  Simen


For reference, the most recent discussion and PR is 
https://github.com/dlang/dmd/pull/7821 (it's currently orphaned)


Re: Parse .eml files

2018-04-09 Thread bachmeier via Digitalmars-d-learn

On Monday, 9 April 2018 at 19:17:20 UTC, Adam D. Ruppe wrote:

My understanding is .eml is the same MIME format the email 
itself and mbox and maildir all use.


Thanks. I didn't know that. I will try it using email.d.


Re: SMTP Mail

2018-04-09 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 9 April 2018 at 15:38:55 UTC, Vino.B wrote:
Thank you very much, I copied your folder arsd under the phobes 
folder in c:\D\... and the program was placed on my desktop and 
tried to execute it from the desktop via rdmd.



I don't think rdmd is seeing the dependency on htmltotext.d. Try 
importing it explicitly from your main:


import arsd.email;
import arsd.htmltotext;

/* the rest of your code */


Just that way rdmd will find it easier.


Re: Parse .eml files

2018-04-09 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 9 April 2018 at 18:47:16 UTC, bachmeier wrote:
Is there a way to do this in D? The email libraries I've found 
don't appear to work with .eml.


My understanding is .eml is the same MIME format the email itself 
and mbox and maildir all use.


So any of those libraries are likely to work with it.

You should open the eml file in Notepad or whatever and see if 
there are more than one message in it. If so, my email.d can 
handle is mbox 
http://dpldocs.info/experimental-docs/arsd.email.processMboxData.html


just `cast(immutable(ubyte)[]) std.file.read` the file to get 
that array.


Otherwise, my email.d would do it as an individual message you 
can construct with this: 
http://dpldocs.info/experimental-docs/arsd.email.IncomingEmailMessage.this.1.html


just std.file.readText and splitLines to get the string[] array.

my email.d is here 
https://github.com/adamdruppe/arsd/blob/master/email.d it depends 
on dom.d htmltotext.d and characterencodings.d


or on dub http://code.dlang.org/packages/arsd-official%3Aemail


Parse .eml files

2018-04-09 Thread bachmeier via Digitalmars-d-learn
I want to save all email messages related to a research project 
into a directory, call a D program to generate an index of all 
messages, and push it to the repo server. That way all coauthors 
have access to all email messages even if they joined a project 
after several years. My client is Thunderbird, which by default 
creates .eml files.


Is there a way to do this in D? The email libraries I've found 
don't appear to work with .eml.


Re: Is std.variant.visit not @nogc?

2018-04-09 Thread Paul Backus via Digitalmars-d-learn

On Monday, 9 April 2018 at 07:07:58 UTC, Chris Katko wrote:

On Monday, 9 April 2018 at 07:02:50 UTC, Hasen Judy wrote:
IMO, this is one more reason why sum-types should be built 
into the language compiler, instead of being implemented in 
user-space.


+1. Any time you have to "create" a fundamental feature in a 
language... from inside the language itself... you're going to 
have confusing error messages, and a huge uphill battle.


I agree in general, but in this case it's actually completely 
doable. In fact, I've done it myself: check out 'sumtype' on 
code.dlang.org. You can replace 'Algebraic' with 'SumType' and 
'visit' with 'match' in helxi's example, and everything Just 
Works™:


import sumtype;

import core.stdc.stdio;

SumType!(T, string) fib_nth(T)(T n)
{
return n % 15
? n % 5
? n % 3
? SumType!(T, string)(n)
: SumType!(T, string)("Fizz")
: SumType!(T, string)("Buzz")
: SumType!(T, string)("Fizzbuzz");
}

void main() @nogc
{
foreach (i; 1 .. 101)
{
fib_nth(i).match!(
(string s) => printf("%s\n", s.ptr),
(int n) => printf("%i\n", n)
);
}
}


Re: SMTP Mail

2018-04-09 Thread Vino.B via Digitalmars-d-learn

On Monday, 9 April 2018 at 13:02:06 UTC, Adam D. Ruppe wrote:

On Sunday, 8 April 2018 at 15:45:48 UTC, Vino wrote:
  I am trying your email.d programming, and i am getting the 
below errors, can you please help me, i just used these 
programs (characterencodings.d, color.d, dom.d, htmltotext.d, 
email.d)


What is your build command?

It looks like a module is just missing in the build.

If you are using rdmd, what is your directory layout too.

(I don't work Sundays btw so that's why I am so slow to 
respond.)


Hi Adam,

Thank you very much, I copied your folder arsd under the phobes 
folder in c:\D\... and the program was placed on my desktop and 
tried to execute it from the desktop via rdmd.


From,
Vino.B



Re: How to destruct class instances allocated by a Region-allocator over a single GC block

2018-04-09 Thread Per Nordlöw via Digitalmars-d-learn
On Monday, 9 April 2018 at 13:51:47 UTC, Steven Schveighoffer 
wrote:
Well, you know the type, because make returned it no? The 
contract is, you call obj = make!X(args), then you have to call 
dispose(obj), where obj is of the type X. That's how it knows.


If you are thinking you want to destroy the whole block at once 
(typed as void[]), that's not how it works.


stdx.allocator is not going to help you with GC collection, 
it's not geared towards that purpose.


Ok, thanks!


Re: How to destruct class instances allocated by a Region-allocator over a single GC block

2018-04-09 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/7/18 10:57 AM, Per Nordlöw wrote:

On Saturday, 7 April 2018 at 07:50:37 UTC, Eduard Staniloiu wrote:

On Friday, 6 April 2018 at 21:49:37 UTC, Per Nordlöw wrote:

On Tuesday, 3 April 2018 at 09:14:28 UTC, Eduard Staniloiu wrote:

So, say `reg` is your allocator, your workflow would be

auto obj = reg.make!Type(args);
/* do stuff */
reg.dispose(obj); // If Type has a __dtor, it will call obj.__dtor
  // and then reg.deallocate(obj)


If I do sucessive calls to reg.make!X where X are different kinds of 
classes of different sizes how does reg.dispose(obj) figure out at 
which address(es) (where emplace filled in the data) the objects reside?


It can't figure out. With custom allocators you have to manually do
the memory management, so the responsibility of when and which object 
needs

to be destroyed falls on the user of the custom allocator.


IMHO, such a complexity should be wrapped in a typed allocation layer. 
Have Andrei spoken anything about `TypedAllocator`(s) to wrap this 
complexity?


Well, you know the type, because make returned it no? The contract is, 
you call obj = make!X(args), then you have to call dispose(obj), where 
obj is of the type X. That's how it knows.


If you are thinking you want to destroy the whole block at once (typed 
as void[]), that's not how it works.


stdx.allocator is not going to help you with GC collection, it's not 
geared towards that purpose.


-Steve


Re: Source expression passed to a lazy parameter

2018-04-09 Thread Stefan Koch via Digitalmars-d-learn

On Monday, 9 April 2018 at 08:27:50 UTC, Per Nordlöw wrote:
Is it possible to get the source expression sent to a lazy 
function?


So that I can implement something like

show(Arg)(lazy Arg arg)
{
writeln(arg.sourceof, arg);
}

used as

show(1+2+3);

will print

1+2+3:6


Because of the way D works with a given piece of code may not 
have a source-location or even a representation which is valid D 
source code.


Note: There is a way to fix this but it's very involved.

Step 1: you use cow (copy-on-write) when modifying AST nodes in 
semantic() or you keep distinct trees.
Step 2: you sanitize implicitly generated code to make sure it's 
actually valid code.
Step 3: you write the generated code, to a well-defined location 
such that source-of can point to a valid location.


also note that support for sourceof at compiletime will bloat the 
executable since it needs to store the source-text.


Re: Source expression passed to a lazy parameter

2018-04-09 Thread Simen Kjærås via Digitalmars-d-learn

On Monday, 9 April 2018 at 11:33:56 UTC, Alex wrote:

On Monday, 9 April 2018 at 09:20:42 UTC, Simen Kjærås wrote:
Nope. Something along the lines of __traits(getSource, arg) 
has been discussed occasionally.


Is this available somehow? And/or do you have a link to the 
discussion, maybe?


https://forum.dlang.org/post/huyqfcoosgzfneswn...@forum.dlang.org

https://github.com/dlang/dmd/pull/953


I'm pretty sure there have been other discussions too, but a 
brief search yielded too many irrelevant results.


--
  Simen


Re: SMTP Mail

2018-04-09 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 8 April 2018 at 15:45:48 UTC, Vino wrote:
  I am trying your email.d programming, and i am getting the 
below errors, can you please help me, i just used these 
programs (characterencodings.d, color.d, dom.d, htmltotext.d, 
email.d)


What is your build command?

It looks like a module is just missing in the build.

If you are using rdmd, what is your directory layout too.

(I don't work Sundays btw so that's why I am so slow to respond.)


Re: SMTP Mail

2018-04-09 Thread Vino via Digitalmars-d-learn

On Sunday, 8 April 2018 at 15:45:48 UTC, Vino wrote:

On Friday, 25 August 2017 at 02:13:42 UTC, Adam D. Ruppe wrote:

[...]


Hi Adam,

  I am trying your email.d programming, and i am getting the 
below errors, can you please help me, i just used these 
programs (characterencodings.d, color.d, dom.d, htmltotext.d, 
email.d)


[...]


Hi All,

 Any help is much appreciated.

From,
Vino.B


Re: Source expression passed to a lazy parameter

2018-04-09 Thread Alex via Digitalmars-d-learn

On Monday, 9 April 2018 at 09:20:42 UTC, Simen Kjærås wrote:
Nope. Something along the lines of __traits(getSource, arg) has 
been discussed occasionally.


Is this available somehow? And/or do you have a link to the 
discussion, maybe?




Re: Benchmarking sigmoid function between C and D

2018-04-09 Thread kinke via Digitalmars-d-learn

On Saturday, 7 April 2018 at 21:53:23 UTC, Guillaume Piolat wrote:

Have you tried LLVM intrinsics? say llvm_exp


While LLVM does have math intrinsics, they seem to boil down to C 
runtime calls in many/most cases. I.e., on Linux x86_64, 
`llvm_exp(real)` simply maps to the C function `expl()` and thus 
yields the same assembly as using `core.stdc.tgmath.exp(real)`: 
https://run.dlang.io/is/kjtM1n


Re: Source expression passed to a lazy parameter

2018-04-09 Thread Simen Kjærås via Digitalmars-d-learn

On Monday, 9 April 2018 at 08:27:50 UTC, Per Nordlöw wrote:
Is it possible to get the source expression sent to a lazy 
function?


Nope. Something along the lines of __traits(getSource, arg) has 
been discussed occasionally.


For lazy what you're asking is impossible, since the compiler 
doesn't know which actual arguments have been passed. show(1+2) 
will look absolutely identical to show(3), will look identical to 
show(myVarWithValue3).


Now, there are some things you can do. Notably, lambdas are 
included verbatim in templated type names, which we can exploit.


struct S(alias fn) {}
void show(alias fn)() {
import std.stdio;
writeln(S!fn.stringof[18..$-1], ": ", fn());
}

unittest {
show!(()=>1+2); // prints "3: 3"
}

As we can see, it optimizes '1+2' to become 3, so it's not 
perfect.


This also works for lambdas that include local variables:

unittest {
int i = 13;
show!(() => i+2); // Prints "i + 2: 15"
}

However, it fails for lambdas that take arguments:

struct S(alias fn) {}
void show(alias fn, T...)(T args) {
import std.stdio;
writeln(S!fn.stringof[18..$-1], ": ", fn(args));
}

unittest {
show!(a => a+2)(3); // Fails to compile
}

The reason this fails is the lambda's textual representation 
decays to '__lambda1'.


There is however still something we can do, but things get even 
less flexible:


struct show(alias fn) {
static void opCall(T...)(T args) {
import std.stdio, std.string;
enum s = show.stringof;
enum i = s.indexOf("=>");
writeln(s[i+3..$-1], ": ", fn(args));
}
}

unittest {
show!(a => a+2)(3); // Prints "a + 2: 5"
}

--
  Simen


Re: Source expression passed to a lazy parameter

2018-04-09 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, April 09, 2018 08:27:50 Per Nordlöw via Digitalmars-d-learn 
wrote:
> Is it possible to get the source expression sent to a lazy
> function?
>
> So that I can implement something like
>
> show(Arg)(lazy Arg arg)
> {
>  writeln(arg.sourceof, arg);
> }
>
> used as
>
>  show(1+2+3);
>
> will print
>
>  1+2+3:6

Given how lazy parameters work, I don't see how that would be possible. lazy
parameters are implemented as delegates, and the function doesn't have
access to what the argument was, just the delegate to get the result of
evaluating it. Functions with lazy parameters don't have to be templates,
and if the function is a template, as long as the type of the expression is
the same, it should only result in one instantiation of the function
template. As such, the only way that something like what you're suggesting
would work would be if there were some way to get the string representation
of the body of a delegate at runtime, and that would be pretty much the same
as getting the string representation of any function at runtime. That sort
of thing simply isn't kept around beyond what's required to compile the
function in the first place. The closest that you'd get would be whatever
comes with debug symbols when they're enabled.

- Jonathan M Davis




Re: Source expression passed to a lazy parameter

2018-04-09 Thread Basile B. via Digitalmars-d-learn

On Monday, 9 April 2018 at 08:27:50 UTC, Per Nordlöw wrote:
Is it possible to get the source expression sent to a lazy 
function?


So that I can implement something like

show(Arg)(lazy Arg arg)
{
writeln(arg.sourceof, arg);
}

used as

show(1+2+3);

will print

1+2+3:6


No (afaik), what you need is an internal compiler function, 
"toChars" IIRC, that's available for each node.


Source expression passed to a lazy parameter

2018-04-09 Thread Per Nordlöw via Digitalmars-d-learn
Is it possible to get the source expression sent to a lazy 
function?


So that I can implement something like

show(Arg)(lazy Arg arg)
{
writeln(arg.sourceof, arg);
}

used as

show(1+2+3);

will print

1+2+3:6



Re: toString contains null for struct with function/method

2018-04-09 Thread number via Digitalmars-d-learn

On Sunday, 8 April 2018 at 15:51:05 UTC, Paul Backus wrote:

On Sunday, 8 April 2018 at 15:04:49 UTC, number wrote:

writeln(s2);// S2(0, null)


S2 is a nested struct [1], which means it has a hidden pointer 
field that's used to access its enclosing scope. If you change 
the definition to `static struct S2`, you'll get the same 
output for both structs.


[1] https://dlang.org/spec/struct.html#nested


Aha, its the nesting. And if i access the outer scope in the 
function its not null anymore.

Thank you!