Re: D binary io functions

2020-08-30 Thread Arjan via Digitalmars-d-learn

On Sunday, 30 August 2020 at 06:00:20 UTC, Andy Balba wrote:
going nuts trying to figure out which D functions will 
read/write binary files


see this blog:
http://nomad.uk.net/articles/working-with-files-in-the-d-programming-language.html


How do I copy struct having immutable pointer member when enabled DIP1000?

2020-08-30 Thread outlandkarasu via Digitalmars-d-learn

I wrote a struct that having a std.datetime.SysTime member.
However this struct cannot copy from scope variable.

I think because SysTime has a immutable(TimeZone) member and it 
cannot copy to out of scope.



struct A { SysTime timestamp; }

A createA() @safe
{
scope a = A(Clock.currTime());

// Error: scope variable a may not be returned
return a;

// OK
//return A(SysTime(a.timestamp.stdTime, 
a.timestamp.timezone));

}

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

There is a similar situation when struct has string enum member.


enum Tag { tag = "tag" }

struct A { Tag tag; }

A createA() @safe
{
scope a = A(Tag.tag);

// Error: scope variable a may not be returned
return a;

// NG
// return A(a);
// return A(a.tag);
}

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

I have an unsightly workaround.


import std;

enum Tag
{
tag = "tag"
}

/// scope tag to unscope.
Tag unscope(scope Tag tag) @nogc nothrow pure @safe
{
static foreach (e; EnumMembers!Tag)
{
if (e == tag)
{
return e;
}
}

assert(false);
}

struct A
{
Tag tag;

/// add this for construct from value.
this (Tag tag) @nogc nothrow pure @safe scope
{
this.tag = tag;
}

/// add this for copy to out of scope.
this (ref scope const A a) @nogc nothrow pure @safe scope
{
this.tag = a.tag.unscope;
}

// cannot use formal copy constructor.
/+
this (ref return scope const A a) @nogc nothrow pure @safe 
scope

{
this.tag = a.tag.unscope;
}
+/
}

A createA() @safe
{
scope a = A(Tag.tag);

// OK
return A(a);
}


I understand those errors are DIP1000 language design.
However I suppose that DIP1000 check can permit immutable pointer 
in some cases.


Is there a better workaround, practices or patterns?


Re: D binary io functions

2020-08-30 Thread Andre Pany via Digitalmars-d-learn

On Sunday, 30 August 2020 at 10:02:09 UTC, Arjan wrote:

On Sunday, 30 August 2020 at 06:00:20 UTC, Andy Balba wrote:
going nuts trying to figure out which D functions will 
read/write binary files


see this blog:
http://nomad.uk.net/articles/working-with-files-in-the-d-programming-language.html


This blog is fantastic. It would be great if it could be included 
into tour.dlang.org.


Kind regards
Andre


Re: How do I copy struct having immutable pointer member when enabled DIP1000?

2020-08-30 Thread ag0aep6g via Digitalmars-d-learn

On 30.08.20 17:24, outlandkarasu wrote:


enum Tag { tag = "tag" }

struct A { Tag tag; }

A createA() @safe
{
     scope a = A(Tag.tag);

     // Error: scope variable a may not be returned
     return a;

     // NG
     // return A(a);
     // return A(a.tag);
}


[...]

I understand those errors are DIP1000 language design.
However I suppose that DIP1000 check can permit immutable pointer in 
some cases.


If I understand correctly, your point is that an enum pointer is 
guaranteed to refer to static data, so it could be exempt from `scope` 
checks.


At a glance, that makes sense to me. But I guess one question is whether 
it's possible to create an enum value that points to the stack. A cast 
does the trick:


immutable char[1] c = 'e';
E e = cast(E) c[];

DMD accepts it as @safe, implying that the cast is valid and that `e` is 
a safe value. If that is correct, then enum pointers are actually not 
guaranteed to refer to static data. They can just as well point to the 
stack. Consequently, an enum pointer must be treated like a plain 
pointer. I.e., `scope` must treat a `Tag` just like a plain `string`.



Is there a better workaround, practices or patterns?


In your example, you can just remove the `scope` annotation. Why mark a 
local that you want to return with `scope`? Doesn't make sense


But I guess your actual use case isn't as simple. Maybe you can show a 
less reduced version of the code where simply removing `scope` is not an 
option?


Re: D binary io functions

2020-08-30 Thread Andy Balba via Digitalmars-d-learn

On Sunday, 30 August 2020 at 10:02:09 UTC, Arjan wrote:

On Sunday, 30 August 2020 at 06:00:20 UTC, Andy Balba wrote:
going nuts trying to figure out which D functions will 
read/write binary files


