how can i mixin a function as struct or class member function

2022-04-16 Thread mycybyby via Digitalmars-d-learn

for example:

function void a(int i) {
writeln(i);
}

auto JSFunction(alias F)() if(isSomeFunction!F) {
class S {
public ReturnType!F dg(ParameterTypeTuple!F) {
mixin(F.body) // what should i do ???
}
}
}

thank you


Can Enums be integral types?

2022-04-16 Thread Manfred Nowak via Digitalmars-d-learn
In the specs(17) about enums the word "integral" has no match. 
But because the default basetype is `int`, which is an integral 
type, enums might be integral types whenever their basetype is an 
integral type.


On the other hand the specs(7.6.5.3) about types say
| A bool value can be implicitly converted to any integral type,
| with false becoming 0 and true becoming 1.

This seems senseless, when the enum has no names defined for one 
of these values.




Template name undefined when import from "library" target.

2022-04-16 Thread Elfstone via Digitalmars-d-learn
I'm building my first program with dub. It appears I can import 
and use a struct from a "library". But the template struct is 
undefined unless I change the target type to "sourceLibrary".


The sources are like this:

// library 
// shapes.d
module core.shapes;

struct triangle
{
}

// math.d
module core.math;
import std.stdio;
struct MATRIX(T, int R, int C)
{
}

// package.d

module core;

public
{
import core.math;
import core.shapes;
}
/

// executable //

import std.stdio;
import core.math;
import core.shapes;
int main(string[] argv)
{
triangle t;
MATRIX!(float, 4, 4) a;
writeln(t);
return 0;
}

// dub.sdl
targetType "none"
dependency ":example" version="*"

subPackage {
name "core"
targetType "library"
sourcePaths "core"
importPaths "."
}

subPackage {
name "example"
targetType "executable"
sourceFiles "example/main.d"
importPaths "example"
dependency "proj:core" version="*"
}


I get Error: template instance `MATRIX!(float, 4, 4)` template 
`MATRIX` is not defined


What am I doing wrong here?




Re: Template name undefined when import from "library" target.

2022-04-16 Thread Elfstone via Digitalmars-d-learn

On Saturday, 16 April 2022 at 11:45:38 UTC, Elfstone wrote:
I'm building my first program with dub. It appears I can import 
and use a struct from a "library". But the template struct is 
undefined unless I change the target type to "sourceLibrary".


The sources are like this:

What am I doing wrong here?


Never mind. I had wrong module paths.


Re: module search paths

2022-04-16 Thread kdevel via Digitalmars-d-learn

On Wednesday, 4 August 2021 at 09:41:45 UTC, Mathias LANG wrote:
[...]
JSONType used to be named `JSON_TYPE`, and this was changed in 
v2.082.0. I think GDC-11 is somewhere around v2.076.0 (with a 
lot of backport for bugs, but no feature / Phobos backport). 
Since v2.082.0 was released 2018-09-02 (almost 3 years ago), 
the documentation has long moved.


TL;DR: Use `JSON_TYPE`.


alias JSONType = JSON_TYPE;

That does not suffice:

jsr.d:56:12: Fehler: no property »string« for type »JSON_TYPE«
   56 |   case JSONType.string:
  |^

