Re: Better debugging?

2021-10-03 Thread max haughton via Digitalmars-d-learn

On Sunday, 3 October 2021 at 22:21:45 UTC, Tim wrote:

Hi all,

I am currently using GDB within VScode with the -gc DMD2 
compiler switch and my debugging is not amazing. Whenever I 
inspect a struct/object it just shows me the pointer rather 
than the object information and strings come up as a gross 
array of the characters. Does anybody happen to know whether 
LDB is better or how I can have a nicer debug environment?


Thanks in advance


Might be something for Iain to weigh in one when it comes to GDC 
specifically but the non-dmd compilers generate better debug info.


Re: Python's list equivalent with std.variant?

2021-10-03 Thread Ali Çehreli via Digitalmars-d-learn

On 10/3/21 3:22 PM, rjkilpatrick wrote:

> I am trying to emulate Python's list in D, to store some derived classes

I notice your code comment about SuperClass[]. It is still the most 
obvious solution here. You just need to check the reuslt of a 
cast(DerivedClass):


import std.stdio : writefln;
import std.variant;
import std.conv;

// Arbitrary super class
class SuperClass {
 this() {
 }
}

// Derived class with members
class DerivedClass : SuperClass {
public:
 this(float a) {
 this.a = a;
 }
 float a;
}

class OtherDerivedClass : SuperClass {}

void main() {
  // When we use `SuperClass[] list;` here, we find 'a' is hidden by 
the base class


  // [Ali]: When you must know what the exact derived type you
  //are using, generally there is a better approach.
  //
  //Assuming that you really want to "downcast", then you
  //simply cast to DerivedClass and see whether the
  //pointer is null or not. (See below.)

  SuperClass[] list;

  // Attempting to append derived class instances to list
  list ~= new DerivedClass(1.0f);
  list ~= new OtherDerivedClass;

  foreach (i, item; list) {
auto p = cast(DerivedClass)item;

if (p) {
  writefln!"%s: Yes, a DerivedClass object with a == %s."(i, p.a);

} else {
  writefln!"%s: No, not a DerivedClass object."(i);
}
  }
}

Ali



Re: Better debugging?

2021-10-03 Thread Basile B. via Digitalmars-d-learn

On Sunday, 3 October 2021 at 22:21:45 UTC, Tim wrote:

Hi all,

I am currently using GDB within VScode with the -gc DMD2 
compiler switch and my debugging is not amazing. Whenever I 
inspect a struct/object it just shows me the pointer rather 
than the object information and strings come up as a gross 
array of the characters. Does anybody happen to know whether 
LDB is better or how I can have a nicer debug environment?


Thanks in advance


1. LDC2 generate better debug infos, especially for classes, 
although this might change from the next DMD version (it will 
include the inherited fields, just like LDC2).


2. to inspect objects you must evluate the dereference of an 
instance.


for example for a project, if  i eval `d`, a class instance VS 
`*d`, the pointed chunk of memory.


| name| value
| ---| ---
d | 0x77b4b480
s | 0x77b506c0
locParentAggr | 0x0
symParentAggr | 0x0
thisParentAggr | 0x0
needInheritance | false
this | 0x77b506c0
loc | 0x77b43ea0
evaluation of `d` | 0x77b4b480
evaluation of `*d` | { = { 
= {}, startPos = {line = 18, column = 1}, ir = 
0x0}, name = 0x77b4e5e0, symbol = 0x77b506c0, attributes 
= 0x0, asTypeDeclared = 0x77b50720, kind = 34 '\"', 
protection = 0 '\\000', progress = 0 '\\000'}


I don't know how the debugger gui you use works, here this is 
just a paste of dexed-ide table for locals and custom 
expressions. More often I use a debug popup:


