Re: Language server

2022-04-27 Thread Alain De Vos via Digitalmars-d-learn

I have not tested yet but i found two interesting links,

https://github.com/Pure-D/serve-d/blob/master/editor-vim.md

https://github.com/neoclide/coc.nvim



Re: Problem with GC - linking C++ & D (with gdc)

2022-04-27 Thread dangbinghoo via Digitalmars-d-learn

On Tuesday, 26 April 2022 at 13:36:19 UTC, Claude wrote:

On Tuesday, 26 April 2022 at 12:49:21 UTC, Alain De Vos wrote:
---

I also encountered problems while I was trying to use CTFE only 
functions (using betterC so I don't have to link 
phobos/D-runtime).


However, if those functions use the GC for instance (like 
appending a dynamic-array), it will require me to link 
D-runtime, whereas I only use them at compile-time. So I'm a 
bit confused... I'll try and get more information and reduce a 
code sample.


dont't use `new` if you mean using betterC, `new` is based on GC. 
with betterC, you should using `malloc/free`


reference info: 
https://dlang.org/spec/cpp_interface.html#memory-allocation




Re: Library for image editing and text insertion

2022-04-27 Thread Adam D Ruppe via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 17:07:54 UTC, matheus wrote:
I know about Adam Ruppe's work, I already used his terminal.d, 
but I think that unfortunately most people don't and I think it 
should be announced more in these parts.


tbh sometimes i just don't feel like answering messages. even 
this one, i was like "i can do that easily, but do i want to 
support another user?"


but meh i had a few mins to kill anyway


For me arsd is for D what stb is for C.


Note that a few of those modules are stb ports over to D.

I wrote the png and bmp modules myself, for example, but the jpeg 
is a port from C (that port done by ketmar on irc) and the ttf 
one is stb_ttf ported to D then with a little wrapper struct 
pasted on top.


One thing I'm kinda proud of though is my OperatingSystemFont 
class in simpledisplay which can load a font and then pass it 
into the ttf thing and/or the nanovega module for all kinds of 
custom effects. You don't necessarily have to package your own 
ttf files!


Re: generic function instance without call

2022-04-27 Thread user1234 via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 17:22:14 UTC, vit wrote:
This work for types but not for attributes like `scope`, 
`return` and `auto ref`.


Oh sorry... auto ref... I totally forgot [this old bug]

[this old bug]: https://issues.dlang.org/show_bug.cgi?id=8204


Re: generic function instance without call

2022-04-27 Thread vit via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 16:03:32 UTC, user1234 wrote:

On Wednesday, 27 April 2022 at 15:23:26 UTC, vit wrote:
Hi, is it possible to get address of generic function instance 
for specified arguments without calling the function?


Example:


```d

auto foo(alias fn, Args...)(auto ref Args args){
///return function/delegate type of `fn` for arguments 
`args`

}

void main(){

long x;

auto fn = foo!((a, b) => true)(new int(42), x);

static assert(is(typeof(fn) == bool function(int*, scope 
ref long)@safe pure nothrow @nogc ));

}

```


yeah sure; declare an alias that (fully) specialize the generic 
func and take the address using the alias Identifier.


This work for types but not for attributes like `scope`, `return` 
and `auto ref`.





Re: Library for image editing and text insertion

2022-04-27 Thread Alexander Zhirov via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 17:07:54 UTC, matheus wrote:

I think in the first time running any D compiler, it should 
blink in the terminal in Yellow/Pink or whatever color you 
like, and show some info like: Are you starting a new project 
any need some libs? Do you know about arsd? If not then go to: 
https://github.com/adamdruppe/arsd. :)


Hahaha  This is the truth! I totally agree!


Re: Library for image editing and text insertion

2022-04-27 Thread matheus via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 00:03:25 UTC, Adam Ruppe wrote:

...


I know about Adam Ruppe's work, I already used his terminal.d, 
but I think that unfortunately most people don't and I think it 
should be announced more in these parts. For me arsd is for D 
what stb is for C.


