Re: Write binary data as a file

2022-08-02 Thread Alexander Zhirov via Digitalmars-d-learn

On Tuesday, 2 August 2022 at 15:30:13 UTC, Adam D Ruppe wrote:
On Tuesday, 2 August 2022 at 11:10:27 UTC, Alexander Zhirov 
wrote:

As a result, I get only a set of text data.


my database layer is doing to!string(that_ubyte) which is 
wrong. gonna see about pushing a fix


It's decided! After the fix, everything works! Thank you very 
much!


Re: Request assistance resolving linker error: Undefined symbol(s) for architecture x86_64

2022-08-02 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Aug 03, 2022 at 04:28:57AM +, anonymouse via Digitalmars-d-learn 
wrote:
> How do I go about tracking down what's causing the following error:
> 
> ```
> Undefined symbols for architecture x86_64:
>   "__D3std8internal6memory12__ModuleInfoZ", referenced from:
>   __D3loxQe12__ModuleInfoZ in dlux.o
> ld: symbol(s) not found for architecture x86_64
> clang: error: linker command failed with exit code 1 (use -v to see
> invocation)
> ```
> 
> I'm not explicitly calling anything in std.internal.memory so not sure
> how to resolve. Thanks.
[...]

This is often a sign of version mismatch between libraries and compiler.
Did you recently upgrade your compiler?  Did you accidentally install
two versions of the standard library and the new compiler is mistakenly
picking up the old library?

Maybe try also recompiling your project from clean slate just in case
your build process is picking up stale binaries for whatever reason. If
you have object files compiled with the old version of the compiler
still lying around, and they get picked up when compiling with the new
compiler, it would cause link errors like the above.


T

-- 
Winners never quit, quitters never win. But those who never quit AND never win 
are idiots.


Request assistance resolving linker error: Undefined symbol(s) for architecture x86_64

2022-08-02 Thread anonymouse via Digitalmars-d-learn
How do I go about tracking down what's causing the following 
error:


```
Undefined symbols for architecture x86_64:
  "__D3std8internal6memory12__ModuleInfoZ", referenced from:
  __D3loxQe12__ModuleInfoZ in dlux.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to 
see invocation)

```

I'm not explicitly calling anything in std.internal.memory so not 
sure how to resolve. Thanks.


--anonymouse


How to find all modules in a package?

2022-08-02 Thread Domain via Digitalmars-d-learn
I want to find out all public functions in all modules in a 
package. Can I do that at compile time?


Re: A look inside "filter" function defintion

2022-08-02 Thread frame via Digitalmars-d-learn

On Tuesday, 2 August 2022 at 14:58:52 UTC, pascal111 wrote:


Maybe this helps:

A template can be seen as a static struct too, so you can 
access its members with the scope operator "." and if there is 
only one member of the same name as the template ifself, the 
compiler auto completes it to this member.




I guess you mean if we will understand the templates concept as 
a static struct that the template is like the struct or - 
"records" in Pascal - that preserve its members values each 
time of calling through the runtime because they are "static". 
Isn't like that or what do you mean?


I don't know anything about Pascal - I can only say that the 
template scope can be seen like that from a logical point of view.


A function defined in a template is a static member of that 
template but not necessarily static context in runtime. It 
depends on the usage and scope.


But most library functions in Phobos are very simple like that 
and you have just to know that most functions are templates. They 
just may not require template arguments to be supplied and others 
do.




Re: Breaking ";" rule with lambda functions

2022-08-02 Thread pascal111 via Digitalmars-d-learn

On Tuesday, 2 August 2022 at 11:27:05 UTC, Andrey Zherikov wrote:

On Monday, 1 August 2022 at 14:15:31 UTC, pascal111 wrote:

[...]


TBH I don't find lambda syntax strange - it's pretty nice and 
there are two forms (unlike in C++): short one (`a => a > 0`) 
and long one (`(a) { return a > 0; }`).


[...]


Maybe I'd wrong beliefs about lambda function. It's already in 
C++, so it's a traditional feature but the problem is that I 
didn't use it before because I didn't study C++ yet.


Re: Write binary data as a file

