Re: byKeyValue is not available at compilation-time right ?

2021-07-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 25 July 2021 at 18:03:05 UTC, someone wrote:

pstrExchangeID = structureExchanges[r"NYSE"d].ID;


well that's a compile time constant


structureExchanges[lstrExchangeID].location;


And that's a compile time constant.

So you could just pass the data directly as a variable. So 
instead of looking up the exchanges[id], just pass the exchange.


AA's are good for runtime but if it is all compiled in you can 
just do static data.


i'll be back later gotta run


Re: byKeyValue is not available at compilation-time right ?

2021-07-25 Thread someone via Digitalmars-d-learn

On Sunday, 25 July 2021 at 17:45:18 UTC, Adam D Ruppe wrote:

On Sunday, 25 July 2021 at 17:29:46 UTC, someone wrote:
What is the proper syntax to use manifest-constants with 
associative arrays then ? The one you showed me ?


You have the right syntax for that. What I'm saying is you 
might not need the associative array at all. Why do you want to 
use that specifically?


Good question.

I re-analyzed the code then.

Because I need code like the following:

```d
...
pstrExchangeID = structureExchanges[r"NYSE"d].ID;
...
```

... and/or (please, see the constructor):

```d
template classExchangeCustom(
   alias dstring lstrExchangeID,
   typeTickerCustom
   ) {

   public class classExchangeCustom : classExchange, 
interfaceExchangeCustom {


  /// (1) given exchange ID; eg: NYSE
  /// (2) given custom‐ticker type; eg: classTickerCustomNYSE

  private typeTickerCustom[dstring] pobjTickers;

  @safe @property public typeTickerCustom[dstring] tickers() 
{ return pobjTickers; }


  @safe this() {

 pudtLocation = 
structureExchanges[lstrExchangeID].location;

 pstrExchangeID = structureExchanges[lstrExchangeID].ID;
 pstrExchangeName = 
structureExchanges[lstrExchangeID].name;
 pstrCurrencyID = 
structureExchanges[lstrExchangeID].currencyID;


  }

  @safe public bool add(typeTickerCustom robjTickerCustom) {

 /// (1) reference to an already-created object for a 
custom‐ticker


 bool lbolAdded = false;

 if (robjTickerCustom ! is null) {

lbolAdded = pobjTickers.require(
   robjTickerCustom.IDsymbolCommon,
   robjTickerCustom
   ) ! is null;

 }

 return lbolAdded;

  }

  @safe public bool add(const dstring lstrSymbolID) {

 /// (1) given symbol ID

 bool lbolAdded = false;

 typeTickerCustom lobjTickerCustom = new 
typeTickerCustom(lstrSymbolID);


 if (lobjTickerCustom ! is null) {

lbolAdded = this.add(lobjTickerCustom);

 }

 return lbolAdded;

  }

   }

}
```

You say a normal foreach ... to be used at compilation-time 
... huh ?


Normal foreach is evaluated at compile time if it is looping 
over an aliasseq tuple or if it is run in a compile time 
context.


Part of the magic of D is ordinary code might be compile time 
or run time depending on how you use it.


Amazing :)


But even static foreach I think can do the

static foreach(k, v; your_assoc_array) {}

in some cases.


I'll look into it.

All in all, and besides these minor issues, I am getting tons of 
totally unexpected flexibility with OOP on D. Some things I am 
doing right now like what I showed you, that probably to you all 
are nothing out-of-the-ordinary to me are ... fantastic features 
:)


Re: byKeyValue is not available at compilation-time right ?

2021-07-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 25 July 2021 at 17:29:46 UTC, someone wrote:
What is the proper syntax to use manifest-constants with 
associative arrays then ? The one you showed me ?


You have the right syntax for that. What I'm saying is you might 
not need the associative array at all. Why do you want to use 
that specifically?


You say a normal foreach ... to be used at compilation-time ... 
huh ?


Normal foreach is evaluated at compile time if it is looping over 
an aliasseq tuple or if it is run in a compile time context.


