How do you implement complementary "A*(A*A) = A^3" operator overloads (I'm getting strange results)

2024-08-19 Thread Daniel via Digitalmars-d-learn

type_theory.d:
```
module type_theory;
import std.conv;
import prod;

class Type
{
public:
   Prod opBinary(string op="*")(Type r)
   {
  alias l = this;
  return new Prod(l, r);
   }

   Type opBinary(string op="^")(int k)
   in {
  assert (k > 0);
   }
   do {
  if (k == 1)
 return this;

  auto P = this;

  while (k > 0)
  {
 k --;
 P = this * P;
  }

  return P;
   }

   override string toString() {
  auto str = to!string(cast(void*) this);
  if (isAtomic)
 return str;
  return "(" ~ str ~ ")";
   }

   @property string typename() {
  return typeof(this).stringof;
   }

   string typingString() {
  return *this ~ ":" ~ typename;
   }

   string opUnary(string op="*")() {
  return toString();
   }

   bool isAtomic() { return false; }
}

```

prod.d:
```
module prod;
import type_theory;

class Prod : Type
{
public:
   this(Type l, Type r)
   {
  this.l = l;
  this.r = r;
   }

   @property Type left() { return l; }
   @property Type right() { return r; }

   override string toString() {
  return "(" ~ *l ~ r"\times " ~ *r ~ ")";
   }

protected:
   Type r,l;
}

unittest {
   import std.stdio;
   auto A = new Type();
   auto P = new Prod(A,A);
   writeln("P = ", P);
   readln();
}
```

var.d:
```
module var;

import type_theory;

class Var : Type
{
public:
   alias assign this;

   this(string syms)
   {
  this.syms = syms;
   }

   Var opOpAssign(string op="~")(Type assign)
   {
  this.assign = assign;
  return this;
   }

   override string toString() { return syms; }

   @property override bool isAtomic() { return true; }

protected:
   Type assign;
   string syms;
}


unittest {
   import std.stdio;

   Var A = new Var("A");
   A ~= new Type();
   writeln(A);
   auto B = A*A^2;
   writeln(A);

   writeln("B=", B);
   readln();
}
```

Result should be "A\times (A\times A)" however it's always coming 
out (in the var.d unittest):


```
B=((A\times A)\times ((A\times A)\times (A\times A)))
```

In other words when it is supposed to print `A` for each of the 
components, it instead prints "A\times A".


Please give me a D recipe for accomplishing this relatively 
simple thing.


Thanks!


Re: How do you implement complementary "A*(A*A) = A^3" operator overloads (I'm getting strange results)

2024-08-19 Thread Daniel via Digitalmars-d-learn
On Monday, 19 August 2024 at 09:42:44 UTC, Daniel Donnelly, Jr. 
wrote:

type_theory.d:
```
module type_theory;
import std.conv;
import prod;

[...]


I had to put `while (k > 1)` instead of `while (k > 0)` and it 
works.  I still though can't get it to work with the obvious 
recursive formula of `this * this^(k-1)` for example (same 
issue); any ideas?


How do you typically debug / write D code (Visual Studio - known to be broken in Error pane etc), VScode - I get this error...

2024-08-19 Thread Daniel via Digitalmars-d-learn
I give up on Visual Studio VisualD plugin as it's had the same 
issues for over five years, and currently my program runs from 
the command line, but VisualD complains with a 528 nonsensical 
errors.


So I investigated using VScode instead (I do need a debugger), 
and I get this:


```
Couldn't find a debug adapter descriptor for debug type 'code-d' 
(extension might have failed to activate)

```

I have the D-support plugin installed and it auto-creates a 
Launch config when I select it from the dropdown after clicking 
Add Configuration.


The launch.json config looks like this:

```
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: 
https://go.microsoft.com/fwlink/?linkid=830387

"version": "0.2.0",
"configurations": [
{
"type": "code-d",
"request": "launch",
"dubBuild": true,
"name": "Build & Debug DUB project",
"cwd": "${command:dubWorkingDirectory}",
"program": "${command:dubTarget}"
}

]
}

```

I've tried both DUB vs non-DUB launch config.  Same error either 
way.


So how do you guys efficiently debug D if there exists no working 
environment or is there a nice IDE I'm unaware of?


Re: How do you typically debug / write D code (Visual Studio - known to be broken in Error pane etc), VScode - I get this error...

2024-08-19 Thread ryuukk_ via Digitalmars-d-learn

What's your OS?


Debugging works very nice with vscode:

```json
{
"name": "game: client",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/bin/game",
"cwd": "${workspaceFolder}/bin",
},
```

use: 
https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb



![screenshot](https://i.imgur.com/14F8FtR.png)


if you are on windows, you can try 
https://github.com/EpicGamesExt/raddebugger (from rad/Epic 
Games), they plan to make it crossplatform


When i was using windows, i was using: 
https://remedybg.itch.io/remedybg, paid but it works


Re: How to find the right function in the Phobos library?

2024-08-19 Thread Renato Athaydes via Digitalmars-d-learn

On Monday, 19 August 2024 at 06:53:34 UTC, Ron Tarrant wrote:
On Saturday, 17 August 2024 at 17:31:53 UTC, Steven 
Schveighoffer wrote:

Go to dlang.org, select dicumentation, then library reference.

Pick any module, click on it

In the upper right, switch the docs from stable to ddox

Now you can use the search bar and it is interactive. Typing 
in indexOf found it right away.


Good trick, Steve. I've been working/playing with D for almost 
six years and I didn't know that. Thank you.


OMG why is that not the default in that search box??? It's so 
much better with interactive search!


Re: How do you typically debug / write D code (Visual Studio - known to be broken in Error pane etc), VScode - I get this error...

2024-08-19 Thread Daniel via Digitalmars-d-learn

On Monday, 19 August 2024 at 13:05:55 UTC, ryuukk_ wrote:

What's your OS?


Debugging works very nice with vscode:

```json
{
"name": "game: client",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/bin/game",
"cwd": "${workspaceFolder}/bin",
},
```

use: 
https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb



![screenshot](https://i.imgur.com/14F8FtR.png)


if you are on windows, you can try 
https://github.com/EpicGamesExt/raddebugger (from rad/Epic 
Games), they plan to make it crossplatform


When i was using windows, i was using: 
https://remedybg.itch.io/remedybg, paid but it works


How do I get LLDB to work?  I would like to Debug / edit from one 
app, or is RAD Debugger worth it as a standalone?


Re: How do you typically debug / write D code (Visual Studio - known to be broken in Error pane etc), VScode - I get this error...

2024-08-19 Thread ryuukk_ via Digitalmars-d-learn

How do I get LLDB to work?


This is why i asked you what OS you use, but you didn't answer

Click on the link bellow the screenshot and follow the doc

And post log about errors you encounter, otherwise i can't help 
you, i am not on your PC


Re: How do you implement complementary "A*(A*A) = A^3" operator overloads (I'm getting strange results)

2024-08-19 Thread IchorDev via Digitalmars-d-learn
On Monday, 19 August 2024 at 09:42:44 UTC, Daniel Donnelly, Jr. 
wrote:

```d
   Type opBinary(string op="^")(int k)
   in {
  assert (k > 0);
   }
   do {
  if (k == 1)
 return this;

  auto P = this;

  while (k > 0)
  {
 k --;
 P = this * P;
  }

  return P;
   }
```


`^` should be a bitwise XOR, instead you can overload `^^` which 
is for exponentiation.


Also, your algorithm is a little bit flawed. To rework it, it 
would help if you consider cases like:

- `1^^n == 1`
- `n^^0 == sign(n)` (if `n` is 0, returns 1 or undefined)
- `0^^n == 0` (divide by zero if `n` is negative)

It’s cases like these which can help inform us how the algorithm 
should work. If you can’t think of how you’d make all those cases 
line up correctly, try looking at an existing integer 
exponentiation algorithm online. An algorithm written in another 
language will still be applicable in D.


Re: How do you typically debug / write D code (Visual Studio - known to be broken in Error pane etc), VScode - I get this error...

2024-08-19 Thread IchorDev via Digitalmars-d-learn
On Monday, 19 August 2024 at 10:59:33 UTC, Daniel Donnelly, Jr. 
wrote:
So how do you guys efficiently debug D if there exists no 
working environment or is there a nice IDE I'm unaware of?


Use gdb; unless you’re running macOS, in which case use the 
version of lldb included in the macOS developer tools package.


Re: How do you typically debug / write D code (Visual Studio - known to be broken in Error pane etc), VScode - I get this error...

2024-08-19 Thread evilrat via Digitalmars-d-learn
On Monday, 19 August 2024 at 10:59:33 UTC, Daniel Donnelly, Jr. 
wrote:
I give up on Visual Studio VisualD plugin as it's had the same 
issues for over five years, and currently my program runs from 
the command line, but VisualD complains with a 528 nonsensical 
errors.


So I investigated using VScode instead (I do need a debugger), 
and I get this:


```
Couldn't find a debug adapter descriptor for debug type 
'code-d' (extension might have failed to activate)

```

I have the D-support plugin installed and it auto-creates a 
Launch config when I select it from the dropdown after clicking 
Add Configuration.


The launch.json config looks like this:

```
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: 
https://go.microsoft.com/fwlink/?linkid=830387

"version": "0.2.0",
"configurations": [
{
"type": "code-d",
"request": "launch",
"dubBuild": true,
"name": "Build & Debug DUB project",
"cwd": "${command:dubWorkingDirectory}",
"program": "${command:dubTarget}"
}

]
}

```

I've tried both DUB vs non-DUB launch config.  Same error 
either way.


So how do you guys efficiently debug D if there exists no 
working environment or is there a nice IDE I'm unaware of?


In windows you need "C++" extension from Microsoft as it has 
native debugger for that OS.