see this blog:
http://nomad.uk.net/articles/working-with-files-in-the-d-programming-language.html


Yes, Great, many thanks! This is the kind of online docs Dlang 
needs more of.


Links for these "great docs" addressing various topics should be 
collected into a Dlang.org online file, where each "great doc" 
contains a link, followed by a brief description.


Getting good and useful, online file-io docs for C++ takes a few 
minutes, but getting equivalent useful information on Dlang 
functions took me hours, with no successful outcome.


IMHO Dlang.org online doc related to file io are too terse to be 
useful to anyone trying to learn Dlang, and are only useful to 
those that already familiar with them and who only need a "syntax 
reminder"


Re: Since DMD 2.089.0 and later, compiled .exe showing SFX zip and opening with winRar when use resource.

2020-08-30 Thread Marcone via Digitalmars-d-learn

On Wednesday, 19 August 2020 at 08:27:34 UTC, Kagamin wrote:

On Tuesday, 18 August 2020 at 19:01:17 UTC, Marcone wrote:

SFX zip in it is properties: https://i.imgur.com/dH7jl5n.png
Opening with winRar: https://i.imgur.com/s7C9mZn.png


Probably winrar messing with your file manager. Try to 
uninstall ungerister winrar from your file manager or try a 
different file manager.


I like Winrar. That is no problem before dmd 2.089.0.
If I add an icon in exe for example I got this problem.


Re: Since DMD 2.089.0 and later, compiled .exe showing SFX zip and opening with winRar when use resource.

2020-08-30 Thread novice2 via Digitalmars-d-learn

Dear Marcone, that you want we all to do?
Rar detect sfx by small signature, and some bytes in exe looks 
like signature.

But this is not the problem - just do not open your exe with rar.
Or i just don't understand...

Another options:
1. try to compile with anoter .res or/and  another .ico (if magic 
bytes in .res)

2. wait next dmd version (if magic bytes in dmd runtime)
3. try gdc or ldc from https://dlang.org/download.html (if magic 
bytes in dmd runtime)

4 try to pack yor exe with free packer like https://upx.github.io/


Re: Since DMD 2.089.0 and later, compiled .exe showing SFX zip and opening with winRar when use resource.

2020-08-30 Thread novice2 via Digitalmars-d-learn

5. Open WinRAR support issue
6. Upload your .exe and say WinRar version then other peoples can 
reproduce


Re: How do I copy struct having immutable pointer member when enabled DIP1000?

2020-08-30 Thread outlandkarasu via Digitalmars-d-learn

On Sunday, 30 August 2020 at 16:33:58 UTC, ag0aep6g wrote:

On 30.08.20 17:24, outlandkarasu wrote:


enum Tag { tag = "tag" }

struct A { Tag tag; }

A createA() @safe
{
     scope a = A(Tag.tag);

     // Error: scope variable a may not be returned
     return a;

     // NG
     // return A(a);
     // return A(a.tag);
}


[...]

I understand those errors are DIP1000 language design.
However I suppose that DIP1000 check can permit immutable 
pointer in some cases.


If I understand correctly, your point is that an enum pointer 
is guaranteed to refer to static data, so it could be exempt 
from `scope` checks.


At a glance, that makes sense to me. But I guess one question 
is whether it's possible to create an enum value that points to 
the stack. A cast does the trick:


immutable char[1] c = 'e';
E e = cast(E) c[];

DMD accepts it as @safe, implying that the cast is valid and 
that `e` is a safe value. If that is correct, then enum 
pointers are actually not guaranteed to refer to static data. 
They can just as well point to the stack. Consequently, an enum 
pointer must be treated like a plain pointer. I.e., `scope` 
must treat a `Tag` just like a plain `string`.



Is there a better workaround, practices or patterns?


In your example, you can just remove the `scope` annotation. 
Why mark a local that you want to return with `scope`? Doesn't 
make sense


But I guess your actual use case isn't as simple. Maybe you can 
show a less reduced version of the code where simply removing 
`scope` is not an option?


Thanks for your reply.

I thought that I cannot make non-scope `ref` parameters from 
`scope` array references.

But I found It allowed currently.

My reduced version code is below;


enum Currency : string {
USD = "USD", EUR = "EUR", GBP = "GBP", JPY = "JPY",
}

struct Instrument {
Currency bid;
Currency ask;
}

struct Price {
Instrument instrument;
ulong value;
}

