Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Ali Çehreli via Digitalmars-d-learn

On 6/30/23 17:42, Cecil Ward wrote:

> https://dlang.org/spec/hash-map.html#testing_membership in the language
> docs, under associative arrays - 13.3 testing membership. Would anyone
> else care to try that example out as that might be quicker?

I tried it by

1) Putting all the code inside a 'void main()' function
2) Pasting the code from the top of that page

and it works:

void main() {
int[string] aa;   // Associative array of ints that are
// indexed by string keys.
// The KeyType is string.
aa["hello"] = 3;  // set value associated with key "hello" to 3
int value = aa["hello"];  // lookup value from a key
assert(value == 3);
int* p;

p = "hello" in aa;
if (p !is null)
{
*p = 4;  // update value associated with key
assert(aa["hello"] == 4);
}
}

> the only substantive
> change being deleting the variable p

Do you mean this:

aa.remove("hello");

That works too.

D's associative arrays have a few quirks but they work just fine. They 
are not buggy as it may be inferred from some of the posts here.


Ali



Graphing

2023-06-30 Thread anonymouse via Digitalmars-d-learn
How would I go about graphing time series data (specifically, 
candles, moving averages, etc) in D and dynamically updating such 
charts?


Thanks,
--anonymouse


Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Cecil Ward via Digitalmars-d-learn

On Friday, 30 June 2023 at 19:05:23 UTC, Cecil Ward wrote:

I have code roughly like the following:

   dstring str = "name"d;
   uint ordinal =  (( str in Decls.ordinals ) !is null)  ?  
Decls.ordinals[ str ]  :  -1;


struct Decls
   {
   uint[ dstring]   ordinals;
   }

//and
   Decls.ordinals[ str ] = ordinal_counter++;

The problem is that it always returns ordinal== -1 from the 
expression. Can you sort me out? I took this from the example 
given in the language reference under arrays, testing for 
membership (or similar, I forget the subssection title).


From good old printfs it seems to be the case that the array is 
being populated (elsewhere) with the expected correct values. 
Taking out the if doesn’t seem to help either. I don’t have a 
way of examining the contents of the dynamic array directly to 
check that they are actually being stored as expected, other 
than seeing that that line of code is indeed being executed 
with the expected values of str going in. Note that I’m using 
32-bit dstrings everywhere, not strings of bytes.


Fool that I am. I did those good old printfs a while back, and 
now I recheck them I see that something has become broken and it 
seems that the insertions are not happening now. So thankyou for 
your kindness and I’ll post again if that doesn’t solve the 
non-issue.


Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Cecil Ward via Digitalmars-d-learn

On Friday, 30 June 2023 at 21:25:23 UTC, H. S. Teoh wrote:
On Fri, Jun 30, 2023 at 07:05:23PM +, Cecil Ward via 
Digitalmars-d-learn wrote: [...]


It would help if you could post the complete code that 
reproduces the problem. Or, if you do not wish to reveal your 
code, reduce it to a minimal case that still exhibits the same 
problem, so that we can see it for ourselves.  The snippets you 
provided do not provide enough information to identify the 
problem.



T


I would indeed need to cut it down massively, as the original 
code is ~2k lines. Mind you, I will just end up with the example 
at https://dlang.org/spec/hash-map.html#testing_membership in the 
language docs, under associative arrays - 13.3 testing 
membership. Would anyone else care to try that example out as 
that might be quicker? That’s because as all I did was copy that 
basically, with the only substantive change being deleting the 
variable p, but I’m still testing whether or not I get a null 
pointer.


I thought I had checked that the insertions were all as expected, 
so I’ll go and recheck that next.


Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread mw via Digitalmars-d-learn

https://forum.dlang.org/thread/duetqujuoceancqtj...@forum.dlang.org

Try HashMap see if it is still a problem.

If no, then it's another example of the built in AA problem.


Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jun 30, 2023 at 07:05:23PM +, Cecil Ward via Digitalmars-d-learn 
wrote:
[...]

It would help if you could post the complete code that reproduces the
problem. Or, if you do not wish to reveal your code, reduce it to a
minimal case that still exhibits the same problem, so that we can see it
for ourselves.  The snippets you provided do not provide enough
information to identify the problem.


