Re: Is there such a JSON parser?

2023-01-01 Thread thebluepandabear via Digitalmars-d-learn

On Sunday, 1 January 2023 at 23:28:12 UTC, torhu wrote:
I need to parse some JSON data into various data structures, so 
I'm looking for a parser based on events or ranges. One that 
doesn't just load the file and build a data structure that 
represents the whole thing. So not std.json, at least.


I'm new to D and also haven't found any solid JSON libraries. 
Unfortunately a massive downside for D is the fact that the 
ecosystem is very small when compared to other languages such as 
Java.


You might want to try the following:

https://github.com/libmir/asdf





Re: Is there such a JSON parser?

2023-01-01 Thread Salih Dincer via Digitalmars-d-learn

On Sunday, 1 January 2023 at 23:28:12 UTC, torhu wrote:
I need to parse some JSON data into various data structures, so 
I'm looking for a parser based on events or ranges. One that 
doesn't just load the file and build a data structure that 
represents the whole thing. So not std.json, at least.


There may be suggestions that will come in handy in a discussion 
that here:


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

SDB@79


Is there such a JSON parser?

2023-01-01 Thread torhu via Digitalmars-d-learn
I need to parse some JSON data into various data structures, so 
I'm looking for a parser based on events or ranges. One that 
doesn't just load the file and build a data structure that 
represents the whole thing. So not std.json, at least.


Re: Solving optimization problems with D

2023-01-01 Thread max haughton via Digitalmars-d-learn

On Sunday, 1 January 2023 at 21:11:06 UTC, Ogi wrote:
I’ve read this [series if 
articles](https://www.gamedeveloper.com/design/decision-modeling-and-optimization-in-game-design-part-1-introduction) about using Excel Solver for all kinds of optimization problems. This is very neat, but of course, I would prefer to write models with code instead, preferably in D. I glanced at mir-optim but it requires knowledge of advanced math. Is there something more approachable for a layperson?


What do you want to optimize? Optimization in general requires 
reasonably advanced mathematics, whereas a single problem can be 
simplified.


Solving optimization problems with D

2023-01-01 Thread Ogi via Digitalmars-d-learn
I’ve read this [series if 
articles](https://www.gamedeveloper.com/design/decision-modeling-and-optimization-in-game-design-part-1-introduction) about using Excel Solver for all kinds of optimization problems. This is very neat, but of course, I would prefer to write models with code instead, preferably in D. I glanced at mir-optim but it requires knowledge of advanced math. Is there something more approachable for a layperson?


Re: Address of a class object

2023-01-01 Thread Ali Çehreli via Digitalmars-d-learn

On 1/1/23 01:01, Paul wrote:

> ...on my laptop it prints...
> ```
>   Size  Alignment  Type
> =
> 9   4  MyClass
>
> 4FFB20  4FFB24
> ```

> If the size of MyClass is 9 bytes why do MyClassO1 & O2 addresses only
> differ by 4 bytes?

As matheus said, classes are reference types, meaning, class variables 
are implemented as pointers. The values above are the addresses of those 
pointers. The fact that those values are different by 4 tells us that 
the code was compiled for 32 bits.


On the other hand, matheus's values were 8 apart because that 
compilation was 64 bits.


> So, I guess my question is actually how do I print out the addresses of
> the MyClassO1 & O2 objects themselves?

You must have missed my earlier answer: You need to cast the variable to 
'void*'. That special operation will give you the address of the object. 
I am adding the following last line to matheus's example:


// ...
writeln("\n",&(MyClassO1.c),"\t",&(MyClassO2.c));
writeln("\n",cast(void*)MyClassO1,"\t",cast(void*)MyClassO2);

The output:

[...]
7F125FE770107F125FE77030  <-- The addresses of the 'c' members

7F125FE770007F125FE77020  <-- The addresses of the objects

The reason why objects are 0x10 bytes before the 'c' members is because 
a class object has two hidden initial members: the vtbl pointer and the 
monitor, both of which are size of a pointer (4 on your system, and 8 on 
matheus's system and mine).


Additionally, if you define the class as extern(C++), there will not be 
the monitor member. The reason for that is C++ does not have that 
concept and it would break interoperability.


Ali



Re: Address of a class object

2023-01-01 Thread matheus via Digitalmars-d-learn

On Sunday, 1 January 2023 at 09:01:24 UTC, Paul wrote:

...
If the size of MyClass is 9 bytes why do MyClassO1 & O2 
addresses only differ by 4 bytes?


Because those addresses(4FFB20  4FFB24) are the addresses of 
the class **variables**, not the addresses of the **objects** 
themselves?


Because MyClass01 and MyClass02 are pointers and in your case 
they differ 4 bytes each other.


Now you could do this:

import std.stdio, std.traits;

class MyClass {char[16] c;}

void main() {
writeln(" Size  Alignment  Type\n",
"=");

size_t size = __traits(classInstanceSize, MyClass);
size_t alignment = classInstanceAlignment!MyClass;

writefln("%4s%8s  %s",size, alignment, MyClass.stringof);

// my test code added
MyClass MyClassO1;
MyClass MyClassO2;
writeln("\n",,"\t",);
writeln("\n",&(MyClassO1.c),"\t",&(MyClassO2.c));
MyClassO1 = new MyClass();
MyClassO2 = new MyClass();
writeln("\n",,"\t",);
writeln("\n",&(MyClassO1.c),"\t",&(MyClassO2.c));
}

In this machine it will print:

 Size  Alignment  Type
=
  32   8  MyClass

7FFD890C64107FFD890C6418 <-  
10	10   <- Note here  [&(MyClassO1.c), 
&(MyClassO2.c)]

7FFD890C64107FFD890C6418 <-   (Same address)
7FD0435D8010	7FD0435D8030 <- Now after instantiation! 
[&(MyClassO1.c), &(MyClassO2.c)]


Finally as you can see I changed your:

class MyClass {char c;}

to:

class MyClass {char[16] c;}

Because from char[1] to char[16] it will keep the address 
difference for [&(MyClassO1.c), &(MyClassO2.c)] by 0x20 (32):


7FD0435D80107FD0435D8030

If I change to char[17]

The difference goes up from 0x20 (32) to 0x30 (48), and keeps 
that way until char[32]:


7FD0435D80107FD0435D8040

char[33] will increase again by 16 bytes and so on.

Matheus.


Re: Address of a class object

2023-01-01 Thread Paul via Digitalmars-d-learn
Thanks all. Yes it seems my understanding and "D" vocabulary are 
still a bit confused.


So I'm taking a D course online and was trying to verify what I 
was learning. The course example printed out the size and 
alignment of types...something close to this:

```d
import std.stdio;
import std.traits;

class MyClass {char c;}

void main() {
writeln(" Size  Alignment  Type\n",
"=");

size_t size = __traits(classInstanceSize, MyClass);
size_t alignment = classInstanceAlignment!MyClass;

writefln("%4s%8s  %s",size, alignment, MyClass.stringof);

// my test code added
MyClass MyClassO1;
MyClass MyClassO2;
writeln("\n",,"\t",);
}
```
...on my laptop it prints...
```
 Size  Alignment  Type
=
   9   4  MyClass

4FFB20  4FFB24
```

If the size of MyClass is 9 bytes why do MyClassO1 & O2 addresses 
only differ by 4 bytes?


Because those addresses(4FFB20  4FFB24) are the addresses of the 
class **variables**, not the addresses of the **objects** 
themselves?


So, I guess my question is actually how do I print out the 
addresses of the MyClassO1 & O2 objects themselves?

```