Part of the magic of D is ordinary code might be compile time or 
run time depending on how you use it.


But even static foreach I think can do the

static foreach(k, v; your_assoc_array) {}

in some cases.



Re: byKeyValue is not available at compilation-time right ?

2021-07-25 Thread someone via Digitalmars-d-learn

On Sunday, 25 July 2021 at 07:22:54 UTC, jfondren wrote:

On Sunday, 25 July 2021 at 05:10:32 UTC, someone wrote:
/// implementation: however, would it be possible to 
dynamically‐load the following enums from a file at 
compilation‐time ?


public immutable enum structureLocations = [
   r"BUE"d : typeLocation(r"arg"d, r"Buenos Aires"d, r"ART"d),
   r"GRU"d : typeLocation(r"bra"d, r"São Paulo"d, r"BRT"d),
   r"HHN"d : typeLocation(r"deu"d, r"Frankfurt am Main"d, 
r"CET"d),

   r"LHR"d : typeLocation(r"gbr"d, r"London"d, r"UTC"d),
   r"NYC"d : typeLocation(r"usa"d, r"New York"d, r"EST"d)
   ];


```
Error: `_aaRange` cannot be interpreted at compile time, 
because it has no available source code

```

Yep, seems that's not available.

What you're doing with `immutable enum structureLocations` is 
creating a manifest constant


Right.

i.e. your AA initialization is copy-pasted into each use of it, 
which means your program is rebuilding this AA at runtime every 
time it comes up. You probably got to this point as a bare 
`immutable structureLocations` errored out to the non-constant 
expression.