2022-08-02 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 2 August 2022 at 11:10:27 UTC, Alexander Zhirov wrote:

As a result, I get only a set of text data.


my database layer is doing to!string(that_ubyte) which is wrong. 
gonna see about pushing a fix


Re: Write binary data as a file

2022-08-02 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Aug 02, 2022 at 11:10:27AM +, Alexander Zhirov via 
Digitalmars-d-learn wrote:
[...]
> ```d
> auto result = db.query("select tcxs.settings_file as dbfile from
> amts.t_client_xrdp_settings tcxs where tcxs.pid_client = ?", id);
> ubyte[] bytes = cast(ubyte[])result.front()["dbfile"];
> write("newFile", bytes);
> ```

Don't use `write` for binary data. Use instead File.rawWrite:

ubyte[] data = ...;
File f = File("dbfile", "w");
f.rawWrite(data[]);


T

-- 
This is a tpyo.


Re: A look inside "filter" function defintion

2022-08-02 Thread pascal111 via Digitalmars-d-learn

On Tuesday, 2 August 2022 at 14:24:50 UTC, frame wrote:

On Tuesday, 2 August 2022 at 12:39:41 UTC, pascal111 wrote:

Instantiation seems some complicated to me. I read "If a 
template contains members whose name is the same as the 
template identifier then these members are assumed to be 
referred to in a template instantiation:" in the provided 
link, but I'm still stuck. Do you have a down-to-earth example 
for beginners to understand this concept?


There are obvious examples direct below.

Where do you get stuck?

Maybe this helps:

A template can be seen as a static struct too, so you can 
access its members with the scope operator "." and if there is 
only one member of the same name as the template ifself, the 
compiler auto completes it to this member.




I guess you mean if we will understand the templates concept as a 
static struct that the template is like the struct or - "records" 
in Pascal - that preserve its members values each time of calling 
through the runtime because they are "static". Isn't like that or 
what do you mean?





Re: A look inside "filter" function defintion

2022-08-02 Thread frame via Digitalmars-d-learn

On Tuesday, 2 August 2022 at 12:39:41 UTC, pascal111 wrote:

Instantiation seems some complicated to me. I read "If a 
template contains members whose name is the same as the 
template identifier then these members are assumed to be 
referred to in a template instantiation:" in the provided link, 
but I'm still stuck. Do you have a down-to-earth example for 
beginners to understand this concept?


There are obvious examples direct below.

Where do you get stuck?

Maybe this helps:

A template can be seen as a static struct too, so you can access 
its members with the scope operator "." and if there is only one 
member of the same name as the template ifself, the compiler auto 
completes it to this member.


You could always write out 
`name!(someTemplateArg).name(someRuntimeArg)`, we just prefer the 
short syntax `name!(someTemplateArg)(someRuntimeArg)` or 
`name(someRuntimeArg)` (if there are no template arguments or it 
could be auto deducted by the compiler). The last syntax shows 
that it can be called as a normal function while in fact it's a 
template.


Re: A look inside "filter" function defintion

2022-08-02 Thread Andrey Zherikov via Digitalmars-d-learn

On Monday, 1 August 2022 at 23:35:13 UTC, pascal111 wrote:

I think this line needs explanation:

'''D
return FilterResult!(unaryFun!predicate, Range)(range);
'''



Create an object of type `FilterResult!(unaryFun!predicate, 
Range)` by passing `range` to its ctor, then return that object.


Re: A look inside "filter" function defintion

2022-08-02 Thread pascal111 via Digitalmars-d-learn

On Tuesday, 2 August 2022 at 04:06:30 UTC, frame wrote:

On Monday, 1 August 2022 at 23:35:13 UTC, pascal111 wrote:
This is the definition of "filter" function, and I think it 
called itself within its definition. I'm guessing how it works?


It's a template that defines the function called "Eponymous 
Templates":

https://dlang.org/spec/template.html#implicit_template_properties

A template generates code, it cannot be called, only 
instantiated.


The common syntax is just a shortcut for using it. Otherwise 
you would need to write `filter!(a => a > 0).filter([1, -1, 2, 
0, -3])`. Like UFCS, some magic the compiler does for you.