I think in the first time running any D compiler, it should blink 
in the terminal in Yellow/Pink or whatever color you like, and 
show some info like: Are you starting a new project any need some 
libs? Do you know about arsd? If not then go to: 
https://github.com/adamdruppe/arsd. :)


matheus.


Re: Library for image editing and text insertion

2022-04-27 Thread Alexander Zhirov via Digitalmars-d-learn
On Wednesday, 27 April 2022 at 16:37:21 UTC, Alexander Zhirov 
wrote:

On Wednesday, 27 April 2022 at 16:07:53 UTC, Adam D Ruppe wrote:


How small did it get?

```sh
dmd: 3136896 byte
ldc:  223952 byte
```


but uses libraries ldc-shared.so

```sh
linux-vdso.so.1 (0x7ffef5d2d000)
libphobos2-ldc-shared.so.99 => 
/usr/lib64/libphobos2-ldc-shared.so.99 (0x7f667e9ee000)
libdruntime-ldc-shared.so.99 => 
/usr/lib64/libdruntime-ldc-shared.so.99 (0x7f667e8ac000)

libm.so.6 => /usr/lib64/libm.so.6 (0x7f667e7c5000)
libgcc_s.so.1 => /usr/lib64/libgcc_s.so.1 
(0x7f667e7a9000)

libc.so.6 => /usr/lib64/libc.so.6 (0x7f667e589000)
/usr/lib64/ld-linux-x86-64.so.2 (0x7f667eebe000)
```

And if without using dynamic libraries, it weighs `1219776` bytes

```sh
linux-vdso.so.1 (0x7ffca9ad)
librt.so.1 => /lib/librt.so.1 (0x7f6240fc4000)
libdl.so.2 => /lib/libdl.so.2 (0x7f6240fbe000)
libpthread.so.0 => /lib/libpthread.so.0 (0x7f6240f9d000)
libm.so.6 => /lib/libm.so.6 (0x7f6240e58000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x7f6240e3e000)
libc.so.6 => /lib/libc.so.6 (0x7f6240c74000)
	/lib64/ld-linux-x86-64.so.2 => /lib/ld-linux-x86-64.so.2 
(0x7f6240ff7000)

```


Re: Library for image editing and text insertion

2022-04-27 Thread Alexander Zhirov via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 16:07:53 UTC, Adam D Ruppe wrote:


How small did it get?

```sh
dmd: 3136896 byte
ldc:  223952 byte
```

And with my libs if you import the other ones like `arsd.png` 
or `arsd.jpeg` directly instead of `arsd.image` that MIGHT help 
trim it down by removing support for other formats.  I'm not 
sure though, none of them are especially big but it might add 
up.


Yes, just now, on your advice, I did so. The size has become a 
little smaller! 





Re: Library for image editing and text insertion

2022-04-27 Thread Adam D Ruppe via Digitalmars-d-learn
On Wednesday, 27 April 2022 at 14:40:59 UTC, Alexander Zhirov 
wrote:

Gorgeous! LDC has compressed my code at times!


How small did it get?

And with my libs if you import the other ones like `arsd.png` or 
`arsd.jpeg` directly instead of `arsd.image` that MIGHT help trim 
it down by removing support for other formats.  I'm not sure 
though, none of them are especially big but it might add up.


Re: generic function instance without call

2022-04-27 Thread user1234 via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 15:23:26 UTC, vit wrote:
Hi, is it possible to get address of generic function instance 
for specified arguments without calling the function?


Example:


```d

auto foo(alias fn, Args...)(auto ref Args args){
///return function/delegate type of `fn` for arguments 
`args`

}

void main(){

long x;

auto fn = foo!((a, b) => true)(new int(42), x);

static assert(is(typeof(fn) == bool function(int*, scope 
ref long)@safe pure nothrow @nogc ));

}

```


yeah sure; declare an alias that (fully) specialize the generic 
func and take the address using the alias Identifier.


Re: CTFE and BetterC compatibility