![](https://i.imgur.com/cGdQfOr.png)

[A similar feature seems possible in 
vscode](https://github.com/microsoft/vscode-java-debug/issues/444), maybe post a feature request to the extension developpers, as the debug popup is often very handy (no need to type expressions, as they are already there in the code !)


Re: Python's list equivalent with std.variant?

2021-10-03 Thread jfondren via Digitalmars-d-learn

On Sunday, 3 October 2021 at 22:22:48 UTC, rjkilpatrick wrote:

```d
void main() {
// When we use `SuperClass[] list;` here, we find 'a' is 
hidden by the base class

Variant[] list;

// Attempting to append derived class instances to list
list ~= new DerivedClass(1.0f);
list ~= new OtherDerivedClass;

list[0].a;
list[0].to!(get!(list[0].type)).a.writeln;
}
```


This works:

```d
void main() {
Variant[] list;

list ~= new DerivedClass(1.0f).Variant;
list ~= new OtherDerivedClass().Variant;

writeln(list[0].get!DerivedClass.a);
}
```

Parameters passed in !() need to be statically known, at 
compile-time, so `get!(list[0].type)` doesn't make sense with a 
runtime list.


If everything in the list is going to be a child of some class, 
then you don't need std.variant at all, you can just use OOP:


```d
import std.stdio : writeln;

class SuperClass {
this() {
}
}

class DerivedClass : SuperClass {
public:
this(float a) {
this.a = a;
}
float a;
}

class OtherDerivedClass : SuperClass {}
class YetAnotherDerivedClass : SuperClass {}

void main() {
SuperClass[] list;

list ~= cast(SuperClass) new DerivedClass(1.0f);
list ~= cast(SuperClass) new OtherDerivedClass;
list ~= cast(SuperClass) new YetAnotherDerivedClass;

writeln((cast(DerivedClass) list[0]).a);

foreach (obj; list) {
if (auto deriv = cast(DerivedClass) obj) {
writeln("I found a DerivedClass: ", deriv.a);
} else if (cast(OtherDerivedClass) obj) {
writeln("I found an OtherDerivedClass");
} else {
writeln("I found an unexpected child: ", obj);
}
}
}
```

output:

```
1
I found a DerivedClass: 1
I found an OtherDerivedClass
I found an unexpected child: variant.YetAnotherDerivedClass
```

Object casts like that are `null` when the cast is invalid.

If you don't necessarily have a superclass, but still do have a 
definite number of possible member types, you can use std.sumtype:


```d
import std.stdio : writeln;
import std.sumtype;

struct A { float a; }
struct B { }
struct C { }
alias Alpha = SumType!(A, B, C);

void main() {
Alpha[] list;

list ~= A(1.0f).Alpha;
list ~= B().Alpha;
list ~= C().Alpha;

list[0].tryMatch!((A a) => writeln(a.a));

foreach (obj; list) {
obj.match!(
(A a) => writeln("I found A(", a.a, ")"),
(B _) => writeln("I found B"),
(C _) => writeln("I found C"),
);
}
}
```

output:

```
1
I found A(1)
I found B
I found C
```


Re: Better debugging?

2021-10-03 Thread Imperatorn via Digitalmars-d-learn

On Sunday, 3 October 2021 at 22:27:20 UTC, Tim wrote:

On Sunday, 3 October 2021 at 22:26:15 UTC, Imperatorn wrote:

On Sunday, 3 October 2021 at 22:21:45 UTC, Tim wrote:

[...]


You don't say which operating system you are using.
I usually use Visual D which works great imo.

If I use vscode I use the C++ debug extension (don't remember 
what it's called).


If I debug outside of the IDE I use WinDbg, also has source 
debug support if configured correctly.


If you are on Linux I'm not sure, but I would go for the C++ 
thing probably.


Apologies. I use KDE linux


Then I guess code-d extension + MS C++ extension in order to 
debug, and webfreak's NativeDebug extension 
(https://marketplace.visualstudio.com/items?itemName=webfreak.debug)


Re: Better debugging?

2021-10-03 Thread Adam Ruppe via Digitalmars-d-learn

On Sunday, 3 October 2021 at 22:21:45 UTC, Tim wrote:

 -gc DMD2 compiler switch


tried plain -g ?

-gc is a compatibility debug thing for things with zero D 
support. p oboslete now


Re: Python's list equivalent with std.variant?

2021-10-03 Thread Imperatorn via Digitalmars-d-learn

On Sunday, 3 October 2021 at 22:22:48 UTC, rjkilpatrick wrote:

Hi.

I am trying to emulate Python's list in D, to store some 
derived classes


[...]


You would have to explicitly cast(Variant) when appending to your 
array.


But in the last example where you have list[0].a, that will only 
work statically if you can resolve that property. So you would 
have to either check the type or get out of the box and use some 
dynamic object


Re: Better debugging?

2021-10-03 Thread Tim via Digitalmars-d-learn

On Sunday, 3 October 2021 at 22:26:15 UTC, Imperatorn wrote:

On Sunday, 3 October 2021 at 22:21:45 UTC, Tim wrote:

[...]


You don't say which operating system you are using.
I usually use Visual D which works great imo.

If I use vscode I use the C++ debug extension (don't remember 
what it's called).


If I debug outside of the IDE I use WinDbg, also has source 
debug support if configured correctly.


If you are on Linux I'm not sure, but I would go for the C++ 
thing probably.


Apologies. I use KDE linux


Re: Better debugging?

2021-10-03 Thread Imperatorn via Digitalmars-d-learn

On Sunday, 3 October 2021 at 22:21:45 UTC, Tim wrote:

Hi all,

I am currently using GDB within VScode with the -gc DMD2 
compiler switch and my debugging is not amazing. Whenever I 
inspect a struct/object it just shows me the pointer rather 
than the object information and strings come up as a gross 
array of the characters. Does anybody happen to know whether 
LDB is better or how I can have a nicer debug environment?


Thanks in advance


You don't say which operating system you are using.
I usually use Visual D which works great imo.

If I use vscode I use the C++ debug extension (don't remember 
what it's called).


If I debug outside of the IDE I use WinDbg, also has source debug 
support if configured correctly.


If you are on Linux I'm not sure, but I would go for the C++ 
thing probably.


Python's list equivalent with std.variant?

2021-10-03 Thread rjkilpatrick via Digitalmars-d-learn

Hi.

I am trying to emulate Python's list in D, to store some derived 
classes


My initial thought was to use a dynamic array of `std.variant`s.
I've tried implementing something but seeing as we can only 
instantiate templates at compile-time, and I may only know the 
type at run-time, I'm not sure how to go about this:


```d
import std.stdio : writeln;
import std.variant;
import std.conv;

// Arbitrary super class
class SuperClass {
this() {
}
}

// Derived class with members
class DerivedClass : SuperClass {
public:
this(float a) {
this.a = a;
}
float a;
}

class OtherDerivedClass : SuperClass {}

void main() {
// When we use `SuperClass[] list;` here, we find 'a' is 
hidden by the base class

Variant[] list;

// Attempting to append derived class instances to list
list ~= new DerivedClass(1.0f);
list ~= new OtherDerivedClass;

list[0].a;
list[0].to!(get!(list[0].type)).a.writeln;
}
```

And we get the error(s):

```
onlineapp.d(27): Error: cannot append type 
`onlineapp.DerivedClass` to type `VariantN!32LU[]`
onlineapp.d(28): Error: cannot append type 
`onlineapp.OtherDerivedClass` to type `VariantN!32LU[]`
onlineapp.d(30): Error: no property `a` for type 
`std.variant.VariantN!32LU`
onlineapp.d(31): Error: template `object.get` does not match any 
template declaration

```

I would be grateful for any help


Better debugging?

2021-10-03 Thread Tim via Digitalmars-d-learn

Hi all,

I am currently using GDB within VScode with the -gc DMD2 compiler 
switch and my debugging is not amazing. Whenever I inspect a 
struct/object it just shows me the pointer rather than the object 
information and strings come up as a gross array of the 
characters. Does anybody happen to know whether LDB is better or 
how I can have a nicer debug environment?


Thanks in advance


Re: Regular Templates May Be `mixin`d?

2021-10-03 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 3 October 2021 at 03:34:19 UTC, surlymoor wrote:

Lord, I'm careless. Thanks.
So the difference between a `mixin template` and a regular one 
is that the former may only be used with a `mixin` statement?


Yes, exactly.