Re: How make wsatring or dstring default for string and literals?

2020-02-24 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 24 February 2020 at 21:25:50 UTC, Marcone wrote:

foreach(i; "Canção")
Prints:


Try

foreach(dchar i; "Canção") // note the added dchar

to see a different presentation of the very same data.

If you take the strings apart byte by byte that will look 
different, but they all represent the same characters, and 
foreach can help decode that if you ask it to.


Re: How make wsatring or dstring default for string and literals?

2020-02-24 Thread Marcone via Digitalmars-d-learn

On Monday, 24 February 2020 at 19:28:43 UTC, Adam D. Ruppe wrote:

On Monday, 24 February 2020 at 19:27:04 UTC, Marcone wrote:
I live in Brazil and speak português brasileiro and need make 
programs that is good with words like ã, á, ç, ê, etc.


Regular string works with those just fine too.


foreach(i; "Canção")
{
writeln(i);
}
Prints:

C
a
n
�
�
�
�
o


Re: catching Errors in OS callbacks how to print stack trace?

2020-02-24 Thread NaN via Digitalmars-d-learn

On Monday, 24 February 2020 at 13:43:30 UTC, Adam D. Ruppe wrote:

On Monday, 24 February 2020 at 13:42:01 UTC, NaN wrote:

try { writeln(e.msg); }


try `writeln(e.toString());` instead.

msg only contains the message passed to the constructor by 
itself, toString includes the file/line and stack trace members 
too.


easiest way usually.


That worked thanks!



Re: Lambda capture by value

2020-02-24 Thread Steven Schveighoffer via Digitalmars-d-learn

On 2/24/20 3:32 PM, H. S. Teoh wrote:


To fix this, copy the value of 'i' to a local variable inside the loop
body, then the lambda will correctly capture a unique per-iteration
instance of the variable.  Like this:

 foreach (i; iota(5))
 {
auto _i = i;
 printers[i] = () { write(_i); };
 }


Nope. It doesn't ;) Because actually, _i is reused for the loop iteration.

You are thinking that the capture happens on the delegate creation. In 
essence, it just sets a flag to the compiler that the enclosing function 
must be placed on the heap.


Adam's method works because the nested call's stack frame is captured 
when you return that delegate.


In other words, it looks like this:

void main()
{
   static struct __mainStackFrame
   {
   int i;
   int _i;
   }

   __mainStackFrame *frame = new __mainStackFrame; // here's where the 
capture happens

   with(*frame)
   {
  // main's function body, with all variables already being declared
   }
}

-Steve


Re: Lambda capture by value

2020-02-24 Thread Elronnd via Digitalmars-d-learn

printers[i] = () { write(i); };


I know it looks silly but if you make that:

 printers[i] = (int i) { return () { write(i); }; }(i);

it will do what you want.


Or, the slightly prettier (imo)

  printers[i] = ((i) => () => write(i))(i);

Or, if you need to force it to return void:

  printers[i] = ((i) => {write(i);})(i);


Re: Lambda capture by value

2020-02-24 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Feb 24, 2020 at 07:50:23PM +, JN via Digitalmars-d-learn wrote:
> import std.range;
> import std.stdio;
> 
> alias NumberPrinter = void delegate();
> 
> NumberPrinter[int] printers;
> 
> void main()
> {
> foreach (i; iota(5))
> {
> printers[i] = () { write(i); };
> }
> 
> foreach (i; iota(5))
> {
> printers[i]();
> }
> }
> 
> This prints 4 4 4 4 4.
> 
> How to make it so that it prints 0 1 2 3 4? Is it possible without
> changing the delegate definition to void delegate(int)?

The cause of the problem is that 'i' in the first foreach loop is
*reused* across loop iterations, so all 5 lambdas are actually closing
over the same variable, which gets its value replaced by the next
iteration.

To fix this, copy the value of 'i' to a local variable inside the loop
body, then the lambda will correctly capture a unique per-iteration
instance of the variable.  Like this:

foreach (i; iota(5))
{
auto _i = i;
printers[i] = () { write(_i); };
}
 

T

-- 
Curiosity kills the cat. Moral: don't be the cat.


