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

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

On Monday, 9 April 2018 at 03:20:58 UTC, helxi wrote:

Is std.variant.visit not @nogc?


These error messages REALLY need to be fixed.

visit, being a template, is @nogc or not based on the arguments 
passed to it as well as its own body, so while the error messages 
point to visit itself, these are frequently actually caused the 
predicate your pass.


well, in this case, it is actually visit itself.

phobos/std/variant.d(2464): Error: @nogc function 
std.variant.visitImpl!(true, VariantN!(8u, int, string), function 
(string s) => printf("%s\x0a", cast(immutable(char)*)s), function 
(int n) => printf("%i\x0a", n)).visitImpl cannot call non-@nogc 
constructor std.variant.VariantException.this
phobos/std/variant.d(2469): Error: @nogc function 
std.variant.visitImpl!(true, VariantN!(8u, int, string), function 
(string s) => printf("%s\x0a", cast(immutable(char)*)s), function 
(int n) => printf("%i\x0a", n)).visitImpl cannot call non-@nogc 
function std.variant.VariantN!(8u, int, 
string).VariantN.peek!int.peek
phobos/std/variant.d(2469): Error: @nogc function 
std.variant.visitImpl!(true, VariantN!(8u, int, string), function 
(string s) => printf("%s\x0a", cast(immutable(char)*)s), function 
(int n) => printf("%i\x0a", n)).visitImpl cannot call non-@nogc 
function std.variant.VariantN!(8u, int, 
string).VariantN.peek!string.peek
phobos/std/variant.d(2173): Error: template instance 
`std.variant.visitImpl!(true, VariantN!(8u, int, string), 
function (string s) => printf("%s\x0a", cast(immutable(char)*)s), 
function (int n) => printf("%i\x0a", n))` error instantiating



Ugh, so unreadable even on this level, but at least the actual 
information is there:


std.variant.VariantException.this is not marked @nogc (but it 
prolly could be)


VariantN.peek is not @nogc because it calls Object.opEquals... 
which is broken af, sadly, but can probably be fixed for this 
case by marking TypeInfo.opEquals nogc.



I think the peek one is going to be the harder one to work around 
since any reimplementation of peek is probably going to still 
call it... though MAYBE you can use `!is` instead of `!=`... and 
any reimplementation of visit needs to check types.



But if you wanna try to work around it, I would copy the 
visitImpl and visit functions out of std.variant and do some 
adjustments, then call your version instead (which will be fairly 
easy btw since they are already UFCS).




FYI: the way I got these error messages was to go into the Phobos 
source and add @nogc to the lowest level template in the 
instantiation chain. Then just recompile your program - no need 
to recompile Phobos itself since they are templates.


I wish the error messages would just do this for you (simulate 
@nogc at the second-highest level) to keep you from having to 
edit it yourself just to know what it is.




Is std.variant.visit not @nogc?

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

import std.variant, core.stdc.stdio;

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

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


Complains source/app.d(18,19): Error: @nogc function D main 
cannot call non-@nogc function std.variant.visit!(function 
(string s) => printf("%s\x0a", cast(immutable(char)*)s), function 
(int n) => printf("%i\x0a", n)).visit!(VariantN!(16LU, int, 
string)).visit

/usr/bin/dmd failed with exit code 1.


If so, is there a way to emulate `visit` in a @nogc setting?


Re: Game and GC

2018-04-08 Thread solidstate1991 via Digitalmars-d-learn

On Monday, 9 April 2018 at 01:01:18 UTC, Chris Katko wrote:
Why... associative arrays? Wouldn't that become expensive when 
you hit 1,000s, or 10,000's of objects, for something as tiny 
as a coordinate (two or three floats) lookup?
Well, that's the other reason why I was looking for a different 
solution. Currently it's quite fast, maybe because it uses 
integers for the most part (only transformation effects use 
floats due to easier workarounds with vectorization), might 
replace the multiple associative arrays with a single 
auto-sorting one.


Re: Checking if a function pointer is set or null

2018-04-08 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, April 09, 2018 00:25:08 solidstate1991 via Digitalmars-d-learn 
wrote:
> Would the if(!(myFunctionPointer is null)){} work is I
> intended?

You can also do

if(myFunctionPointer !is null)

- Jonathan M Davis



Re: Checking if a function pointer is set or null