2022-04-27 Thread Adam D Ruppe via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 14:21:15 UTC, Claude wrote:
The operation requiring the D-runtime is appending the array, 
but it should **only** be done at compile-time.


In that case, you want to prove to the compiler it is only called 
at compile time by encapsulating the function inside a template 
or defining it immediately where it is called.


Delete the stand-alone function `parse` and instead call it like 
this:


```
// define it
enum Data parsedData = function Data (string str) pure
{
Data data;

while (str.length != 0)
{
// Skip spaces
while (str[0] == ' ')
str = str[1 .. $];

// Parse single digit integer
data.digits ~= parseDigit(str[0]);

// Consume digit
str = str[1 .. $];
}

return data;
} ("5 4 2 6 9"); // and call it in the same place
```



Or if you want it to be independently defined still, you can 
define it inside a helper template but you'd have to return a 
basic type instead of Data, so I think calling it immediately is 
what you want to do here.



There was going to be a ctfe-only thing you could put in the 
function so the compiler doesn't try to generate the runtime 
version, but this got killed due to internal D politics. A pity.


Re: generic function instance without call

2022-04-27 Thread vit via Digitalmars-d-learn

Fix:
```d
auto fn = foo!((a, ref b) => true)(new int(42), x);
```



generic function instance without call

2022-04-27 Thread vit via Digitalmars-d-learn
Hi, is it possible to get address of generic function instance 
for specified arguments without calling the function?


Example:


```d

auto foo(alias fn, Args...)(auto ref Args args){
///return function/delegate type of `fn` for arguments `args`
}

void main(){

long x;

auto fn = foo!((a, b) => true)(new int(42), x);

static assert(is(typeof(fn) == bool function(int*, scope ref 
long)@safe pure nothrow @nogc ));

}

```


Re: CTFE and BetterC compatibility

2022-04-27 Thread Claude via Digitalmars-d-learn
On Wednesday, 27 April 2022 at 14:34:27 UTC, rikki cattermole 
wrote:

This works:


Cool, thanks.

Unfortunately, with that implementation, I need to know the 
maximum size for the array. It works for that particular example, 
but in the context of an XML file analysis, it's a bit awkward.


Regarding my comment above, I tried using cork functions for 
missing symbols: it also works! However the linker does not 
optimize those functions out (I see the symbols in the executable 
binary)...


Re: A template construct like using()

2022-04-27 Thread user1234 via Digitalmars-d-learn

On Tuesday, 26 April 2022 at 21:33:43 UTC, Chris Katko wrote:
I swear I asked something like this before years ago but it 
doesn't show up in my previous forum posts.


I'm looking for a construct that mimics using(var)/with(var)

```D
bitmap* b;

draw_with(b)
  {
  draw_pixel(red, 16, 16); //draw red pixel to bitmap b (b is 
implied above)

  }
```

But the code ends up being:
```D
bitmap* b;

set_target_bitmap(b); //entry code
draw_pixel(red, 16, 16); // body
set_target_bitmap(original_target); // exit code
```

The essence is wrapping something the code up in a kind of 
RAII-like entry and exit code that references a given target 
variable.


Perhaps a mixin is what I'm looking for?


assuming `set_target_bitmap` returns the previous target :

```d
struct Bitmap
{
}

struct PushPopBitmap
{
Bitmap* bitmap;
Bitmap* old;
alias bitmap this;

this(Bitmap* bmp) {
old = set_target_bitmap(bitmap = bmp);
}

~this() {
set_target_bitmap(old);
}
}

Bitmap* set_target_bitmap(Bitmap* bmp);
void draw_pixel();

void main()
{
Bitmap* bmp;
with (PushPopBitmap(bmp)) draw_pixel();
}
```

At the end of the WithStatement block that restores the previous 
context.


Re: Library for image editing and text insertion

2022-04-27 Thread Alexander Zhirov via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 11:59:20 UTC, Bastiaan Veelo wrote:


Or use LDC.


Gorgeous! LDC has compressed my code at times! Thanks again to 
everyone for help! Special thanks to **Adam Ruppe**