T

-- 
What's the difference between a 4D tube and an overweight Dutchman?  One
is a hollow spherinder, and the other is a spherical Hollander.


Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Ali Çehreli via Digitalmars-d-learn

On 6/30/23 13:16, Cecil Ward wrote:

On Friday, 30 June 2023 at 19:58:39 UTC, FeepingCreature wrote:



Note that you can do `uint ordinal = Decls.ordinals.get(str, -1);`.


Is the second argument an ‘else’ then, my friend?


Yes, .get and friends appear in this table:

  https://dlang.org/spec/hash-map.html#properties

Ali



Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Cecil Ward via Digitalmars-d-learn

On Friday, 30 June 2023 at 19:58:39 UTC, FeepingCreature wrote:

On Friday, 30 June 2023 at 19:05:23 UTC, Cecil Ward wrote:

I have code roughly like the following:

   dstring str = "name"d;
   uint ordinal =  (( str in Decls.ordinals ) !is null)  ?  
Decls.ordinals[ str ]  :  -1;


struct Decls
   {
   uint[ dstring]   ordinals;
   }

//and
   Decls.ordinals[ str ] = ordinal_counter++;

The problem is that it always returns ordinal== -1 from the 
expression. Can you sort me out?


Impossible to tell without a complete repro, I'm afraid. The 
expression, at least, looks correct at first glance.


Note that you can do `uint ordinal = Decls.ordinals.get(str, 
-1);`.


Is the second argument an ‘else’ then, my friend?


Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Cecil Ward via Digitalmars-d-learn

On Friday, 30 June 2023 at 20:12:08 UTC, Ali Çehreli wrote:

On 6/30/23 12:05, Cecil Ward wrote:
> I have code roughly like the following:
>
> dstring str = "name"d;

Aside: One almost never needs dstring.

> uint ordinal =  (( str in Decls.ordinals ) !is null)  ?
> Decls.ordinals[ str ]  :  -1;
>
> struct Decls
> {
> uint[ dstring]   ordinals;

Do you mean 'ordinals' is 'static'? Otherwise, Decls.ordinals 
does not compile.


> }
>
> //and
> Decls.ordinals[ str ] = ordinal_counter++;

Are you doing that *after* you initialize 'ordinal' as you show 
here? :)


Ali


Hi Ali, ‘ordinal’ is a static initialised explicitly with zero.


Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Ali Çehreli via Digitalmars-d-learn

On 6/30/23 12:05, Cecil Ward wrote:
> I have code roughly like the following:
>
> dstring str = "name"d;

Aside: One almost never needs dstring.

> uint ordinal =  (( str in Decls.ordinals ) !is null)  ?
> Decls.ordinals[ str ]  :  -1;
>
> struct Decls
> {
> uint[ dstring]   ordinals;

Do you mean 'ordinals' is 'static'? Otherwise, Decls.ordinals does not 
compile.


> }
>
> //and
> Decls.ordinals[ str ] = ordinal_counter++;

Are you doing that *after* you initialize 'ordinal' as you show here? :)

Ali



Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread FeepingCreature via Digitalmars-d-learn

On Friday, 30 June 2023 at 19:05:23 UTC, Cecil Ward wrote:

I have code roughly like the following:

   dstring str = "name"d;
   uint ordinal =  (( str in Decls.ordinals ) !is null)  ?  
Decls.ordinals[ str ]  :  -1;


struct Decls
   {
   uint[ dstring]   ordinals;
   }

//and
   Decls.ordinals[ str ] = ordinal_counter++;

The problem is that it always returns ordinal== -1 from the 
expression. Can you sort me out?


Impossible to tell without a complete repro, I'm afraid. The 
expression, at least, looks correct at first glance.


Note that you can do `uint ordinal = Decls.ordinals.get(str, 
-1);`.


Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Cecil Ward via Digitalmars-d-learn

I have code roughly like the following:

   dstring str = "name"d;
   uint ordinal =  (( str in Decls.ordinals ) !is null)  ?  
Decls.ordinals[ str ]  :  -1;


struct Decls
   {
   uint[ dstring]   ordinals;
   }

//and
   Decls.ordinals[ str ] = ordinal_counter++;