Instantiation seems some complicated to me. I read "If a template 
contains members whose name is the same as the template 
identifier then these members are assumed to be referred to in a 
template instantiation:" in the provided link, but I'm still 
stuck. Do you have a down-to-earth example for beginners to 
understand this concept?


Re: Breaking ";" rule with lambda functions

2022-08-02 Thread Andrey Zherikov via Digitalmars-d-learn

On Monday, 1 August 2022 at 14:15:31 UTC, pascal111 wrote:
We all know the strange syntax of lambda function within filter 
algorithm like "auto r = chain(a, b).filter!(a => a > 0);".


TBH I don't find lambda syntax strange - it's pretty nice and 
there are two forms (unlike in C++): short one (`a => a > 0`) and 
long one (`(a) { return a > 0; }`).


Compare to C++ lambda syntax: `[](auto a) { return a > 0; }`

My note is, don't we break D rules by leaving ";" after lambda 
function syntax?!


There is no breakage: `a => a > 0` in this example is a 
(template) parameter to `filter` function. You can rewrite it in 
different ways, like: `filter!((a) { return a > 0; })` or


```d
alias criteria = (a) { return a > 0; };
auto r = chain(a, b).filter!criteria;
```

or even longer:

```d
auto criteria(T)(T a)
{
return a > 0;
}

auto r = chain(a, b).filter!criteria;
```

Many of D rules are taken from C, we know that, so a general 
basic rule is to put ";" after each statement


I think this is more or less correct but I personally like that I 
don't need to put ";" after definition of a class or struct 
unlike in C.


so the previous statement of filter should be "auto r = 
chain(a, b).filter!(a => a > 0;);"? Why D leaves ";" in this 
case?


No. it should not. The statement here is `auto r = chain(a, 
b).filter!(a => a > 0);`, not `a => a > 0`. If you use longer 
version of lambda syntax then yes, you'll see ";" there: `auto r 
= chain(a, b).filter!((a) { return a > 0; });` but ";" is not 
after lambda function, it's inside because you have `{...}` 
function body (which, I believe, is defined as a sequence of 
statements so you have ";" there).


Again, both `a => a > 0` and `(a) { return a > 0; }` are just 
parameters to `filter` function. Parameters are not terminated 
with ";". This is the same as in C - you are not adding ";" after 
function parameter:

```cpp
auto is_even = [](int i){ return i%2 == 0; };
auto result = std::find(..., ..., is_even);
```


Write binary data as a file

2022-08-02 Thread Alexander Zhirov via Digitalmars-d-learn
I'm trying to write a mechanism for writing and reading from 
Postgres.
Using the Adama D. Ruppe library. I write data to Postgres in the 
form of this code:

```d
ubyte[] bytes = cast(ubyte[])read("myFile");
PostgresResult resultQuery = cast(PostgresResult) 
db.query("insert into amts.t_client_xrdp_settings (pid_client, 
settings_file) values (?, ?)", id, bytes);

assert(resultQuery !is null);
```
Data appears in the database. Now I'm trying to do the reverse 
process. Get data from Postgres and create a file:

```d
auto result = db.query("select tcxs.settings_file as dbfile from 
amts.t_client_xrdp_settings tcxs where tcxs.pid_client = ?", id);

ubyte[] bytes = cast(ubyte[])result.front()["dbfile"];
write("newFile", bytes);
```
As a result, I get only a set of text data.
I am sure that my mechanism lacks refinement. It remains only to 
find out which one.


Re: Converting JSONValue to AssociativeArray.

2022-08-02 Thread hype_editor via Digitalmars-d-learn
On Monday, 1 August 2022 at 22:47:03 UTC, Steven Schveighoffer 
wrote:

On 8/1/22 2:00 PM, hype_editor wrote:

[...]


```d
// option 1
string[string] aa_data;
foreach(string k, v; data) {
aa_data[k] = v.get!string;
}
// option 2
import std.algorithm : map;
import std.array : assocArray;
import std.typecons : tuple;
auto aa_data = data.object
.byKeyValue
.map!(kv => tuple(kv.key, kv.value.get!string))
.assocArray;
```

I personally prefer the straightforward loop.

-Steve


Steve, thank you very much, works fine!