Re: CTFE and BetterC compatibility

2022-04-27 Thread Claude via Digitalmars-d-learn
On Wednesday, 27 April 2022 at 14:27:43 UTC, Stanislav Blinov 
wrote:
This is a long-standing pain point with BetterC (see 
https://issues.dlang.org/show_bug.cgi?id=19268).


That's what I was afraid of... Thanks for the link to the 
bug-report.


On Wednesday, 27 April 2022 at 14:27:43 UTC, Stanislav Blinov 
wrote:
When not using BetterC, but not linking against druntime 
either, you have to provide your own implementation for those 
functions. This is e.g. so you can replace druntime with your 
own version.


Yeah... The problem is that there will a lot of those functions 
to define (for a whole XML parser). I suppose I can use cork 
functions with empty bodies??


I will check if the linker optimize them out...


Re: CTFE and BetterC compatibility

2022-04-27 Thread rikki cattermole via Digitalmars-d-learn

This works:

```d
struct Data
{
int[] digits;
}

int parseDigit(char c) pure
{
return c - '0';
}

Data parse(string str) pure
{
Data data;

if (__ctfe) {
size_t used;
data.digits.length = str.length;

while (str.length != 0)
{
// Skip spaces
while (str[0] == ' ')
str = str[1 .. $];

// Parse single digit integer
data.digits[used++] = parseDigit(str[0]);

// Consume digit
str = str[1 .. $];
}

data.digits = data.digits[0 .. used];
}

return data;
}

enum Data parsedData = parse("5 4 2 6 9");

extern(C) int main()
{
pragma(msg, "First digit=", parsedData.digits[0]);
return 0;
}
```


Re: CTFE and BetterC compatibility

2022-04-27 Thread Stanislav Blinov via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 14:21:15 UTC, Claude wrote:

This is a long-standing pain point with BetterC (see 
https://issues.dlang.org/show_bug.cgi?id=19268).


As for this:

If I compile without the BetterC switch, compilation actually 
works but I'll have some linker issues:

```
$ gcc test.d -o test
First digit=5
/tmp/ccuPwjdv.o : In function 
« _D5test5parseFNaAyaZS5test4Data » :

test.d:(.text+0x137) : undefined reference to « _d_arraybounds »
test.d:(.text+0x183) : undefined reference to « _d_arraybounds »

etc...
```


When not using BetterC, but not linking against druntime either, 
you have to provide your own implementation for those functions. 
This is e.g. so you can replace druntime with your own version.


Re: CTFE and BetterC compatibility

2022-04-27 Thread Andrea Fontana via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 14:21:15 UTC, Claude wrote:

data.digits ~= parseDigit(str[0]);


Dynamic arrays are not supported using -betterC

Andrea





CTFE and BetterC compatibility

2022-04-27 Thread Claude via Digitalmars-d-learn

Hello,

I want to make a SAX XML parser in D that I could both use at 
run-time or compile-time.


Also when I use it at compile-time, I would like to use BetterC 
so I don't have to link D-runtime.


But I have some compilation problems. I use GDC (GCC 9.4.0).

Here's a reduced sample code:

```
struct Data
{
int[] digits;
}

int parseDigit(char c) pure
{
return c - '0';
}

Data parse(string str) pure
{
Data data;

while (str.length != 0)
{
// Skip spaces
while (str[0] == ' ')
str = str[1 .. $];

// Parse single digit integer
data.digits ~= parseDigit(str[0]);

// Consume digit
str = str[1 .. $];
}

return data;
}

enum Data parsedData = parse("5 4 2 6 9");

extern(C) int main()
{
pragma(msg, "First digit=", parsedData.digits[0]);
return 0;
}
```

If I compile and link against D-runtime, it works:
```
$ gcc test.d -lgdruntime -o test
First digit=5
```

If I compile with BetterC (no D-runtime for GDC), I get a 
compilation error about RTTI:

```
$ gcc test.d -fno-druntime -o test
test.d: In function ‘parse’:
test.d:25:21: error: ‘object.TypeInfo’ cannot be used with 
-fno-rtti

   25 | data.digits ~= parseDigit(str[0]);
  | ^
```

If I compile without the BetterC switch, compilation actually 
works but I'll have some linker issues:

```
$ gcc test.d -o test
First digit=5
/tmp/ccuPwjdv.o : In function 
« _D5test5parseFNaAyaZS5test4Data » :

test.d:(.text+0x137) : undefined reference to « _d_arraybounds »
test.d:(.text+0x183) : undefined reference to « _d_arraybounds »

etc...
```

The operation requiring the D-runtime is appending the array, but 
it should **only** be done at compile-time.


I don't understand why it requires to link against the D-runtime 
whereas it only needs it at compilation-time (and the compilation 
and CTFE interpretation works, as we can see in the last example).


Is there a way to force the compiler to not emit any object ode 
for those functions?


Or am I missing something?

Regards,

Claude


Re: Library for image editing and text insertion

2022-04-27 Thread Bastiaan Veelo via Digitalmars-d-learn
On Wednesday, 27 April 2022 at 09:27:24 UTC, Alexander Zhirov 
wrote:
Now I would like to reduce the size of the executable file and 
it would be great at all!


Try the `-release` option to `dmd`. Or use LDC.

-- Bastiaan.


Re: Language server

2022-04-27 Thread Bastiaan Veelo via Digitalmars-d-learn

On Tuesday, 26 April 2022 at 18:15:57 UTC, Alain De Vos wrote:
In a perfect world there would be someone uploading a youtube 
video how to implement

neovim with a dlang language-server.
With function-completions-help where hints are given about the 
functions and libraries.

If anyone could do this , this would be nice to have.


I'm not a vim user, but there is this: 
https://wiki.dlang.org/D_in_Vim


If that is what you needed to get what you want, maybe make a 
YouTube video about it ;-)


-- Bastiaan.


Re: Library for image editing and text insertion

2022-04-27 Thread Alexander Zhirov via Digitalmars-d-learn
On Wednesday, 27 April 2022 at 08:29:27 UTC, Alexander Zhirov 
wrote:
On Wednesday, 27 April 2022 at 07:42:31 UTC, Alexander Zhirov 
wrote:

On Wednesday, 27 April 2022 at 00:03:25 UTC, Adam Ruppe wrote:

Sample code would be:

1) How to write to jpeg correctly?