class MinRecorder
{
@nogc nothrow pure @safe:

// prices from local scoped array buffers.
// I want to restrict prices reference to scope.
void update(scope const(Price)[] prices) scope
{
foreach (price; prices)
{
update(price);
}
}

// I thought price parameter need `scope` when called by 
scoped array elements.

// But it can remove `scope` attribute.
void update( /* scope */ ref const(Price) price) scope
{
if (minPrice.isNull || price.value < minPrice.get.value)
{
minPrice = price;
}
}

Nullable!Price minPrice;
}


full example:
https://run.dlang.io/is/wVbrwf

I also found a worried point that I can take non-scope pointer 
from non-scope `ref` parameter in DMV v2.093.1.



class MinPointerRecorder
{
@nogc nothrow pure @safe:

void update(scope const(Price)[] prices) scope
{
foreach (price; prices)
{
update(price);
}
}

void update( /* scope */ ref const(Price) price) scope
{
if (!minPrice || price.value < minPrice.value)
{
// Is this DIP1000 BUG?
// When without DIP1000, reported compile error.
// Error: cannot take address of parameter price
minPrice = &price;
}
}

const(Price)* minPrice;
}


I also think about Flyweight pattern in D.
I expected simple struct that contains cached `immutable` 
reference behaves a simple value type like primitive types.

But reference contained struct is not simple.

If I want a simple value type struct, the struct shouldn't be 
contain any references also include static string or immutable 
reference.


Re: How do I convert an ISO 8601 datetime into a unix timestamp - at compile-time?

2020-08-30 Thread Andrej Mitrovic via Digitalmars-d-learn
On Friday, 28 August 2020 at 12:35:26 UTC, Steven Schveighoffer 
wrote:

It's trying to look up the local timezone at compile time.

You need to specify a time zone:

static time =
SysTime(DateTime.fromISOString("20220101T00"), 
UTC()).toUnixTime;


-Steve


Aw, thanks Steve!


Re: How do I copy struct having immutable pointer member when enabled DIP1000?

2020-08-30 Thread ag0aep6g via Digitalmars-d-learn

On 31.08.20 06:24, outlandkarasu wrote:
I thought that I cannot make non-scope `ref` parameters from `scope` 
array references.

But I found It allowed currently.

[...]

enum Currency : string {
     USD = "USD", EUR = "EUR", GBP = "GBP", JPY = "JPY",
}

struct Instrument {
     Currency bid;
     Currency ask;
}

struct Price {
     Instrument instrument;
     ulong value;
}

[...]

     void update(scope const(Price)[] prices) scope
     {
     foreach (price; prices)
     {
     update(price);
     }
     }

     // I thought price parameter need `scope` when called by scoped 
array elements.

     // But it can remove `scope` attribute.
     void update( /* scope */ ref const(Price) price) scope
     {
     if (minPrice.isNull || price.value < minPrice.get.value)
     {
     minPrice = price;
     }
     }


`ref` kind of implies `scope` [1]. You don't need to type it out. When 
you do type out `scope ref const(Price)`, the `scope` actually doesn't 
apply to the `ref` but to the pointers in `Price` (whereas the `scope` 
in `scope const(Price)[]` applies to the pointer of the array).


So you don't need `scope` on the `price` parameter because you're taking 
it as a `ref`. You would need `scope` if you were taking it as a pointer 
(`scope const(Price)* price`).


By the way, semantically there isn't any reason to take `price` as 
either `ref` or pointer. You can just as well take it by value, since 
you're making a copy of it anyway with `minPrice = price` (and you also 
make a copy earlier with `foreach (price; prices)`).


[...]
I also found a worried point that I can take non-scope pointer from 
non-scope `ref` parameter in DMV v2.093.1.



class MinPointerRecorder
{
@nogc nothrow pure @safe:

     void update(scope const(Price)[] prices) scope
     {
     foreach (price; prices)
     {
     update(price);
     }
     }

     void update( /* scope */ ref const(Price) price) scope
     {
     if (!minPrice || price.value < minPrice.value)
     {
     // Is this DIP1000 BUG?
     // When without DIP1000, reported compile error.
     // Error: cannot take address of parameter price
     minPrice = &price;
     }
     }

     const(Price)* minPrice;
}



Definitely a bug, yes. Reduced test case:


class MinPointerRecorder
{
int* minPrice;
void update(ref int price) @safe
{
minPrice = &price; /* Should not compile. */
}
}

void main() @safe
{
auto r = new MinPointerRecorder;
() { int mp = 42; r.update(mp); } ();
() { ulong[1000] stomp = 13; } ();
import std.stdio: writeln;
writeln(*r.minPrice); /* Prints "13". */
}


I don't think this is in Bugzilla yet. Please file an issue. Or let me 
know if you want me to do it.


https://issues.dlang.org




[1] I'm not exactly sure how it works. As far as I know, it's not 
documented anywhere.