Re: Detect uninitialized class var access

2022-09-24 Thread Tejas via Digitalmars-d-learn

On Saturday, 24 September 2022 at 23:04:00 UTC, rassoc wrote:

On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote:

gdb --args ./your_program

and then it will tell you all the details you want to know 
about when this happens.


Thank you for your input, Adam.  Real shame that there's no 
built-in compiler solution and probably never will be according 
to [1]. :(


D's competition got stuff like this right:

```

crystal build foo.cr

In foo.cr:6:6

 6 | puts f.x
  ^
Error: read before assignment to local variable 'f'
```

[1] https://issues.dlang.org/show_bug.cgi?id=4595


AFAIK diagnostics like that are not possible because Walter 
doesn't want to implement full blown dataflow analysis in the 
compiler since it will slow down the compilation speed of dmd


Re: Detect uninitialized class var access

2022-09-24 Thread Salih Dincer via Digitalmars-d-learn

On Saturday, 24 September 2022 at 13:17:19 UTC, rassoc wrote:
Recently I refactored some old code of mine, now utilizing 
classes instead structs, and I got hit by an uninitialized 
variable access pattern similar to the simplified example below.


I think changing the structure will make things harder (I mean in 
general). I know most people will object;  but classes are 
useless.  Structs are enough for you for the rest of your life 😀


SDB@79


Re: Detect uninitialized class var access

2022-09-24 Thread rassoc via Digitalmars-d-learn

On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote:

gdb --args ./your_program

and then it will tell you all the details you want to know about when this 
happens.


Thank you for your input, Adam.  Real shame that there's no built-in compiler 
solution and probably never will be according to [1]. :(

D's competition got stuff like this right:

```

crystal build foo.cr

In foo.cr:6:6

 6 | puts f.x
  ^
Error: read before assignment to local variable 'f'
```

[1] https://issues.dlang.org/show_bug.cgi?id=4595


Anaphoric "macros"

2022-09-24 Thread Christian Köstlin via Digitalmars-d-learn

Hi,

I just stumbled upon anaphoric macros 
(https://en.wikipedia.org/wiki/Anaphoric_macro) in elisp. Seems that the 
dlang feature e.g. `map!"a*2"` is something similar to that, although I 
never read about it phrased like that before.


Kind regards,
Christian




Re: Detect uninitialized class var access

2022-09-24 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 24 September 2022 at 13:17:19 UTC, rassoc wrote:
just crashes with `Error: program killed by signal 11` on linux 
and nothing else.


gdb --args ./your_program

and then it will tell you all the details you want to know about 
when this happens.


strangly sometimes adding the -O option will cause dmd to catch 
simple cases at compile time but gdb is the general solution to 
track these down.


Detect uninitialized class var access

2022-09-24 Thread rassoc via Digitalmars-d-learn
Recently I refactored some old code of mine, now utilizing classes instead structs, and I got hit 
by an uninitialized variable access pattern similar to the simplified example below.


How can we easily spot this? What other switches and safeguards are there?

> dmd -dip1000 -debug -g -w

just crashes with `Error: program killed by signal 11` on linux and nothing 
else.

```d
import std;

@safe:

class Foo { int x; }

void main() {
Foo f; // = new Foo;
writeln(f.x);
}
```


Re: Serial communication library

2022-09-24 Thread Arjan via Digitalmars-d-learn

On Saturday, 24 September 2022 at 08:52:42 UTC, Imperatorn wrote:
On Thursday, 22 September 2022 at 12:05:00 UTC, Imperatorn 
wrote:

Hi guys!

What's the best/preferred library to use for serial 
communication (RS)?


Thanks 🍀


I will give onyx-serial a try


Also on code.dlang.org:

https://github.com/NCrashed/serial-port

Depending on you specific needs, using the platform/os api is 
also an option.




Re: Is this a new bug ?

2022-09-24 Thread Steven Schveighoffer via Digitalmars-d-learn

On Saturday, 24 September 2022 at 06:13:55 UTC, test123 wrote:
If so please report it for me to bugs platform. I can not 
register one.



```d
package {
version(TEST) {
static:
} else {
   __gshared:
}
uint  test = 0;
}
```


ldmd2  -betterC -vtls -c ./test.d

./test.d(7): `test` is thread local


static does nothing to module level variables.

Without attributes, test will be thread local. Your attributes 
are having no effect on the variable because they don’t apply 
outside the braces.


-Steve



Re: Is this a new bug ?

2022-09-24 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 24 September 2022 at 06:13:55 UTC, test123 wrote:

If so please report it for me to bugs platform.


This isn't a bug, the effect of keyword: things stop at the 
matching }.


(static if and version don't introduce a namespace scope, but 
they still follow this rule for the { colon: ... } blocks)


You need to duplicate your uint test2 variable inside those 
branches.


Re: Serial communication library

2022-09-24 Thread Imperatorn via Digitalmars-d-learn

On Thursday, 22 September 2022 at 12:05:00 UTC, Imperatorn wrote:

Hi guys!

What's the best/preferred library to use for serial 
communication (RS)?


Thanks 🍀


I will give onyx-serial a try


Re: Is this a new bug ?

2022-09-24 Thread test123 via Digitalmars-d-learn
On Saturday, 24 September 2022 at 07:11:12 UTC, rikki cattermole 
wrote:

```d
version(all) {
__gshared:
uint test2;
}

uint test;
```

Output with -vtls:

```
Up to  2.079.1: Success with output: onlineapp.d(9): test 
is thread local
Since  2.080.1: Success with output: onlineapp.d(9): `test` 
is thread local

```

Looks fine to me.


I think it should not be thread local.


Re: Is this a new bug ?

2022-09-24 Thread rikki cattermole via Digitalmars-d-learn

```d
version(all) {
__gshared:
uint test2;
}

uint test;
```

Output with -vtls:

```
Up to  2.079.1: Success with output: onlineapp.d(9): test is thread 
local
Since  2.080.1: Success with output: onlineapp.d(9): `test` is 
thread local

```

Looks fine to me.



Passing D reimplementation of C++ template as argument to a C++ function

2022-09-24 Thread Gregor Mückl via Digitalmars-d-learn

Hi!

I have a D template struct that reimplements a C++ class template 
with identical memory layout for a set of types that matter to 
me. Now, I want to use some C++ functions and classes that use 
these template instances, from D. For that, I want to 
purposefully alias the D and C++ types. However, with the C++ 
type being templated, I don't know how to name that type in a 
extern declaration in D.


Consider, the following simplified example:

```cpp
// C++ template class (from third party library)
template
class Foo {
  // X is made part of the type as a behavior switch, but is 
never instanced

  T a, b;
};

using FooFloat = Foo;

// own C++ function:
void bar(FooFloat f);

```

```d
// D template struct
struct Foo(T) {
  T a, b;
}

alias FooFloat = Foo!float;

// How do I correctly bind the C++ function bar here?

```

Ideally, I want to pass an instance of the D implementation of 
FooFloat without introducing additional wrapper functions.


I know that the whole thing does sound quite mad at first glance 
and screams "bad idea!", but there is a reason why I want to try 
to tread into this minefield.