That's how I managed to write to jpeg:

```d
writeJpeg("dst.jpg", tci);
```

Now I would like to reduce the size of the executable file and it 
would be great at all!





Re: Library for image editing and text insertion

2022-04-27 Thread Alexander Zhirov via Digitalmars-d-learn
On Wednesday, 27 April 2022 at 07:42:31 UTC, Alexander Zhirov 
wrote:

On Wednesday, 27 April 2022 at 00:03:25 UTC, Adam Ruppe wrote:

Sample code would be:


My inattention, it was necessary to write like this:

```sh
dmd app.d -i arsd/image.d
```

Now everything is compiled and really the way I need it! Thank 
you very much!


There are two more questions left:
1) How to write to jpeg correctly?
2) How can I reduce the size of the executable file? And then 3 
megabytes is too much for such a utility.




Re: Library for image editing and text insertion

2022-04-27 Thread Dennis via Digitalmars-d-learn
On Wednesday, 27 April 2022 at 07:42:31 UTC, Alexander Zhirov 
wrote:

```d
~/programming/d/pic $ dmd app.d
```


Try passing the `-i` flag: `dmd -i app.d`. This way, imported 
modules are actually compiled and linked too. Currently it looks 
like you import arsd, but then don't link the library, so it 
complains about undefined references to functions in arsd.


Re: Library for image editing and text insertion

2022-04-27 Thread Alexander Zhirov via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 00:03:25 UTC, Adam Ruppe wrote:


Sample code would be:


Ego have problems cum scribendarum hic quoque, fortasse opus est 
dare aliquid aliud, cum componendis?


