Re: static weirdness

2018-01-25 Thread Kagamin via Digitalmars-d-learn
On Wednesday, 24 January 2018 at 02:01:54 UTC, Jonathan M Davis 
wrote:

(so there's no need to dereference the pointer to call it)


It used to check this pointer with an assert. When did it change?


BitFlags usage

2018-01-25 Thread Alex via Digitalmars-d-learn

I have a question on usage of BitFlags, described here:
https://dlang.org/library/std/typecons/bit_flags.html

and/or on bitop
https://dlang.org/phobos/core_bitop.html#.bsf

A similar example to the bit flags example is given here:

[code]
import std.typecons;
enum Rs : ubyte
{
None,
s_f = 1 << 0,
s_s = 1 << 1,
s_p = 1 << 2,
t_f = 1 << 3,
t_s = 1 << 4,
t_p = 1 << 5
}

struct R
{
import core.bitop : popcnt;
invariant {/* some useful asserts here */}
this(ubyte val)
{
assert(popcnt(cast(uint)val) == 2); // separate asserts...
r |= cast(BitFlags!Rs)val; // line 20 ... from asignment
}
BitFlags!Rs r;  
alias r this;
}

void main(){}
[/code]

ok, now: the idea is that if I use the functionality of bit 
flags, then I can take advantage of bit operations. And I'm 
looking for the inverse operation of converting a bit flag to its 
raw value, like the line before last in the example on the bit 
flags documentation site.


How should my line 20 looks like to achieve an assignment of a 
raw value to a BitFlags variable in a single step?


Re: static weirdness

2018-01-25 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, January 25, 2018 10:17:34 Kagamin via Digitalmars-d-learn 
wrote:
> On Wednesday, 24 January 2018 at 02:01:54 UTC, Jonathan M Davis
>
> wrote:
> > (so there's no need to dereference the pointer to call it)
>
> It used to check this pointer with an assert. When did it change?

Actually, assert on a pointer to a struct or a reference to a class checks
for null _and_ calls the invariant, and that hasn't changed. But you have to
actually assert the pointer or reference if you want to do that, and the OP
didn't do that. He asserted whether it == null.

- Jonathan M Davis



Re: static weirdness

2018-01-25 Thread Alex via Digitalmars-d-learn

On Thursday, 25 January 2018 at 10:17:34 UTC, Kagamin wrote:
On Wednesday, 24 January 2018 at 02:01:54 UTC, Jonathan M Davis 
wrote:

(so there's no need to dereference the pointer to call it)


It used to check this pointer with an assert. When did it 
change?


Fortunately, I have some compilers here and just tested it. 
2.076.1 results in a segmentation fault, whereas 2.077.0 does not.





Re: static weirdness

2018-01-25 Thread Alex via Digitalmars-d-learn
On Thursday, 25 January 2018 at 12:06:07 UTC, Jonathan M Davis 
wrote:
On Thursday, January 25, 2018 10:17:34 Kagamin via 
Digitalmars-d-learn wrote:

On Wednesday, 24 January 2018 at 02:01:54 UTC, Jonathan M Davis

wrote:
> (so there's no need to dereference the pointer to call it)

It used to check this pointer with an assert. When did it 
change?


Actually, assert on a pointer to a struct or a reference to a 
class checks for null _and_ calls the invariant, and that 
hasn't changed. But you have to actually assert the pointer or 
reference if you want to do that, and the OP didn't do that. He 
asserted whether it == null.


- Jonathan M Davis


Yeah... the cool thing is, that if the function does not need 
anything from the referenced object, then the pointer 
"degenerates" to a "namespace" implicitly... sorry, for strange 
wording.


It seems that this behavior changed from 2.076.1 to 2.077.


Re: static weirdness

2018-01-25 Thread Kagamin via Digitalmars-d-learn
On Thursday, 25 January 2018 at 12:06:07 UTC, Jonathan M Davis 
wrote:
Actually, assert on a pointer to a struct or a reference to a 
class checks for null _and_ calls the invariant, and that 
hasn't changed. But you have to actually assert the pointer or 
reference if you want to do that, and the OP didn't do that. He 
asserted whether it == null.


I mean the compiler implicitly inserted a check like:
struct S
{
auto fun()
{
assert(&this!=null);
return 42;
}
}


Re: static weirdness

2018-01-25 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, January 25, 2018 12:38:25 Kagamin via Digitalmars-d-learn 
wrote:
> On Thursday, 25 January 2018 at 12:06:07 UTC, Jonathan M Davis
>
> wrote:
> > Actually, assert on a pointer to a struct or a reference to a
> > class checks for null _and_ calls the invariant, and that
> > hasn't changed. But you have to actually assert the pointer or
> > reference if you want to do that, and the OP didn't do that. He
> > asserted whether it == null.
>
> I mean the compiler implicitly inserted a check like:
> struct S
> {
>  auto fun()
>  {
>   assert(&this!=null);
>   return 42;
>  }
> }

AFAIK, it has never done that. Walter's stance on null pointers has always
been that that's what segfaults are for, and he's against adding any
additional null checks.

- Jonathan M Davis



Re: static weirdness

2018-01-25 Thread Kagamin via Digitalmars-d-learn

See https://ideone.com/VZ97dh


Re: static weirdness

2018-01-25 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, January 25, 2018 12:42:57 Kagamin via Digitalmars-d-learn 
wrote:
> See https://ideone.com/VZ97dh

I don't know what's going on there. Such an assertion does not seem in line
with what Walter has typically said on the subject.

Thinking about it, I do vaguely recall a discussion sometime last year about
an invariant being invisibly inserted under some set of circumstances. Maybe
that's what's happening? I believe that it was complained about in that
discussion, so maybe it was removed after that. Certainly, if I try that
code locally with master, it runs just fine without hitting any assertions.

- Jonathan M Davis



Re: BitFlags usage

2018-01-25 Thread Ali Çehreli via Digitalmars-d-learn

On 01/25/2018 03:50 AM, Alex wrote:


     r |= cast(BitFlags!Rs)val; // line 20 ... from asignment


r |= BitFlags!Rs(cast(Rs)val);

Ali


Re: BitFlags usage

2018-01-25 Thread Alex via Digitalmars-d-learn

On Thursday, 25 January 2018 at 13:05:07 UTC, Ali Çehreli wrote:

On 01/25/2018 03:50 AM, Alex wrote:

     r |= cast(BitFlags!Rs)val; // line 20 ... from 
asignment


r |= BitFlags!Rs(cast(Rs)val);

Ali


Thanks :)


Re: static weirdness

2018-01-25 Thread Mike Parker via Digitalmars-d-learn
On Thursday, 25 January 2018 at 12:58:12 UTC, Jonathan M Davis 
wrote:


Thinking about it, I do vaguely recall a discussion sometime 
last year about an invariant being invisibly inserted under 
some set of circumstances. Maybe that's what's happening? I 
believe that it was complained about in that discussion, so 
maybe it was removed after that. Certainly, if I try that code 
locally with master, it runs just fine without hitting any 
assertions.



https://dlang.org/changelog/2.077.0.html#removePreludeAssert


Re: static weirdness

2018-01-25 Thread Steven Schveighoffer via Digitalmars-d-learn

On 1/25/18 8:24 AM, Mike Parker wrote:

On Thursday, 25 January 2018 at 12:58:12 UTC, Jonathan M Davis wrote:

Thinking about it, I do vaguely recall a discussion sometime last year 
about an invariant being invisibly inserted under some set of 
circumstances. Maybe that's what's happening? I believe that it was 
complained about in that discussion, so maybe it was removed after 
that. Certainly, if I try that code locally with master, it runs just 
fine without hitting any assertions.



https://dlang.org/changelog/2.077.0.html#removePreludeAssert


I think that changelog is wrong. The prelude assert was added I think to 
all member calls. And it has been removed.


-Steve


Using std traits

2018-01-25 Thread JN via Digitalmars-d-learn
I decided it's time to learn how std traits work. I still find 
the whole compile time business a bit weird to deal with, so I 
decided to write a simple JSON serializer for struct that loops 
over member fields and outputs them.


import std.stdio;
import std.json;
import std.traits;

struct TestStruct
{
@("noserialize") int x;
int y;
int z;
}

void serialize(T)(T obj)
{
if (is(T == struct))
{
foreach (i, member; FieldNameTuple!T)
{
if (!hasUDA!(member, "noserialize"))
{
writeln(member);
}
}
} else {
assert(0, "Not a struct");
}
}

void main()
{
TestStruct ts;
ts.x = 1;
ts.y = 2;
ts.z = 3;
serialize(ts);
}

here's a runnable version: https://ideone.com/U4ROAT


I expected it to output "y" "z", but "x" is also present. What am 
I doing wrong with hasUDA?


Re: Using std traits

2018-01-25 Thread Ali Çehreli via Digitalmars-d-learn

On 01/25/2018 11:49 AM, JN wrote:


     foreach (i, member; FieldNameTuple!T)
     {
     if (!hasUDA!(member, "noserialize"))
     {
     writeln(member);
     }


'member' is a string local variable, which does not have that UDA. You 
need to get the symbol of the struct:


if (!hasUDA!(__traits(getMember, T, member), "noserialize"))
{
writeln(member);
}

However, note that you're using a compile-time foreach, which expands 
its body for each iteration. Since you used a regular if, you have three 
checks at runtime.


What you really want is use a 'static if':

static if (!hasUDA!(__traits(getMember, T, member), 
"noserialize"))

{
writeln(member);
}

Aaah... Much lighter... :)

Even further, you may want to consider using a 'static foreach':

static foreach (i, member; FieldNameTuple!T)
{
// ...
}

That is more powerful because it can iterate over more ranges. However 
there are some differences from the regular foreach: For example, 
'static foreach' does not introduce a scope per iteration.


Ali


Re: Using std traits

2018-01-25 Thread JN via Digitalmars-d-learn

On Thursday, 25 January 2018 at 19:49:05 UTC, JN wrote:

if (!hasUDA!(member, "noserialize"))


Nevermind, I get it now, member is only the field name, not a 
'reference', changed it to:


if (!hasUDA!(mixin(T.stringof ~ "." ~ member), "noserialize"))

and works now



Re: Class instance memory overhead lower than 3 words?

2018-01-25 Thread kinke via Digitalmars-d-learn

On Wednesday, 24 January 2018 at 21:48:21 UTC, Nordlöw wrote:
Why is the memory overhead for a class instance as high as 3 
words (24 bytes on 64-bit systems? I find that annoyingly much 
for my knowledge database application. I'm aware of 
extern(C++), having one word overhead, but such 
extern(C++)-classes cannot use all of D; I get compilation 
errors such as


node.d(99,25): Error: Internal Compiler Error: type 
`inout(Edge)[]` can not be mapped to C++


If you don't need C++ interop and only care about getting rid of 
the monitor pointer, you can declare the class as C++ class but 
use the D linkage/ABI for all functions, so that they accept D 
slices:


extern(C++) class Node
{
extern(D):
void foo(Node[] nodes) {}
}

Is a struct not an option?


curl_mime_init: How to get curl handle?

2018-01-25 Thread Andre Pany via Digitalmars-d-learn

Hi,

I need to send multipart form data using curl. Until now I build 
the message body myself according to the HTML RFC but it is 
getting complex with large files causing Out Of Memory Exceptions.


Low level curl supports multipart form data and I think I copied 
the C headers accordingly. The issue is curl_mime_init needs the 
reference to CURL and it seems I do not get it from std.net.curl: 
HTTP.


curl_mime *curl_mime_init(CURL *easy);

curl_mime* form = null;
auto http = HTTP("");
form = curl_mime_init(http.handle.handle);

Error: struct std.net.curl.Curl member handle is not accessible

Is there a way to get curl_mime_init working?

Kind regards
André