The problem is that it always returns ordinal== -1 from the 
expression. Can you sort me out? I took this from the example 
given in the language reference under arrays, testing for 
membership (or similar, I forget the subssection title).


From good old printfs it seems to be the case that the array is 
being populated (elsewhere) with the expected correct values. 
Taking out the if doesn’t seem to help either. I don’t have a way 
of examining the contents of the dynamic array directly to check 
that they are actually being stored as expected, other than 
seeing that that line of code is indeed being executed with the 
expected values of str going in. Note that I’m using 32-bit 
dstrings everywhere, not strings of bytes.




Re: IntelliJ D language plugin

2023-06-30 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

I use it and contribute to it ;)


IntelliJ D language plugin

2023-06-30 Thread Dmitry Olshansky via Digitalmars-d-learn

Have anyone had any luck with it?

So far I'm trying to install DMD as SDK but it fails with not a 
valid D compiler home.


--
Dmitry Olshansky

https://olshansky.me


Re: Debugging by old fashioned trace log printfs / writefln

2023-06-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, June 29, 2023 12:27:22 PM MDT Cecil Ward via Digitalmars-d-learn 
wrote:
> I’m trying to debug my D program with old-fashioned printfs stuck
> in various strategic places, actually using writefln(). My
> problem is that the addition of printf fights with the existing
> declarations for pure nothrow @nogc @safe and I have to adjust
> them, then put them back correctly when the writefln() trace
> statements are later removed.
>
> Is there something else I could be using, something that is
> allowed to violate the checking rules for purity, nothrow, @nogc?
> Would pragma( msg, "…" ) do the trick? Is that what I should be
> using?

pragma(msg, ...) and writeln are fundamentally different.

pragmas are run when code is compiled. When you calling a function during
CTFE, you are calling that function. You are not compiling it. It has
already been compiled at that point. That function may be being called as
part of compiling another function, but function that you're calling has
already been compiled. So, something like

string foo() { return "foo"; }

void bar(int i)
{
pragma(msg, foo());
}

will compile just fine, and it will print out "foo" at compile time, whereas

void bar(int i)
{
pragma(msg, i);
}

will not compile.

void bar(int i)
{
writeln(i);
}

will compile, but it won't print anything when it's compiled, and it cannot
be called with CTFE. However, it will of course print out if called at
runtime.

If you need to print out a message during testing, and the function in
question has attributes that writeln does not satisfy, then you can use
debug statements and compile the code with the -debug flag.

https://dlang.org/spec/version.html#debug

e.g.

void foo() pure @safe
{
debug
{
writeln("hello");
}
}

Of course, you have to be very careful when you do that, since you'll get
undefined behavior if the debug statements have side effects which violate
the guarantees that those attributes are supposed to make (e.g. mutating a
global variable in a pure function or throwing an exception from a nothrow
function), but simply printing out stuff shouldn't be a problem unless
generating the strings to print has side effects.

- Jonathan M Davis






Re: is Dlang support Uniform initialization like c++

2023-06-30 Thread Ali Çehreli via Digitalmars-d-learn

On 6/30/23 08:18, lili wrote:


How too wirte this: addPoint({4,5}, {4,6})


In this case, arrays are better but only if you don't define a 
constructor, which you don't need for simple types like Point below:


struct Point {
int x;
int y;
}

void main() {
// The type is explicit on the left-hand side
Point[] points = [ {1,2} ];
}

Ali



Re: Debugging by old fashioned trace log printfs / writefln

2023-06-30 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jun 30, 2023 at 03:43:14PM +, Cecil Ward via Digitalmars-d-learn 
wrote:
[...]
> Since I can pass my main function some compile-time-defined input, the
> whole program should be capable of being executed with CTFE, no? So in
> that case pragma( msg ) should suffice for a test situation? Would
> pragma(message) have the advantage over writefln that I don’t have to
> pervert the function attributes like nogc nothrow pure ?

Just use the `debug` statement:

auto pureFunc(Args args) pure {
...
debug writefln("debug info: %s", ...);
...
}

Compile with `-debug` to enable the writefln during development. When
not compiling with `-debug`, the writefln will not be compiled and the
function will actually be pure.