```d
~/programming/d/pic $ dmd app.d
/usr/bin/ld: app.o:(.data.rel.ro+0x10): undefined reference 
«_D4arsd5image12__ModuleInfoZ»
/usr/bin/ld: app.o:(.data.rel.ro+0x30): undefined reference 
«_D4arsd3png12__ModuleInfoZ»

/usr/bin/ld: app.o: в функции «_Dmain»:
app.d:(.text._Dmain[_Dmain]+0xcb): undefined reference 
«_D4arsd3ttf7TtfFont6__ctorMFNcIAhZSQBhQBfQBe»
/usr/bin/ld: app.d:(.text._Dmain[_Dmain]+0xfb): undefined 
reference «_D4arsd3ttf7TtfFont12renderStringMFIAaiJiJiZAh»
/usr/bin/ld: app.d:(.text._Dmain[_Dmain]+0x297): undefined 
reference «_D4arsd5color5Color6__ctorMFNaNbNcNiNfZSQBqQBoQBl»
/usr/bin/ld: app.d:(.text._Dmain[_Dmain]+0x2a2): undefined 
reference 
«_D4arsd5color5Color10alphaBlendMxFNaNbNiNeSQBpQBnQBkZQl»
/usr/bin/ld: app.d:(.text._Dmain[_Dmain]+0x31a): undefined 
reference «_D4arsd3png8writePngFAyaCQx5color11MemoryImageZv»
/usr/bin/ld: app.o: в функции 
«_D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage»:

app.d:(.text._D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage[_D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage]+0x1f):
 undefined reference 
«_D4arsd5image29guessImageFormatFromExtensionFAxaZEQBwQBu15ImageFileFormat»
/usr/bin/ld: 
app.d:(.text._D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage[_D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage]+0x1d8): undefined reference «_D4arsd5image19loadImageFromMemoryFAxvZCQBm5color11MemoryImage»
/usr/bin/ld: 
app.d:(.text._D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage[_D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage]+0x297): undefined reference «_D4arsd3png7readPngFAyaZCQx5color11MemoryImage»
/usr/bin/ld: 
app.d:(.text._D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage[_D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage]+0x2a9): undefined reference «_D4arsd3bmp7readBmpFAyaZCQx5color11MemoryImage»
/usr/bin/ld: 
app.d:(.text._D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage[_D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage]+0x2bb): undefined reference «_D4arsd4jpeg8readJpegFAxaZCQz5color11MemoryImage»
/usr/bin/ld: 
app.d:(.text._D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage[_D4arsd5image__T17loadImageFromFileHTAyaZQzFQhZCQBu5color11MemoryImage]+0x341): undefined reference «_D4arsd5image7readSvgFAyaZCQz5color11MemoryImage»
/usr/bin/ld: app.o: в функции 
«_D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage»:

app.d:(.text._D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage[_D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage]+0x9ab):
 undefined reference «_D4arsd5color14TrueColorImage7__ClassZ»
/usr/bin/ld: 
app.d:(.text._D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage[_D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage]+0x9c6): undefined reference «_D4arsd5color14TrueColorImage6__ctorMFNaNbNfiiZCQBuQBsQBp»
/usr/bin/ld: 
app.d:(.text._D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage[_D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage]+0xa01): undefined reference «_D4arsd5color14TrueColorImage4Data6colorsMNgFNaNbNdNiNeZANgSQCgQCe5Color»
/usr/bin/ld: 
app.d:(.text._D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage[_D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage]+0xc12): undefined reference «_D4arsd5color14TrueColorImage4Data6colorsMNgFNaNbNdNiNeZANgSQCgQCe5Color»
/usr/bin/ld: 
app.d:(.text._D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage[_D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage]+0xd02): undefined reference «_D4arsd5color14TrueColorImage4Data6colorsMNgFNaNbNdNiNeZANgSQCgQCe5Color»
/usr/bin/ld: 
app.d:(.text._D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage[_D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZCQCf5color11MemoryImage]+0xd5e): undefined reference «_D4arsd5color14TrueColorImage4Data6colorsMNgFNaNbNdNiNeZANgSQCgQCe5Color»
/usr/bin/ld: app.o: в функции 
«_D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZ__T11readColor16Vbi0ZQsMFMDFZhZSQDk5color5Color»:

app.d:(.text._D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZ__T11readColor16Vbi0ZQsMFMDFZhZSQDk5color5Color[_D4arsd5targa__T11loadTgaImplTS3std5stdio4FileZQBfFKQwAxaZ__T11readColor16Vbi0ZQsMFMDFZhZSQDk5color5Color]+0xab):
 undefined reference «_D4arsd5color5Color6__ctorMFNaNbNcNiNfZSQBqQBoQBl»
/usr/bin/ld: app.o: в функции 

Re: Reference counting example

2022-04-27 Thread vit via Digitalmars-d-learn

On Tuesday, 26 April 2022 at 23:33:28 UTC, cc wrote:

On Tuesday, 26 April 2022 at 22:16:01 UTC, cc wrote:

Test application:


I should point out that all this stuff with saving refcounted 
things to arrays and so on is extremely untested and 
experimental


One problem I'm seeing is the inability for a refcounted class 
to pass itself to another function, since the class internals 
don't see the struct wrapper.. you can pass the naked object 
reference itself, and hope the reference doesn't get saved 
otherwise there's your dangling pointer, but then you also have 
the problem of inconsistent method declarations, with some 
things taking Foo and others taking RefCounted!Foo etc...


Every night I pray for a `refcounted` keyword.  Wouldn't 
something like `auto foo = new refcount Foo();` be nice?  Then 
every class that deals with the objects could continue to be 
allocator-agnostic... definitely not a trivial change though.


Yor code has antoher big problem. Class Animal has 
@safe/pure/nothrow/@nogc destruction but class Cow has @system 
destructor. When you assign RC!Cow to RC!Animal you can have 
@safe @nogc ... function call @system destructor of Cow.



I have library with check this kind of error for you 
(https://code.dlang.org/packages/btl).


If you need aliasing (your case) or weak pointers then try it.

```d

