Re: Group dots not working

2023-12-19 Thread Joel via Digitalmars-d-learn

On Wednesday, 20 December 2023 at 01:45:57 UTC, Joel wrote:
The dots are supposed to keep moving till they hit something 
then change direction.


[...]


Oh, I found the problem, I wasn't resetting the hit bool variable.


Re: Operator "+=" overloading for class?

2023-12-19 Thread Steven Schveighoffer via Digitalmars-d-learn

On Monday, 18 December 2023 at 14:38:14 UTC, Ki Rill wrote:

your code just return result value,
but it should not return but save result to "this"
see example at 
https://dlang.org/spec/operatoroverloading.html#index_op_assignment


I get an error, but don't understand why.
```d
auto opOpAssign(string op)(Value rhs)
{
this = this + rhs;
return this;
}

// Error: `this` is not an lvalue and cannot be modified
```


Assigning an object is like copying a pointer. You may think you 
can try overloading the assignment, but it is 
[forbidden](https://dlang.org/spec/operatoroverloading.html#assignment):


```
However for class types, identity assignment is not allowed. All 
class types have reference semantics, so identity assignment by 
default rebinds the left-hand-side to the argument at the right, 
and this is not overridable.

```

But you aren't trying to do this.

Instead you are trying to reassign the `this` reference, which is 
a local (and also forbidden). Think about it a second, your `this 
+ rhs` is going to allocate a *new* object. Then if the 
assignment to the local `this` parameter succeeded, what happens 
outside the member function? The true reference that is calling 
this will not be updated!


The only way to do this I can see is to reimplement for op=, or 
maybe perform the operation and swap the guts out.


-Steve


Group dots not working

2023-12-19 Thread Joel via Digitalmars-d-learn
The dots are supposed to keep moving till they hit something then 
change direction.


Using DotsLogicType.solid the move at first, but then stop moving 
once they hit something.


What supposed to happen: First they check left or right 
(depending on the dir.x is negative or positive). If no dots hit 
anything then move the whole dot group, otherwise change 
direction x variable. Do the same again, but with up and down.


```d
module freedots;

import jart;

struct Dot {
Vector2 pos, dir;
Color col;
Vector2 wpos;
bool hit;
bool hitx, hity; // old

Vector2 logic(in DotsLogicType dotsLogicType, int hrd, ref 
char[][] hitMap, in RenderTexture canvas) {

Vector2 result;
final switch(dotsLogicType) with(DotsLogicType) {
case none:
break;
case solid:
// mixin(tce("hrd dir.x dir.y".split));
// look horrisontal
if (hrd==0)
if (pos.x+dir.x>=canvas.texture.width || 
pos.x+dir.x<0 || 
hitMap[cast(size_t)(pos.x+dir.x)][cast(size_t)pos.y]=='#') {

hit=true;
}

// look virtical
if (hrd==1)
if (pos.y+dir.y>=canvas.texture.height || 
pos.y+dir.y<0 || 
hitMap[cast(size_t)pos.x][cast(size_t)(pos.y+dir.y)]=='#') {

hit=true;
}
break;
case fold:
if (pos.x+dir.x>=canvas.texture.width || 
pos.x+dir.x<0 || 
hitMap[cast(size_t)(pos.x+dir.x)][cast(size_t)pos.y]=='#') {

dir.x*=-1;
} else pos.x+=dir.x;
if (pos.y+dir.y>=canvas.texture.height || 
pos.y+dir.y<0 || 
hitMap[cast(size_t)pos.x][cast(size_t)(pos.y+dir.y)]=='#') {

dir.y*=-1;
} else pos.y+=dir.y;
break;
}
return result;
}

void draw() {
DrawRectangleV(Vector2(cast(int)pos.x, cast(int)pos.y)*3, 
Vector2(3,3), col);

}
}

struct DotCnt {
Dot[] dots;
static DotsLogicType dotsLogicType=DotsLogicType.none;

void clearDotsHitMap(ref char[][] hitMap) {
foreach(ref d; dots) {
hitMap[cast(size_t)d.pos.x][cast(size_t)d.pos.y]=' ';
}
}
void setDotsHitMap(ref char[][] hitMap) {
foreach(ref d; dots) {
hitMap[cast(size_t)d.pos.x][cast(size_t)d.pos.y]='#';
}
}

void logic(in RenderTexture canvas, ref char[][] hitMap) {
final switch(dotsLogicType) with(DotsLogicType) {
case none:
setDotsHitMap(hitMap);
break;
case solid:
clearDotsHitMap(hitMap);
foreach(hrd; 0..2) {
Vector2 mdir;
bool hit;
// go through dots looking
foreach(ref d; dots) {
mdir=(hrd==0 ? Vector2(d.dir.x,0) : 
Vector2(0,d.dir.y));
d.logic(DotsLogicType.solid, hrd, hitMap, 
canvas);

if (d.hit) {
hit=true;
break;
}
}
if (! hit)
foreach(ref d; dots) {
d.pos+=mdir;
}
if (hit)
foreach(ref d; dots) {
if (hrd==0)
d.dir.x=-mdir.x;
if (hrd==1)
d.dir.y=-mdir.y;
mixin(tce("hrd mdir d.pos 
d.dir".split));

}
} // for loop h and v
setDotsHitMap(hitMap);
break;
case fold:
foreach(ref d; dots)
d.logic(DotsLogicType.fold, 0, hitMap, 
canvas);

break;
}
}

void draw() {
foreach(ref d; dots)
d.draw;
}

void dotifyFrmTex(in PointerNSelecter pntsel, Texture2D tex, 
bool flip=false) {

auto ds=buildXDollarsFromTex(tex, flip);
foreach(y; 0..tex.height)
foreach(x; 0..tex.width)
if (ds[x][y].a!=0)
dots~=Dot(pntsel.pos+Vector2(x,y), 
Vector2(1,1), ds[x][y]);

}
}
```

Here's YouTube video that gives an idea of what my grouped dots 
can do. https://youtu.be/t5EYeV4BYzU?si=bcpGYwYIX2ecfa9Q




Re: serve-d and emacs

2023-12-19 Thread Renato via Digitalmars-d-learn
On Thursday, 20 April 2023 at 19:54:11 UTC, Christian Köstlin 
wrote:
I tried to reproduce my old eglot experiment, and for me 
serve-d was not even compiling with the newest dmd. Which 
versions are you using?


Kind regards,
Christian


I've just managed to make Emacs 29 work with serve-d using 
use-package and eglot (which both come built-in on Emacs 29+).


If anyone is interested, I just downloaded a pre-built binary 
from the serve-d Github releases page, then added this to my 
init.el file:


```lisp
;; D
(use-package d-mode :ensure t
  :init (with-eval-after-load 'eglot
  (add-to-list 'eglot-server-programs
   '(d-mode . 
("~/programming/apps/serve-d")

```

This follows the eglot documentation's advice.
When you go do a D buffer, d-mode starts up automatically, but 
not eglot... just hit "M-x eglot" to get serve-d assistance 
(which will also work on other files you open in the same 
project).


I found the eglot docs quite helpful: 
https://joaotavora.github.io/eglot/#Setting-Up-LSP-Servers


Things that are working for me (so you can check if your setup is 
working):


* eglot-format-buffer - formats the buffer
* minibuffer docs - as you point at code elements.
* code navigation - e.g. hit `s-.` to go to a definition, 
including D stdlib.
* auto-completion - shows available symbols, even from 
non-imported modules.
* flycheck - shows errors as you type, highlighting them in the 
editor.


The auto-import functionality does not seem to work (it doesn't 
automatically insert imports, despite the auto-completions saying 
"auto-import ...". But apart from that, this is a very nice 
environment to write D code!


I also added a [".dir-locals.el" 
file](https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html) on the root of my project with something like this (so that the formatter doesn't put 8 spaces on indentation! And "M-x compile" does the right thing):


```lisp
((nil . ((indent-tabs-mode . nil)
 (tab-width . 4)))
 (d-mode . ((compile-command . "dmd -L-ld_classic -run"
```

Hope that helps others as I had to spend some time trying to do 
more advanced stuff when the setup that actually works is really 
basic.


Re: SIGSEGV (Segmentation Fault) upon setting character in char array (object member var)

2023-12-19 Thread John Kiro via Digitalmars-d-learn

On Tuesday, 19 December 2023 at 14:01:31 UTC, John Kiro wrote:
Thanks Adam. I agree, the behavior associated with the 
initialization here is confusing (compared for example to a 
similarly-looking code in Java). Also I don't get why an array 
of characters would be considered as an immutable array of 
characters (that is a **string**). I agree this could be a 
compiler bug, specially that it's not caught by the compiler.


I mean: specially that **attempting to update it** is not caught 
by the compiler.


Re: SIGSEGV (Segmentation Fault) upon setting character in char array (object member var)

2023-12-19 Thread John Kiro via Digitalmars-d-learn
Thanks Adam. I agree, the behavior associated with the 
initialization here is confusing (compared for example to a 
similarly-looking code in Java). Also I don't get why an array of 
characters would be considered as an immutable array of 
characters (that is a **string**). I agree this could be a 
compiler bug, specially that it's not caught by the compiler.


Re: SIGSEGV (Segmentation Fault) upon setting character in char array (object member var)

2023-12-19 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 19 December 2023 at 13:10:40 UTC, John Kiro wrote:

 class Test {
   static enum MAX = 10;
   uint index = 0;
   auto intArray = new int[MAX];
   auto charArray = new char[MAX];


This is run at compile time, and the compiler treats any char 
array at compile time as an immutable string literal when it 
crosses the barrier into run time. Moreover, this would be a 
static instance of the initial array; the default reference is 
shared across all objects created (so if you modify intArray then 
`new` another object, you'll see the modified intArray!).


You almost never want to assign arrays or objects at compile time 
like this in a class definition; use constructors instead.


I think the `new char[]` thing being immutable in runtime is a 
compiler bug, it makes it immutable but it is still typed as 
mutable in the type system. but this whole static init thing is 
probably not what you want anyway.


SIGSEGV (Segmentation Fault) upon setting character in char array (object member var)

2023-12-19 Thread John Kiro via Digitalmars-d-learn

Hello there,

First time to experiment with DLang, after a long time.

I'm getting a weird behavior with an **array of chars**, where I 
get a segmentation fault upon writing to it (see code and output 
below). What makes this problem weird it two things:


1) Why there is no problem with the **int** array (unlike the 
**char** array)?
2) Why reading the array element (**charArray[index]**) works, 
unlike writing to it?


The problem is gone if I uncomment the constructor code.

Code:

 import std.stdio;

 void main()
 {
   (new Test).runit();
 }

 class Test {
   static enum MAX = 10;
   uint index = 0;
   auto intArray = new int[MAX];
   auto charArray = new char[MAX];

   this() {
 /*
 charArray = new char[MAX];
 */
   }

   void runit() {
 debug writefln("IntArray initially: %s", intArray);
 intArray[index] = 40; //OK
 debug writefln("IntArray becomes: %s", intArray);

 debug writefln("Adding char in place of (%c) at index 
(%d)..",

charArray[index], index);
 charArray[index] = 'a'; //ERROR: segmentation fault 
(code -11)!
 debug writefln("CharArray becomes %s (PROGRAM ABORTS 
BEFORE IT ACTUALLY!)",

   charArray);
   }
 }


Output:

 $ dub run
 Starting Performing "debug" build using 
/home/john/dlang/dmd-2.106.0/linux/bin64/dmd for x86_64.
 Building hello-dlang ~master: building configuration 
[application]

  Linking hello-dlang
  Running hello-dlang
 IntArray initially: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 IntArray becomes: [40, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 Adding char in place of (�) at index (0)..
 Error Program exited with code -11