Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread Tejas via Digitalmars-d-learn
On Thursday, 5 January 2023 at 22:49:01 UTC, thebluepandabear wrote: Have fun reading this : https://issues.dlang.org/show_bug.cgi?id=21929 Thanks for the code suggestion although it still doesn't fix the bug. I am curious as to what those brackets do as well. Okay, my bad for writing the

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 05, 2023 at 10:46:07PM +, thebluepandabear via Digitalmars-d-learn wrote: > On Thursday, 5 January 2023 at 17:36:55 UTC, H. S. Teoh wrote: [...] > > ```D > > foreach (BoardSize boardSize; arr) { > > Button button = new Button(); > > button.text = format("%sx%s",

Re: Is there a way to enforce UFCS?

2023-01-05 Thread thebluepandabear via Digitalmars-d-learn
them or remove them. I agree, forbidding function call syntax would be a great usecase for `@property`. It will probably never get implemented though.

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread thebluepandabear via Digitalmars-d-learn
``` These two solutions should compile to approximately the same runtime code, with optimizations enabled. So, it's really down to personal preference; the former is more explicit about what the computer is to do, while the latter is more concise. Thanks! Works great.

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread thebluepandabear via Digitalmars-d-learn
Have fun reading this : https://issues.dlang.org/show_bug.cgi?id=21929 Thanks for the code suggestion although it still doesn't fix the bug. I am curious as to what those brackets do as well.

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread thebluepandabear via Digitalmars-d-learn
On Thursday, 5 January 2023 at 17:36:55 UTC, H. S. Teoh wrote: On Thu, Jan 05, 2023 at 11:55:33AM +, thebluepandabear via Digitalmars-d-learn wrote: [...] ```D foreach (BoardSize boardSize; arr) { Button button = new Button(); button.text = format("%sx%s", boardSize[0],

Re: Address of a class object

2023-01-05 Thread areYouSureAboutThat via Digitalmars-d-learn
On Thursday, 5 January 2023 at 17:23:39 UTC, H. S. Teoh wrote: On Thu, Jan 05, 2023 at 06:32:47AM +, areYouSureAboutThat Also, I cannot read hex, [...] IMNSHO, anyone who claims to be a programmer should at least know that much. ?? Well, like all, I learnt this at uni. .. as well as

Re: GC interaction with malloc/free

2023-01-05 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 05, 2023 at 08:18:42PM +, DLearner via Digitalmars-d-learn wrote: > On Thursday, 5 January 2023 at 19:54:01 UTC, H. S. Teoh wrote: [...] > > core.stdc.stdlib.{malloc,free} *is* the exact same malloc/free that > > C uses, it has nothing to do with the GC. The allocated memory is >

Re: GC interaction with malloc/free

2023-01-05 Thread DLearner via Digitalmars-d-learn
On Thursday, 5 January 2023 at 19:54:01 UTC, H. S. Teoh wrote: On Thu, Jan 05, 2023 at 07:49:38PM +, DLearner via Digitalmars-d-learn wrote: Suppose there is a D main program (not marked anywhere with @nogc), that _both_ A: Calls one or more C functions that themselves call malloc/free;

Re: GC interaction with malloc/free

2023-01-05 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 05, 2023 at 07:49:38PM +, DLearner via Digitalmars-d-learn wrote: > Suppose there is a D main program (not marked anywhere with @nogc), > that _both_ > > A: Calls one or more C functions that themselves call malloc/free; and > also > B: Calls one or more D functions that

GC interaction with malloc/free

2023-01-05 Thread DLearner via Digitalmars-d-learn
Suppose there is a D main program (not marked anywhere with @nogc), that _both_ A: Calls one or more C functions that themselves call malloc/free; and also B: Calls one or more D functions that themselves call malloc/free via `import core.stdc.stdlib;` Assuming the malloc/free's are used

Re: Address of a class object

2023-01-05 Thread Paul via Digitalmars-d-learn
On Thursday, 5 January 2023 at 05:59:26 UTC, Ali Çehreli wrote: While we're here, you can force the class objects to be on the stack as well: scope MyClassVar1 = new MyClass(); I replaced 'auto' with 'scope'. Ali Very interesting. Thanks Ali.

Re: Is there a way to enforce UFCS?

2023-01-05 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 05, 2023 at 02:32:17PM +, Dom DiSc via Digitalmars-d-learn wrote: [...] > I think this is really another usecase for @property: we should forbid the > function call syntax for them (so one needs to use them like a variable). [...] > Properties are not functions. If you want a

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 05, 2023 at 11:55:33AM +, thebluepandabear via Digitalmars-d-learn wrote: [...] > ```D > foreach (BoardSize boardSize; arr) { > Button button = new Button(); > button.text = format("%sx%s", boardSize[0], boardSize[1]); > button.onButtonClick = { >

Re: Address of a class object

2023-01-05 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 05, 2023 at 06:32:47AM +, areYouSureAboutThat via Digitalmars-d-learn wrote: [...] > Second, to be sure your getting the correct results, it would be nice > if there was a 'category of type' in std.traits for: > > isAllocatedOnStack > isAllocatedOnHeap > > As it is, your just

Re: Error "Outer Function Context is Needed" when class declared in unittest

2023-01-05 Thread Vijay Nayar via Digitalmars-d-learn
On Thursday, 5 January 2023 at 16:41:32 UTC, Adam D Ruppe wrote: On Thursday, 5 January 2023 at 16:38:49 UTC, Vijay Nayar wrote: Does that class inherit the scope of the function it is inside, similar to how an inner class does with an outer class? yup. They can see the local variables from

Re: Error "Outer Function Context is Needed" when class declared in unittest

2023-01-05 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 5 January 2023 at 16:38:49 UTC, Vijay Nayar wrote: Does that class inherit the scope of the function it is inside, similar to how an inner class does with an outer class? yup. They can see the local variables from the function.

Re: Error "Outer Function Context is Needed" when class declared in unittest

2023-01-05 Thread Vijay Nayar via Digitalmars-d-learn
On Thursday, 5 January 2023 at 13:47:24 UTC, Adam D Ruppe wrote: On Thursday, 5 January 2023 at 13:27:23 UTC, Vijay Nayar wrote: Why is this error only found when declaring a class in the unittest? A unittest is just a special function, it can run code and have local variables. classes and

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread Tejas via Digitalmars-d-learn
On Thursday, 5 January 2023 at 11:55:33 UTC, thebluepandabear wrote: I am using CSFML D bindings and I have created my own sort of UI library for drawing elements onto the screen. One of the classes I've created is a `Button` class, which contains a delegate called `onButtonClick` which is

Re: Is there a way to enforce UFCS?

2023-01-05 Thread Dom DiSc via Digitalmars-d-learn
On Wednesday, 4 January 2023 at 14:21:46 UTC, bauss wrote: ```d class Foo { int bar; void setBar(Foo foo, int value) { foo.bar = value; } } void main() { foo.setBar(100); // Not UFCS - just method call to the class foo.setBar = 100; // Not UFCS - simply a setter function call

Re: forgetting -betterC means no runtime bounds checking?

2023-01-05 Thread tsbockman via Digitalmars-d-learn
On Thursday, 5 January 2023 at 09:10:00 UTC, areYouSureAboutThat wrote: My question is: why is there no bounds checking occurring if I forget to use -betterC? module test; extern(C) void main() Note that whether bounds checking is performed depends on [compiler

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread tsbockman via Digitalmars-d-learn
On Thursday, 5 January 2023 at 13:05:46 UTC, thebluepandabear wrote: Update some time later: the only way (oof!) around this seems to be using a `static foreach` with arrays: ```D Button[3] b; static foreach (indx, BoardSize boardSize; arr) { b[indx] = new Button(); b[indx].text =

Re: Error "Outer Function Context is Needed" when class declared in unittest

2023-01-05 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 5 January 2023 at 13:27:23 UTC, Vijay Nayar wrote: Why is this error only found when declaring a class in the unittest? A unittest is just a special function, it can run code and have local variables. classes and structs declared inside it have access to those local contexts,

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread tsbockman via Digitalmars-d-learn
On Thursday, 5 January 2023 at 11:55:33 UTC, thebluepandabear wrote: ```D foreach (BoardSize boardSize; arr) { Button button = new Button(); button.text = format("%sx%s", boardSize[0], boardSize[1]); button.onButtonClick = {

Error "Outer Function Context is Needed" when class declared in unittest

2023-01-05 Thread Vijay Nayar via Digitalmars-d-learn
I've run into an unexpected problem that only seems to happen in unittests, but not outside of them. Consider the following example: ``` unittest { class Ab { int a; string b; static class Builder { int _a; string _b; Builder a(int a) { _a = a;

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread thebluepandabear via Digitalmars-d-learn
Update some time later: the only way (oof!) around this seems to be using a `static foreach` with arrays: ```D Button[3] b; static foreach (indx, BoardSize boardSize; arr) { b[indx] = new Button(); b[indx].text = format("%sx%s", boardSize[0], boardSize[1]); b[indx].onButtonClick

How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread thebluepandabear via Digitalmars-d-learn
I am using CSFML D bindings and I have created my own sort of UI library for drawing elements onto the screen. One of the classes I've created is a `Button` class, which contains a delegate called `onButtonClick` which is called when the button is clicked on by the user. Up until now,

Re: forgetting -betterC means no runtime bounds checking?

2023-01-05 Thread Tejas via Digitalmars-d-learn
On Thursday, 5 January 2023 at 09:10:00 UTC, areYouSureAboutThat wrote: I was playing around with betterC, when I discovered, that if i accidently forget to provide -betterC to the compiler, it will still compile this, but, there will be no runtime bounds checking occuring. My question is:

Re: forgetting -betterC means no runtime bounds checking?

2023-01-05 Thread Mathias LANG via Digitalmars-d-learn
On Thursday, 5 January 2023 at 09:17:28 UTC, areYouSureAboutThat wrote: core.exception.ArrayIndexError@test.d(25): index [5] exceeds array of length 5 Aborted (core dumped) This is bounds checking happening.

Re: forgetting -betterC means no runtime bounds checking?

2023-01-05 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
On 05/01/2023 10:17 PM, areYouSureAboutThat wrote: src/rt/dwarfeh.d:330: uncaught exception reached top of stack This might happen if you're missing a top level catch in your fiber or signal handler core.exception.ArrayIndexError@test.d(25): index [5] exceeds array of length 5 Aborted (core

Re: forgetting -betterC means no runtime bounds checking?

2023-01-05 Thread areYouSureAboutThat via Digitalmars-d-learn
On Thursday, 5 January 2023 at 09:10:00 UTC, areYouSureAboutThat wrote: btw. the output (when you forget to use -betterC): 0 1 2 3 4 src/rt/dwarfeh.d:330: uncaught exception reached top of stack This might happen if you're missing a top level catch in your fiber or signal handler

forgetting -betterC means no runtime bounds checking?

2023-01-05 Thread areYouSureAboutThat via Digitalmars-d-learn
I was playing around with betterC, when I discovered, that if i accidently forget to provide -betterC to the compiler, it will still compile this, but, there will be no runtime bounds checking occuring. My question is: why is there no bounds checking occurring if I forget to use -betterC?