The problem with pragma(msg) is that it happens very early in the
compilation process; some things may not be available to it, such as the
value of variables in CTFE. This may limit its usefulness in some
situations.  For more details on this, see:

https://wiki.dlang.org/Compile-time_vs._compile-time


T

-- 
He who sacrifices functionality for ease of use, loses both and deserves 
neither. -- Slashdotter


Re: is Dlang support Uniform initialization like c++

2023-06-30 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/30/23 11:18 AM, lili wrote:

     struct Point {
  int x;
  int y;
   this(int x, int y) { this.x =x; this.y=y;}
     }

     void addPoint(Point a, Point b) {
    ...
     }

How too wirte this: addPoint({4,5}, {4,6})


You have to write `Point(4, 5)`. The advantage is we don't need to deal 
with the complexity of C++ overloading rules.


-Steve


Re: is Dlang support Uniform initialization like c++

2023-06-30 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jun 30, 2023 at 03:18:41PM +, lili via Digitalmars-d-learn wrote:
> struct Point {
>  int x;
>  int y;
>   this(int x, int y) { this.x =x; this.y=y;}
> }
> 
> void addPoint(Point a, Point b) {
>...
> }
> 
> How too wirte this: addPoint({4,5}, {4,6})

addPoint(Point(4,5), Point(4,6));


T

-- 
"No, John.  I want formats that are actually useful, rather than over-featured 
megaliths that address all questions by piling on ridiculous internal links in 
forms which are hideously over-complex." -- Simon St. Laurent on xml-dev


Re: Debugging by old fashioned trace log printfs / writefln

2023-06-30 Thread Cecil Ward via Digitalmars-d-learn

On Thursday, 29 June 2023 at 23:54:45 UTC, Chris Katko wrote:

On Thursday, 29 June 2023 at 18:27:22 UTC, Cecil Ward wrote:
I’m trying to debug my D program with old-fashioned printfs 
stuck in various strategic places, actually using writefln(). 
My problem is that the addition of printf fights with the 
existing declarations for pure nothrow @nogc @safe and I have 
to adjust them, then put them back correctly when the 
writefln() trace statements are later removed.


Is there something else I could be using, something that is 
allowed to violate the checking rules for purity, nothrow, 
@nogc? Would pragma( msg, "…" ) do the trick? Is that what I 
should be using?


pragma(msg, "") is only for compile time. It for debugging 
functions/templates if they're actually used (which static path 
is used), instantiated, and you can also get type values from 
template inputs to confirm they're what you expect. "Oh this is 
a char[][] not a char[]!"


pragmas are the D equivalent of C/C++ pragmas. In this case, 
C/C++:

```C
#pragma message( message-string )
```


Since I can pass my main function some compile-time-defined 
input, the whole program should be capable of being executed with 
CTFE, no? So in that case pragma( msg ) should suffice for a test 
situation? Would pragma(message) have the advantage over writefln 
that I don’t have to pervert the function attributes like nogc 
nothrow pure ?


is Dlang support Uniform initialization like c++

2023-06-30 Thread lili via Digitalmars-d-learn

struct Point {
 int x;
 int y;
  this(int x, int y) { this.x =x; this.y=y;}
}

void addPoint(Point a, Point b) {
   ...
}

How too wirte this: addPoint({4,5}, {4,6})


Re: Mixin and compile-time functions for code generation

2023-06-30 Thread Christian Köstlin via Digitalmars-d-learn

On 24.06.23 18:31, Cecil Ward wrote:
I have a function that can be run at compile-time and which will be able 
to output code to be injected into the D source code stream. Can I get 
mixin whatever to do this for me? Mixin with a function that runs at 
compile-time and creates the required source ? Like D’s solution for a 
replacement for function-style C preprocessor macros ? - but far more 
advanced and capable ?


I need to re-read Ali Çehreli’s excellent book for the third time.

Is this what you want todo:

```d
import std.string : format, capitalize;
import std.stdio : writeln;

string getter(string v)
{
return "auto get%s() { return %s; }".format(v.capitalize, v);
}

class T
{
int abc;
mixin(getter("abc"));
}

void main() {
auto t = new T();
writeln(t.getAbc());
}
```