Note that you can quickly get LDC / DMD setup with the install 
script, or d-apt (https://d-apt.sourceforge.io/).


Will that be corrected in GCC 11.3 or not until GCC 12?


TIC-80 WebAssembly: pointers to fixed addresses

2022-04-16 Thread Pierce Ng via Digitalmars-d-learn

Hi all,

D newbie here. I'm building a D WebAssembly binding for 
[TIC-80](https://github.com/nesbox/TIC-80), a fantasy gaming 
console.


TIC-80 has a [fixed size 
RAM](https://github.com/nesbox/TIC-80/wiki/RAM) that is directly 
addressable by a running program using peek() and poke() 
functions. Basically, 16,384 bytes starting at address zero form 
the video RAM, next 8192 bytes are for storing tiles, next 8192 
bytes for sprites, etc.


Here's how Zig's TIC-80 binding describes the memory layout:

```
pub const FRAMEBUFFER: *allowzero volatile [16320]u8 = 
@intToPtr(*allowzero volatile [16320]u8, 0);

pub const TILES : *[8192]u8 = @intToPtr(*[8192]u8, 0x4000);
pub const SPRITES : *[8192]u8 = @intToPtr(*[8192]u8, 0x6000);
```

I believe the above is declaring that FRAMEBUFFER is a pointer to 
16320 (sic) bytes starting at address 0, TILES is a pointer to 
8192 bytes starting at address 0x4000, and SPRITES a pointer to 
8192 bytes starting at address 0x6000.


Currently for D I have the following, copying the D binding for 
Wasm4, another fantasy console:


```
const FRAMEBUFFER = cast(uint*)0;
const TILES = cast(uint*)0x4000;
const SPRITES = cast(uint*)0x6000;
```

How to express in D, similarly to Zig, that FRAMEBUFFER refers to 
a byte[16384] array starting from address zero, and so on?


Pierce


Re: TIC-80 WebAssembly: pointers to fixed addresses

2022-04-16 Thread Adam Ruppe via Digitalmars-d-learn

On Saturday, 16 April 2022 at 14:29:09 UTC, Pierce Ng wrote:

```
pub const FRAMEBUFFER: *allowzero volatile [16320]u8 = 
@intToPtr(*allowzero volatile [16320]u8, 0);

pub const TILES : *[8192]u8 = @intToPtr(*[8192]u8, 0x4000);
pub const SPRITES : *[8192]u8 = @intToPtr(*[8192]u8, 0x6000);
```

I believe the above is declaring that FRAMEBUFFER is a pointer 
to 16320 (sic) bytes starting at address 0, TILES is a pointer 
to 8192 bytes starting at address 0x4000, and SPRITES a pointer 
to 8192 bytes starting at address 0x6000.


Currently for D I have the following, copying the D binding for 
Wasm4, another fantasy console:


```
const FRAMEBUFFER = cast(uint*)0;
const TILES = cast(uint*)0x4000;
const SPRITES = cast(uint*)0x6000;
```

How to express in D, similarly to Zig, that FRAMEBUFFER refers 
to a byte[16384] array starting from address zero, and so on?


Take the pointer and slice the pointer:

__gshared ubyte[] framebuffer = (cast(uint*) 0) [0 .. 16320]; // 
gshared cuz otherwise D assumes it is TLS and it isn't



Now you can notice it isn't const. You don't want it const since  
the point is to write to the thing. What you might do to keep the 
pointer itself from ever changing is to make it a property:



ubyte[] framebuffer() { return (cast(uint*) 0) [0 .. 16320]; }


And the compiler will see how trivial that is and inline it so it 
works the same way, but then nobody can ever rebind the 
framebuffer symbol.


Beginner memory question.

2022-04-16 Thread WhatMeWorry via Digitalmars-d-learn
I'm playing around with dynamic arrays and I wrote the tiny 
program (at bottom). I get the following output:


PS C:\D\sandbox> dmd -m64 maxMem.d
PS C:\D\sandbox> .\maxMem.exe
Reserving 1,610,613,245 elements
reserve() returned a size of: 1,610,613,245
The capacity() of big is 1,610,613,245
ulong.sizeof (num bytes) = 8
Total bytes allocated = 12,884,905,960
Total megabytes allocated = 12,288
Total gigabytes allocated = 12


The discrepancy occurs because my Windows 10 computer only has 
8.0 GB of memory (and that is not even taking the OS into 
consideration).  Are my mega and giga sizes wrong?  Is virtual 
memory entering into the equation?



import std.stdio, std.array, std.algorithm;
import std.format;
import core.exception;

void main()
{
ulong[] big;

// reserve returns the new capacity of the array
ulong e = 1_610_613_245;   // 1_610_613_246 returns out of 
memory error

writeln("Reserving ", format("%,3d", e) ," elements");
auto u = big.reserve(e);

writeln("reserve() returned a size of: ", format("%,3d", u) );

writeln("The capacity() of big is ", format("%,3d", 
big.capacity));	

writeln("ulong.sizeof (num bytes) = ", ulong.sizeof);
writeln("Total bytes allocated = ", format("%,3d", e * 
ulong.sizeof));


immutable ulong megabyte = 1_048_576;// (1024 x 1024)
immutable ulong gigabyte = 1024 * 1024 * 1024;

writeln("Total megabytes allocated = ", format("%,3d", (e * 
ulong.sizeof)/megabyte));
writeln("Total gigabytes allocated = ", format("%,3d", (e * 
ulong.sizeof)/gigabyte));

 }


Re: module search paths

2022-04-16 Thread kdevel via Digitalmars-d-learn

On Saturday, 16 April 2022 at 13:45:17 UTC, kdevel wrote:
[...]

Will that be corrected in GCC 11.3 or not until GCC 12?


GCC 12.



Re: Beginner memory question.

2022-04-16 Thread Adam Ruppe via Digitalmars-d-learn

On Saturday, 16 April 2022 at 20:41:25 UTC, WhatMeWorry wrote:

Is virtual memory entering into the equation?


Probably. Memory allocated doesn't physically exist until written 
to a lot of the time.


Do I have to pass parameters with ref explicitly?

2022-04-16 Thread Elfstone via Digitalmars-d-learn
I'm reading some d-sources, and it looks like they pass big 
structs by value.

Such as:

Matrix4x4f opBinary(string op)(Matrix4x4f rhs) { ... }

I came from a C++ background, and I would have written:

Matrix4x4f opBinary(string op)(const ref Matrix4x4f rhs) { 
... }


I haven't found anything in the docs yet. Will the d-compiler 
optimize them into virtually the same? Can someone give me a 
reference?


Re: Do I have to pass parameters with ref explicitly?

2022-04-16 Thread max haughton via Digitalmars-d-learn

On Sunday, 17 April 2022 at 03:00:28 UTC, Elfstone wrote:
I'm reading some d-sources, and it looks like they pass big 
structs by value.

Such as:

Matrix4x4f opBinary(string op)(Matrix4x4f rhs) { ... }

I came from a C++ background, and I would have written:

Matrix4x4f opBinary(string op)(const ref Matrix4x4f rhs) { 
... }


I haven't found anything in the docs yet. Will the d-compiler 
optimize them into virtually the same? Can someone give me a 
reference?


It's the same as C++.


Re: Do I have to pass parameters with ref explicitly?

2022-04-16 Thread Elfstone via Digitalmars-d-learn

On Sunday, 17 April 2022 at 04:00:19 UTC, max haughton wrote:

On Sunday, 17 April 2022 at 03:00:28 UTC, Elfstone wrote:
I'm reading some d-sources, and it looks like they pass big 
structs by value.

Such as:

Matrix4x4f opBinary(string op)(Matrix4x4f rhs) { ... }

I came from a C++ background, and I would have written:

Matrix4x4f opBinary(string op)(const ref Matrix4x4f rhs) { 
... }


I haven't found anything in the docs yet. Will the d-compiler 
optimize them into virtually the same? Can someone give me a 
reference?


It's the same as C++.


In C++ I would expect the copy to be optimized away, even when 
passed by value. Do you mean d-compilers (optionally?) do the 
same optimization too?