Re: Lambda capture by value

2020-02-24 Thread kinke via Digitalmars-d-learn

On Monday, 24 February 2020 at 19:50:23 UTC, JN wrote:

foreach (i; iota(5))
{
printers[i] = () { write(i); };
}


This allocates 1 closure and generates 1 lambda, so all printers 
are identical delegates. You could use a static foreach:


NumberPrinter[] printers;
static foreach (i; 0..5)
printers ~= () { write(i); };
foreach (d; printers)
d();


Re: Lambda capture by value

2020-02-24 Thread JN via Digitalmars-d-learn

On Monday, 24 February 2020 at 20:00:20 UTC, Adam D. Ruppe wrote:

On Monday, 24 February 2020 at 19:50:23 UTC, JN wrote:

foreach (i; iota(5))
{
printers[i] = () { write(i); };


I know it looks silly but if you make that:

 printers[i] = (int i) { return () { write(i); }; }(i);

it will do what you want.

This is something that used to be common in javascript, write a 
little function that passes the capture-by-value args and 
returns the lambda you actually want and call it immediately.


That extra layer causes the compiler to create a new copy of 
the capture variables snapshotted in time.


D'oh! I am actually familiar with the pattern from Javascript, 
used it many times, but somehow got it mixed up with something 
else and couldn't make it work.


Thanks.


Re: Lambda capture by value

2020-02-24 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 24 February 2020 at 19:50:23 UTC, JN wrote:

foreach (i; iota(5))
{
printers[i] = () { write(i); };


I know it looks silly but if you make that:

 printers[i] = (int i) { return () { write(i); }; }(i);

it will do what you want.

This is something that used to be common in javascript, write a 
little function that passes the capture-by-value args and returns 
the lambda you actually want and call it immediately.


That extra layer causes the compiler to create a new copy of the 
capture variables snapshotted in time.


Lambda capture by value

2020-02-24 Thread JN via Digitalmars-d-learn

import std.range;
import std.stdio;

alias NumberPrinter = void delegate();

NumberPrinter[int] printers;

void main()
{
foreach (i; iota(5))
{
printers[i] = () { write(i); };
}

foreach (i; iota(5))
{
printers[i]();
}
}

This prints 4 4 4 4 4.

How to make it so that it prints 0 1 2 3 4? Is it possible 
without changing the delegate definition to void delegate(int)?


How make wsatring or dstring default for string and literals?

2020-02-24 Thread Marcone via Digitalmars-d-learn
I live in Brazil and speak português brasileiro and need make 
programs that is good with words like ã, á, ç, ê, etc. How can I 
make wstring or dstring default for string and literals? I don't 
want to explicit this every time.


Re: How make wsatring or dstring default for string and literals?

2020-02-24 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 24 February 2020 at 19:27:04 UTC, Marcone wrote:
I live in Brazil and speak português brasileiro and need make 
programs that is good with words like ã, á, ç, ê, etc.


Regular string works with those just fine too.


A small D/GtkD example game

2020-02-24 Thread mark via Digitalmars-d-learn

I've just completed a small D/GtkD game.

It might be useful for others trying to learn GtkD since it is 
only just over 1000 lines, yet shows how to create a dialog-style 
app with a modal dialog and a modeless dialog, and a custom drawn 
widget, as well as keyboard and mouse handling.


The source (and a 64-bit Windows binary) is here:
https://github.com/mark-summerfield/gravitate-d

I'm posting to the Learn forum because I'm just a D beginner, so 
felt that using the announce list would be a bit presumptuous.


Just a Reminder...

2020-02-24 Thread Ron Tarrant via Digitalmars-d-learn
In case you thought the GtkDcoding blog announcements have 
stopped, I just want to let you know that starting with #0101, 
they'll be made in the Announce sub-forum from now on. This is 
where they were originally supposed to be made, but because I got 
the wrong end of the stick last year, I ended up making them here 
in Learn.


My new year's resolution was to move them back to where they 
belong and thus, all future GtkDcoding blog announcements will be 
made in Announce. (So far, I seem to be remembering to do them 
there!)


And here are links to the last two so you don't have to go 
searching:


https://forum.dlang.org/thread/cyukzsyegremxoalz...@forum.dlang.org
https://forum.dlang.org/thread/hqwdxbwgtxcgjwtra...@forum.dlang.org

I apologize for any inconvenience or misunderstanding this may 
have caused.


Re: Order of static this() execution?

2020-02-24 Thread Steven Schveighoffer via Digitalmars-d-learn

On 2/24/20 9:25 AM, Steven Schveighoffer wrote:

I'll file an issue report.


https://issues.dlang.org/show_bug.cgi?id=20605

-Steve


Re: Order of static this() execution?

2020-02-24 Thread Steven Schveighoffer via Digitalmars-d-learn

On 2/23/20 6:55 AM, drathier wrote:

On Sunday, 23 February 2020 at 11:41:25 UTC, Johan Engelen wrote:

On Sunday, 23 February 2020 at 09:59:45 UTC, drathier wrote:
I'm having some trouble with the order in which `static this()` runs. 
This is the order defined in the source file, numbered for convenience:


To avoid confusion: you have all `static this()` in a single source 
file? Or across several source files?


-Johan


They're all in a single source file. The `[template]` prints are inside 
templates, like this:


```
template none(msg) {
 T!(msg)  none;
 static this() {
     none = ((std.functional.toDelegate(&batch!(msg) )))(X!(T!(msg) ));
 }
}
```

The whole reason I have `static this()` is to avoid ctfe crashing from 
trying to run `toDelegate` at compile time:


```
std/functional.d(1501,22): Error: dummyDel.funcptr cannot be evaluated 
at compile time

```


The static this is run in lexical order, but template static this are 
run as if they were stuck into the code at the first part they were 
instantiated.


So for instance, if you have:

template T() {
   int* T;
   static this() {
 T = new int;
   }
}

int x;
static this() {
   x = *T!();
}

The T instantiation comes AFTER the module-level static this. So it will 
run the module static this first, and then the T static this.


How to fix this? I tried putting the template into a separate module. 
Technically, the static constructor will be inserted into the 
instantiating module. But it does seem to order it properly in that case.


This is a limitation that I think D should be able to resolve.

I'll file an issue report.

-Steve


Re: catching Errors in OS callbacks how to print stack trace?

2020-02-24 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 24 February 2020 at 13:42:01 UTC, NaN wrote:

try { writeln(e.msg); }


try `writeln(e.toString());` instead.

msg only contains the message passed to the constructor by 
itself, toString includes the file/line and stack trace members 
too.


easiest way usually.


catching Errors in OS callbacks how to print stack trace?

2020-02-24 Thread NaN via Digitalmars-d-learn
Normally a failed assert gives the file, line number and a stack 
trace, but I hit one today that just prints..


assertion failure

Im sure it is because it's in the WindowProc callback from the 
OS. As the callback is nothrow you need to catch and handle 
anything there, you have to catch all throwables or else it just 
hangs when you hit an assert. But how do I get it to print the 
full file, line, and stack trace?


Here's my window proc...

extern(Windows)
LRESULT WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM 
lparam) nothrow

{
try
{
auto window = cast(Window) (cast(void*) 
GetWindowLongPtr(hwnd, 0));


if (window is null)
return DefWindowProcA(hwnd, msg, wparam, lparam);
else
return window.windowProc(msg, wparam, lparam);
}
catch (Throwable e)
{
try { writeln(e.msg); }
catch(Exception what) {}
PostQuitMessage(0);
return 0;
}
}




Re: What's the dub command to create a library to be published in dub.pm?

2020-02-24 Thread dnsmt via Digitalmars-d-learn

On Monday, 24 February 2020 at 06:02:00 UTC, Adnan wrote:
how can I change dub.json to build it as a library and not look 
for the main function?


The targetType setting determines how a project is build. It 
defaults to autodetect, which attempts to create an executable if 
there's a file at source/app.d. To create a library, either set 
the targetType setting to library explicitly, or make sure there 
are no files that trigger an application build.


See the dub documentation for a complete list of files that 
trigger an application build on autodetect.


https://dub.pm/package-format-json.html#configurations
https://dub.pm/package-format-json.html#target-types