2018-04-08 Thread Uknown via Digitalmars-d-learn

On Monday, 9 April 2018 at 00:25:08 UTC, solidstate1991 wrote:
Would the if(!(myFunctionPointer is null)){} work is I 
intended?


Yes, that works as you expect

https://run.dlang.io/is/ZTtm0P


Re: Game and GC

2018-04-08 Thread Chris Katko via Digitalmars-d-learn

On Monday, 9 April 2018 at 00:25:21 UTC, solidstate1991 wrote:
On Saturday, 24 February 2018 at 07:12:21 UTC, Guillaume Piolat 
wrote:

From my experience a combination of the following is necessary:
- not having the audio thread registered
- using pools aggressively for game entities


Also you can save a lot of clockcycles if you put @nogc 
everywhere you don't allocate on the heap, the stack will be 
automatically cleaned up.


I'm currently thinking on restructuring the way my engine 
handles display lists on sprites (dynamic array contains the 
priority, multiple associative arrays for Coordinates, sprites, 
attributes), however if enabling exception handling in @nogc 
parts will enable associative array indexing, I'll just skip 
the whole procedure, otherwise probably moving the whole thing 
to rbtree.


Why... associative arrays? Wouldn't that become expensive when 
you hit 1,000s, or 10,000's of objects, for something as tiny as 
a coordinate (two or three floats) lookup?


Checking if a function pointer is set or null

2018-04-08 Thread solidstate1991 via Digitalmars-d-learn
Would the if(!(myFunctionPointer is null)){} work is I 
intended?


Re: Game and GC

2018-04-08 Thread solidstate1991 via Digitalmars-d-learn
On Saturday, 24 February 2018 at 07:12:21 UTC, Guillaume Piolat 
wrote:

From my experience a combination of the following is necessary:
- not having the audio thread registered
- using pools aggressively for game entities


Also you can save a lot of clockcycles if you put @nogc 
everywhere you don't allocate on the heap, the stack will be 
automatically cleaned up.


I'm currently thinking on restructuring the way my engine handles 
display lists on sprites (dynamic array contains the priority, 
multiple associative arrays for Coordinates, sprites, 
attributes), however if enabling exception handling in @nogc 
parts will enable associative array indexing, I'll just skip the 
whole procedure, otherwise probably moving the whole thing to 
rbtree.


Re: stirling numbers and multidimensional arrays

2018-04-08 Thread ag0aep6g via Digitalmars-d-learn

On 04/08/2018 06:15 PM, popgen wrote:
I am trying to implement some code to calculate Stirling numbers.  The 
code shown below provides the correct calculation but throws a 
Segmentation fault: 11 once it is done running. I suspect there is 
something with the way I am setting up the multidimensional array.

[...]