Sounds bad, inefficient at least :(

I refactored this little chunk of code many times and I thought 
this was the best one that I came across.


You should be able to use a shared static module initializer to 
initialize an immutable AA, 
https://dlang.org/spec/module.html#staticorder , but although 
I've seen examples of simple string[string] initialization, it 
seems to be very hard to get more complex AAs past the 
std.array/std.exception tools.


So I am seeking free trouble then :(


So here's something you can do:

```d
__gshared const dstring[][dstring] structureExchanges;
__gshared const dstring[dstring] exchangeStructures;


ahhh, the __gshared attribute, I remember it while trying to 
understand the differences for all the attributes: multi‐tasking 
related: to share (non‐immutable global declarations) across all 
threads vs local‐storage (default) ... right ?



shared static this() {
import std.string, std.algorithm;

dstring[][dstring] exchanges;
// could easily load this from a file
"   B3: B3 formerly Bolsa de Valores de São Paulo (aka 
BOVESPA)

BCBA: Bolsa de Comercio de Buenos Aires
LSE: London Stock Exchange
NASDAQ: National Association of Securities Dealers 
Automated Quotations

NYSE: New York Stock Exchange
XETRA: Deutsche Börse
"d.strip.splitLines.map!strip.each!((dstring exch) {
const pair = exch.split(": ");
exchanges[pair[0]] = [pair[1], "some other values"d];
});
structureExchanges = exchanges;

dstring[dstring] structures;
foreach (k, v; exchanges)
structures[v[0]] = k;
exchangeStructures = structures;
}
```

std.array.assocArray might also come in handy.


I suppose I should rethink this matter once again then; I do not 
want to introduce things that make debugging harder than needed.


I'll try to analyze and to implement what you showed me and see 
how it unrolls.


Re: byKeyValue is not available at compilation-time right ?

2021-07-25 Thread someone via Digitalmars-d-learn

On Sunday, 25 July 2021 at 10:30:47 UTC, Adam D Ruppe wrote:

On Sunday, 25 July 2021 at 05:10:32 UTC, someone wrote:
As you can see in the following code I cannot avoid to type 
the public immutable enum structureLocations = [

   r"BUE"d : typeLocation(r"arg"d, r"Buenos Aires"d, r"ART"d),
   r"GRU"d : typeLocation(r"bra"d, r"São Paulo"d, r"BRT"d),


Coudln't you instead just do like

enum structureLoctations = {
   BUE = typeLocation..,
   GRU = typeLiocation
}

?


What I was attempting to implement is a manifest constant as 
described in "enum values that are not of an enum type" @ 
http://ddili.org/ders/d.en/enum.html specifically where it says:


- Such constants are rvalues and they are called manifest 
constants.


- It is possible to create manifest constants of arrays and 
associative arrays as well. However, as we will see later in the 
Immutability chapter, enum arrays and associative arrays may have 
hidden costs.


Since there is no example there the syntax that first occurred to 
me was what you've already seen:


```d
enum structureLocations = [r"XXX"d : ...]
```

What is the proper syntax to use manifest-constants with 
associative arrays then ? The one you showed me ?


Then you can build a runtime hash map if you need it in a 
static constructor or use a binary search switch to convert 
from strings and look up the id from reflection. It depends on 
the usage.


I am not following you :(


static foreach(
   structureExchange sudtExchange;
   structureExchanges.byKeyValue
   ) {


You can also try a normal foreach

foreach(k, v; structureExchanges) {
// use k and v
}


You say a normal foreach ... to be used at compilation-time ... 
huh ?


static foreach might work too but assocative arrays are weird 
beasts that needs to exist all at run time or all at compile 
time; they cannot cross the barrier and use one item for both.


ACK. How should I've known LoL !


But the built-in k,v instead of byKeyValue might help anyway.


Re: POST request with std.net.curl

2021-07-25 Thread frame via Digitalmars-d-learn

On Sunday, 25 July 2021 at 13:07:36 UTC, bachmeier wrote:

On Friday, 23 July 2021 at 18:11:51 UTC, bachmeier wrote:

[...]

After all this, it turned out the answer was a simple (but not 
obvious) typo in the header information. It would be nice to 
get more information than "HTTP request returned status code 
400 ()". I don't know if that's possible, but command line curl 
provides better messages.


In doubt you can turn on the verbose() method on the HTTP object.


Re: POST request with std.net.curl

2021-07-25 Thread bachmeier via Digitalmars-d-learn

On Friday, 23 July 2021 at 18:11:51 UTC, bachmeier wrote:

[...]

After all this, it turned out the answer was a simple (but not 
obvious) typo in the header information. It would be nice to get 
more information than "HTTP request returned status code 400 ()". 
I don't know if that's possible, but command line curl provides 
better messages.


Re: POST request with std.net.curl

2021-07-25 Thread bachmeier via Digitalmars-d-learn

On Saturday, 24 July 2021 at 06:01:25 UTC, frame wrote:

On Friday, 23 July 2021 at 21:25:01 UTC, bachmeier wrote:

Authorization is working - it's the same whether I'm doing a 
GET or POST request. The problem is passing the data. The main 
problem is that the documentation doesn't explain how to 
translate a `--data` option into a `post` call. I've tried 
everything I can think of, including what's shown in the 
documentation, but haven't found anything that works.


You just need to supply a JSON encoded string for the data - 
from an object like std.json.JSONValue via its toString() 
method for example.


I understand, and indeed, that's what's done by the working curl 
command I posted. I don't know how to translate that into a post 
request using std.net.curl. The natural thing would be to pass as 
the second argument the same string I'm sending as the `--data` 
option to curl, but that doesn't work.


I've decided to give up on std.net.curl and use executeShell to 
capture the output from shell commands.


Re: Traceinfo gone

2021-07-25 Thread frame via Digitalmars-d-learn

On Sunday, 25 July 2021 at 10:26:34 UTC, Adam D Ruppe wrote:

On Saturday, 24 July 2021 at 08:41:20 UTC, frame wrote:
I recently discovered that my exceptions do not show a trace 
anymore.

How can this happen?


It needs to load the .pdb file at runtime, so make sure it 
stays next to your exe.


No, I think you misunderstand the issue. I don't care about the 
symbolic debug information.


The issue is: w/o calling test() there are stack lines available 
but when I compile it with the test() call, no stack lines are 
available?


Re: How to check if variable of some type can be of null value?

2021-07-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 24 July 2021 at 18:10:07 UTC, Alexey wrote:
The goal I with to achieve by this check - is to use template 
and to assign value to variable basing on it's ability to 
accept null as a value.


The most direct representation of that is __traits(compiles, (T 
t) { t = null; });



Another cool trick to consider is:

is(typeof(null) : Whatever)


What that means is if the null literal will implicitly convert to 
Whatever type. This means you can pass `null` as an argument to a 
function accepting Whatever. Thus it includes pointers, classes, 
interfaces, arrays. But does NOT include structs, even if they 
have a null accepting constructor / opAssign since you still must 
explicitly construct them.


struct S {
   void opAssign(typeof(null) n) {}
}

void main() {
  S s;
  s = null; // allowed due to opAssign
}

pragma(msg, is(typeof(null) : S)); // FALSE because this check 
only looks for implicit conversion, not user-defined assign 
overloads or constructors.


The traits compiles check will allow this, since it is looking at 
assign... but the traits compiles will say false if it gets a 
`const` type since obviously then assign is not allowed, even if 
implicit conversion would be.


Depending on your needs you might use one of these, or perhaps 
both.


Re: byKeyValue is not available at compilation-time right ?

2021-07-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Sunday, 25 July 2021 at 05:10:32 UTC, someone wrote:
As you can see in the following code I cannot avoid to type the 
public immutable enum structureLocations = [

   r"BUE"d : typeLocation(r"arg"d, r"Buenos Aires"d, r"ART"d),
   r"GRU"d : typeLocation(r"bra"d, r"São Paulo"d, r"BRT"d),


Coudln't you instead just do like

enum structureLoctations = {
   BUE = typeLocation..,
   GRU = typeLiocation
}


?


Then you can build a runtime hash map if you need it in a static 
constructor or use a binary search switch to convert from strings 
and look up the id from reflection. It depends on the usage.




static foreach(
   structureExchange sudtExchange;
   structureExchanges.byKeyValue
   ) {


You can also try a normal foreach


foreach(k, v; structureExchanges) {
// use k and v
}


static foreach might work too but assocative arrays are weird 
beasts that needs to exist all at run time or all at compile 
time; they cannot cross the barrier and use one item for both.


But the built-in k,v instead of byKeyValue might help anyway.


Re: Traceinfo gone

2021-07-25 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 24 July 2021 at 08:41:20 UTC, frame wrote:
I recently discovered that my exceptions do not show a trace 
anymore.

How can this happen?


It needs to load the .pdb file at runtime, so make sure it stays 
next to your exe.




Re: Destructors can't be @nogc?

2021-07-25 Thread Tejas via Digitalmars-d-learn

On Friday, 23 July 2021 at 20:24:02 UTC, Jim wrote:

Hello,

I've been playing with D and trying to understand how to work 
with @nogc. I must be doing something wrong, because even 
though I tagged the destructor for my class `@nogc`, I'm 
getting the following error: `.\min.d(27): Error: "@nogc" 
function "D main" cannot call non-@nogc function 
"object.destroy!(true, TestClass).destroy`


```D
import std.stdio : printf;
import core.lifetime : emplace;
import core.stdc.stdlib : malloc, free;

class TestClass {
int x;

this(int x) @nogc {
printf("TestClass's constructor called\n");
this.x = x;
}


~this() @nogc {
printf("TestClass's destructor called\n");
}
}

@nogc void
main() {
auto size = __traits(classInstanceSize, TestClass);
auto memory = malloc(size)[0..size];
TestClass x = emplace!(TestClass)(memory, 1);

printf("TestClass.x = %d\n", x.x);

destroy(x);
free(cast(void*)x);
}

```

What is the problem here? Should I not call `destroy`? If so, 
what should I call instead?


Try using the ```scope``` storage class. You will lose the 
ability to explicitly call the destructor, but maybe it is good 
enough for your purposes.


The following works:

```d
import std.stdio : printf;
import core.lifetime : emplace;
import core.stdc.stdlib : malloc, free;

class TestClass {
int x;

this(int x) @nogc {
printf("TestClass's constructor called\n");
this.x = x;
}


~this() @nogc {
printf("TestClass's destructor called\n");
}
}

@nogc void
main() {
//auto size = __traits(classInstanceSize, TestClass);
//auto memory = malloc(size)[0..size];
scope /*notice the scope storage class*/TestClass x =  
new TestClass(1);// emplace!(TestClass)(memory, 1);


printf("TestClass.x = %d\n", x.x);

//destroy(x);
//free(cast(void*)x);
}
```

If you absolutely want to be able to explicitly call the 
destructor, then custom functions are the only option, 
unfortunately.


Re: byKeyValue is not available at compilation-time right ?

2021-07-25 Thread jfondren via Digitalmars-d-learn

On Sunday, 25 July 2021 at 05:10:32 UTC, someone wrote:
/// implementation: however, would it be possible to 
dynamically‐load the following enums from a file at 
compilation‐time ?


public immutable enum structureLocations = [
   r"BUE"d : typeLocation(r"arg"d, r"Buenos Aires"d, r"ART"d),
   r"GRU"d : typeLocation(r"bra"d, r"São Paulo"d, r"BRT"d),
   r"HHN"d : typeLocation(r"deu"d, r"Frankfurt am Main"d, 
r"CET"d),

   r"LHR"d : typeLocation(r"gbr"d, r"London"d, r"UTC"d),
   r"NYC"d : typeLocation(r"usa"d, r"New York"d, r"EST"d)
   ];


```
Error: `_aaRange` cannot be interpreted at compile time, because 
it has no available source code

```

Yep, seems that's not available.

What you're doing with `immutable enum structureLocations` is 
creating a manifest constant, i.e. your AA initialization is 
copy-pasted into each use of it, which means your program is 
rebuilding this AA at runtime every time it comes up. You 
probably got to this point as a bare `immutable 
structureLocations` errored out to the non-constant expression.


You should be able to use a shared static module initializer to 
initialize an immutable AA, 
https://dlang.org/spec/module.html#staticorder , but although 
I've seen examples of simple string[string] initialization, it 
seems to be very hard to get more complex AAs past the 
std.array/std.exception tools.


So here's something you can do:

```d
__gshared const dstring[][dstring] structureExchanges;
__gshared const dstring[dstring] exchangeStructures;

shared static this() {
import std.string, std.algorithm;

dstring[][dstring] exchanges;
// could easily load this from a file
"   B3: B3 formerly Bolsa de Valores de São Paulo (aka 
BOVESPA)

BCBA: Bolsa de Comercio de Buenos Aires
LSE: London Stock Exchange
NASDAQ: National Association of Securities Dealers 
Automated Quotations

NYSE: New York Stock Exchange
XETRA: Deutsche Börse
"d.strip.splitLines.map!strip.each!((dstring exch) {
const pair = exch.split(": ");
exchanges[pair[0]] = [pair[1], "some other values"d];
});
structureExchanges = exchanges;

dstring[dstring] structures;
foreach (k, v; exchanges)
structures[v[0]] = k;
exchangeStructures = structures;
}
```

std.array.assocArray might also come in handy.


Re: Traceinfo gone

2021-07-25 Thread frame via Digitalmars-d-learn

On Saturday, 24 July 2021 at 08:41:20 UTC, frame wrote:


I cannot reproduce it with a standalone example app


Meanwhile I can, it seems a linking problem or I'm doing 
something wrong? Please consider:


```d

// dmd -m64 -L/DLL -version=lib test.d -of=common.dll
// dmd -m64 test.d

// test.d
version (lib)
{
import core.sys.windows.dll;
import std.stdio;

mixin SimpleDllMain;

export extern (C) void test()
{
writeln("test() called");
}
}
else
{
import std.stdio;

pragma(lib, "common.lib");
extern (C) void test();

void main()
{
// No trace info anymore if extern function is called
// test();

try
{
throw new Exception("test");
}
catch (Throwable e)
{
writeln("trace: ", e.info);
writeln("whatever");
}
}
}
```