import std.stdio;
import core.memory;
import core.lifetime;

import btl.autoptr;
import btl.vector;

alias ARRAY = Vector;
alias RC = RcPtr;

class Animal {
void speak() {
writeln("Animal: ???");
}

~this()@system{}   //without this code doesnt compile
}
class Cow : Animal {
ARRAY!(RC!Animal) friends; // Amazingly, this works, as 
long as the array elem type is NOT the same as RC!(this class)
// otherwise we get a 
forwarding error

int x;
this() { writefln("[Cow]"); }
this(int x) { this.x = x; writefln("[Cow %s]", x); }
~this() { writefln("[/Cow %s]", x); }
override void speak() {
writefln("Cow#%s: Moo.", x);
}
}

class Farm {
ARRAY!(RC!Cow) animals;
//this() {}
this(int) { writeln("[Farm]"); }
~this() {
writeln("[~Farm]");
animals.clear();
writeln("[/Farm]");
}

void pet(RC!Animal animal) {
writefln("Farm: The %s says...", animal);
animal.get.speak;
}

}

void main() {
auto used = GC.stats.usedSize;
scope(exit) assert(GC.stats.usedSize == used); // GC is 
not touched!

{
assert(RC!Cow.make().get.x == 0);
assert(RC!Cow.make(99).get.x == 99);
}

RC!Animal other;

auto farm = RC!Farm.make(1);
{
auto cow = RC!Cow.make(1);
farm.get.animals ~= cow;
farm.get.animals ~= RC!Cow.make(2);
other = farm.get.animals[1];
auto cowGoesOutOfScope = RC!Cow.make(70);
}
writeln("out, should have seen Cow#70's dtor");

farm.get.animals[0] = farm.get.animals[1];
writeln("animals[0] (Cow#1) just got overwritten so we 
should have seen its dtor");


farm.get.animals ~= RC!Cow.make(3);

farm.get.pet(other);
other = null;

farm = null;

writeln("done");
}
```