int stirling1(int n, int k)
{
     auto matrix = new int[][](n+1,k+1) ;

[...]

     for(int i = 1; i <= n ; i++)
     {
     for(int q = 1; q <= i ; q++)


Should it be `q <= k` here? You're using q as an index into an array of 
length k + 1. If you go up to i, you'll exceed that and go out of bounds.


That you're seeing a segfault instead of a range error indicates that 
you're compiling with -release. Better not do that when debugging.


stirling numbers and multidimensional arrays

2018-04-08 Thread popgen via Digitalmars-d-learn
I am trying to implement some code to calculate Stirling numbers. 
 The code shown below provides the correct calculation but throws 
a Segmentation fault: 11 once it is done running. I suspect there 
is something with the way I am setting up the multidimensional 
array.




import std.stdio;
import std.datetime ;
import std.conv ;
import std.file;
import std.string;
import std.regex;
import std.bigint ;
import std.range : enumerate;


int stirling1(int n, int k)
{
auto matrix = new int[][](n+1,k+1) ;
for(int i = 0; i <= n; i++)
{
matrix[i][0] = 0 ;
}
for(int i = 0; i <= k; i++)
{
matrix[0][k] = 0 ;
}
for(int i = 1; i <= n ; i++)
{
for(int q = 1; q <= i ; q++)
{
if(q == 1 || i == q)
{
matrix[i][q] = 1 ;
}
else
{
matrix[i][q] = q*matrix[i-1][q] + 
matrix[i-1][q-1] ;

}
}
}
return(matrix[n][k]) ;
}

void main()
{
writeln("s(n,k) for s(7,2)") ;
writeln(stirling1(7,2)) ;
}




Re: toString contains null for struct with function/method

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

On Sunday, 8 April 2018 at 15:04:49 UTC, number wrote:
the write() shows a 'null' if the struct has a function/method. 
why is that?

```
import std.stdio;

void main()
{
struct S
{
int i;
}
S s;
writeln(s);// S(0)
writeln(typeid(s).sizeof); // 8

struct S2
{
int i;
this(this){}
}
S2 s2;
import std.conv: to;
writeln(s2);// S2(0, null)
writeln(typeid(s2).sizeof); // 8
}
```


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


Re: SMTP Mail

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

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

On Tuesday, 22 August 2017 at 12:52:24 UTC, Vino.B wrote:
  Request your help on sending Mails, I am able to receive 
mails with empty body the line "smtp.message ="Example 
Message" doesn't seem to be working and also please let me 
know how do i send a file as a attachment in a email.


The message there needs to be the complete message, including 
headers. The SMTP struct is pretty low-level.


My email.d (plus its dependencies, characterencodings.d, dom.d, 
and htmltotext.d) has the code to form a full message. It isn't 
very documented though.


https://github.com/adamdruppe/arsd


auto message = new EmailMessage();
message.to ~= "some@email";
message.subject = "Subject"
message.setTextBody("hi");
message.send(RelayInfo("smtp://whatever", "user", "pass"));


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)


Program
import arsd.email;
void main() {

auto message = new EmailMessage();
message.to ~= "v...@xxx.com";
message.subject = "Test";
message.setHtmlBody("1.Line 12.Line 23.Line 
3");

message.addAttachment("text/txt", "C:\\Temp\\test.log", "Text");
message.send(RelayInfo("smtp://smtp..com"));
}

Error
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
C:\Users\-TSK-X~1\AppData\Local\Temp\6\.rdmd\rdmd-dmail.d-4C543ACF04BF46D1398435D9500B3B70\objs\dmai
l.exe.obj(dmail.exe)
 Error 42: Symbol Undefined 
__D6object__T14__switch_errorZQrFNaNbNiNfAyakZv

C:\Users\-TSK-X~1\AppData\Local\Temp\6\.rdmd\rdmd-dmail.d-4C543ACF04BF46D1398435D9500B3B70\objs\dmai
l.exe.obj(dmail.exe)
 Error 42: Symbol Undefined 
__D4arsd10htmltotext10htmlToTextFAyabiZQg

C:\Users\-TSK-X~1\AppData\Local\Temp\6\.rdmd\rdmd-dmail.d-4C543ACF04BF46D1398435D9500B3B70\objs\dmai
l.exe.obj(dmail.exe)
 Error 42: Symbol Undefined __D4arsd10htmltotext12__ModuleInfoZ
Error: linker exited with status 3

From,
Vino.B


Re: std.datetime.systime: days Deprecation message

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

On Saturday, 7 April 2018 at 18:12:00 UTC, Alex wrote:

On Saturday, 7 April 2018 at 17:25:07 UTC, Vino wrote:

Hi All,

  Request your help on the below Deprecation message.

import std.datetime.systime: Clock, days, SysTime;

void main (int AgeSize) {
int AgeSize = 1
auto ct2 = Clock.currTime(), st2 = ct2 + days(-AgeSize);
}
test.d(30): Deprecation: Symbol core.time.days is not visible 
from module test.d because it is privately imported in module 
systime



From,
Vino.B


As far as I understand it, days are located in core.time.

import core.time : days;


Hi Alex,

  Thank you, that resolved the issue.

From,
Vino.B



toString contains null for struct with function/method

2018-04-08 Thread number via Digitalmars-d-learn
the write() shows a 'null' if the struct has a function/method. 
why is that?

```
import std.stdio;

void main()
{
struct S
{
int i;
}
S s;
writeln(s);// S(0)
writeln(typeid(s).sizeof); // 8

struct S2
{
int i;
this(this){}
}
S2 s2;
import std.conv: to;
writeln(s2);// S2(0, null)
writeln(typeid(s2).sizeof); // 8
}
```


Re: HTTP-methods and encoding

2018-04-08 Thread Vindex via Digitalmars-d-learn

On Sunday, 8 April 2018 at 06:51:22 UTC, ikod wrote:

On Saturday, 7 April 2018 at 23:54:21 UTC, Vindex wrote:

On Saturday, 7 April 2018 at 15:58:14 UTC, Seb wrote:

On Saturday, 7 April 2018 at 13:02:39 UTC, Vindex wrote:
There is an error on some sites when using HTTP-methods 
(std.net.curl.get, std.net.curl.post):
std.encoding.EncodingException@std/encoding.d(2505): 
Unrecognized Encoding: utf8


Is there a beautiful way around it?
For the GET-method I use the download() and readText(). But 
for the POST-method I can not come up with an alternative 
solution.


That's weird. std.net.curl should be able to handle UTF-8.
What content are you trying to download/post?
Maybe you can open a bug report for it?

In any case, you might want to checkout requests:

https://github.com/ikod/dlang-requests

It's by far more convenient to use than std.net.curl


The problem is that "utf-8" (or "UTF-8") is required instead 
of "utf8".


I tried to get HTML and JSON. For example, this GET-query 
returns error:

"https://yobit.net/api/3/ticker/btc_usd;

Thank you for the advice


Hello,

"utf-8" (or "UTF-8") is required instead of "utf8" - explain, 
please.


Anyway this code works as expected:

import requests;
import std.stdio;
import std.format;

void main() {
auto rq = Request();
auto rs = rq.get("https://yobit.net/api/3/ticker/btc_usd;);
writeln(rs.responseBody);
}

output:

{"btc_usd":{"high":7216.09463851,"low":6950,"avg":7083.04731925,"vol":753989.73116823,"vol_cur":105.94453165,"last":7114,"buy":7114.,"sell":7135.1911,"updated":1523170067}}


Thank you, I will use requests.
Function _decodeContent() in curl.d consists:

if (encoding == "UTF-8") return cast(char[])(content);
auto scheme = EncodingScheme.create(encoding);
enforce!CurlException(scheme !is null, format("Unknown encoding 
'%s'", encoding));


Encoding name (in std.encoding) is transferred to the lower case 
and matches are checked among the supported encoding names. There 
is "utf-8", but there is no "utf8" (without the hyphen).


Re: Benchmarking sigmoid function between C and D

2018-04-08 Thread kinke via Digitalmars-d-learn
On Sunday, 8 April 2018 at 05:35:10 UTC, Arun Chandrasekaran 
wrote:

Did you also generate the bar graph plot using D?


Heh nope, I used LibreOffice Calc for that.


Re: HTTP-methods and encoding

2018-04-08 Thread ikod via Digitalmars-d-learn

On Saturday, 7 April 2018 at 23:54:21 UTC, Vindex wrote:

On Saturday, 7 April 2018 at 15:58:14 UTC, Seb wrote:

On Saturday, 7 April 2018 at 13:02:39 UTC, Vindex wrote:
There is an error on some sites when using HTTP-methods 
(std.net.curl.get, std.net.curl.post):
std.encoding.EncodingException@std/encoding.d(2505): 
Unrecognized Encoding: utf8


Is there a beautiful way around it?
For the GET-method I use the download() and readText(). But 
for the POST-method I can not come up with an alternative 
solution.


That's weird. std.net.curl should be able to handle UTF-8.
What content are you trying to download/post?
Maybe you can open a bug report for it?

In any case, you might want to checkout requests:

https://github.com/ikod/dlang-requests

It's by far more convenient to use than std.net.curl


The problem is that "utf-8" (or "UTF-8") is required instead of 
"utf8".


I tried to get HTML and JSON. For example, this GET-query 
returns error:

"https://yobit.net/api/3/ticker/btc_usd;

Thank you for the advice


Hello,

"utf-8" (or "UTF-8") is required instead of "utf8" - explain, 
please.


Anyway this code works as expected:

import requests;
import std.stdio;
import std.format;

void main() {
auto rq = Request();
auto rs = rq.get("https://yobit.net/api/3/ticker/btc_usd;);
writeln(rs.responseBody);
}

output:

{"btc_usd":{"high":7216.09463851,"low":6950,"avg":7083.04731925,"vol":753989.73116823,"vol_cur":105.94453165,"last":7114,"buy":7114.,"sell":7135.1911,"updated":1523170067}}