Re: Mixin helper help

2023-01-13 Thread bauss via Digitalmars-d-learn
On Friday, 13 January 2023 at 16:54:34 UTC, Ali Çehreli wrote: On 1/13/23 00:48, bauss wrote: > 1. Change your mixin template to something like this: There was a technique as a workaround for this template mixin limitation but I can't find it right now. > 2. Change the place where you

Re: Failed to archive JPEG (ArchiveMember): Invalid UTF-8 sequence (at index 1)

2023-01-13 Thread Ki Rill via Digitalmars-d-learn
On Saturday, 14 January 2023 at 01:13:33 UTC, Adam D Ruppe wrote: On Saturday, 14 January 2023 at 01:08:25 UTC, Ki Rill wrote: a JPEG image. member.expandedData(file.readText().dup().representation()); A jpeg image is not a text file. Read it with `std.file.read()` instead of

Re: Failed to archive JPEG (ArchiveMember): Invalid UTF-8 sequence (at index 1)

2023-01-13 Thread Adam D Ruppe via Digitalmars-d-learn
On Saturday, 14 January 2023 at 01:08:25 UTC, Ki Rill wrote: a JPEG image. member.expandedData(file.readText().dup().representation()); A jpeg image is not a text file. Read it with `std.file.read()` instead of `readText`. Then you can get rid of those useless dup.representation calls

Failed to archive JPEG (ArchiveMember): Invalid UTF-8 sequence (at index 1)

2023-01-13 Thread Ki Rill via Digitalmars-d-learn
Please, help me solve the annoying error above. I've been refactoring and rewriting code for my archive utility called [zippo](https://github.com/rillki/zippo) and I face this error when it tries to archive a JPEG image. I tracked it down to the following function that helps me add a new

Re: Creating a pointer/slice to a specific-size buffer? (Handing out a page/frame from a memory manager)

2023-01-13 Thread Gavin Ray via Digitalmars-d-learn
On Friday, 13 January 2023 at 19:16:17 UTC, H. S. Teoh wrote: On Fri, Jan 13, 2023 at 08:31:17AM -0800, Ali Çehreli via Digitalmars-d-learn wrote: On 1/13/23 07:07, Gavin Ray wrote: > This is "valid" D I hope? Yes because static arrays are just elements side-by-side in memory. You can cast

Re: Coding Challenges - Dlang or Generic

2023-01-13 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 13 January 2023 at 18:59:01 UTC, matheus wrote: Unfortunately it's not working for me Yeah, it was an old development version. I also implemented another version the same day: * [Nested Class](https://forum.dlang.org/thread/vkjhkftvyprsivozy...@forum.dlang.org) * [Only One

Re: Unittests on a module

2023-01-13 Thread Dennis via Digitalmars-d-learn
On Friday, 13 January 2023 at 19:07:46 UTC, DLearner wrote: Is this intended? It is by design, though opinions differ on whether it's a good design. It's not a problem to add temporary ``` void main() { } ``` to the bottom of the module, You can add the `-main` flag to make dmd

Re: Creating a pointer/slice to a specific-size buffer? (Handing out a page/frame from a memory manager)

2023-01-13 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 13, 2023 at 08:31:17AM -0800, Ali Çehreli via Digitalmars-d-learn wrote: > On 1/13/23 07:07, Gavin Ray wrote: > > > This is "valid" D I hope? > > Yes because static arrays are just elements side-by-side in memory. > You can cast any piece of memory to a static array provided the

Unittests on a module

2023-01-13 Thread DLearner via Digitalmars-d-learn
If unittest run without a main() being present, crashes on link error: ``` lld-link: error: subsystem must be defined Error: linker exited with status 1 ``` Is this intended? It's not a problem to add temporary ``` void main() { } ``` to the bottom of the module, but seems wrong as not then

Re: Coding Challenges - Dlang or Generic

2023-01-13 Thread matheus via Digitalmars-d-learn
On Thursday, 12 January 2023 at 19:06:49 UTC, Salih Dincer wrote: ... Now, I wrote a nested class using range and copying from Matheus' code. Of course not as comprehensive as [your dcal](https://github.com/quickfur/dcal/blob/master/dcal.d). I like this one and even thought of a new

Re: Mixin helper help

2023-01-13 Thread Ali Çehreli via Digitalmars-d-learn
On 1/13/23 00:48, bauss wrote: > 1. Change your mixin template to something like this: There was a technique as a workaround for this template mixin limitation but I can't find it right now. > 2. Change the place where you instantiate to this: I think the workaround I am trying to remember

Re: Creating a pointer/slice to a specific-size buffer? (Handing out a page/frame from a memory manager)

2023-01-13 Thread Ali Çehreli via Digitalmars-d-learn
On 1/13/23 07:22, Gavin Ray wrote: > Maybe it would be better to wrap the slice in a new class with an > invariant? Possibly but please check before using because I think 'invariant' requires presence of member functions: https://dlang.org/spec/struct.html#Invariant > Because what I want

Re: Creating a pointer/slice to a specific-size buffer? (Handing out a page/frame from a memory manager)

2023-01-13 Thread Ali Çehreli via Digitalmars-d-learn
On 1/13/23 07:07, Gavin Ray wrote: > This is "valid" D I hope? Yes because static arrays are just elements side-by-side in memory. You can cast any piece of memory to a static array provided the length and alignment are correct. However, such a cast is not allowed in @safe code. Ali

Re: Why not allow elementwise operations on tuples?

2023-01-13 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 13, 2023 at 02:22:34PM +, Sergei Nosov via Digitalmars-d-learn wrote: > Hey, everyone! > > I was wondering if there's a strong reason behind not implementing > elementwise operations on tuples? > > Say, I've decided to store 2d points in a `Tuple!(int, int)`. It would > be

Re: Creating a pointer/slice to a specific-size buffer? (Handing out a page/frame from a memory manager)

2023-01-13 Thread Gavin Ray via Digitalmars-d-learn
Maybe it would be better to wrap the slice in a new class with an invariant? Because what I want to do is: 1. Ensure that the length of the underlying referenced/pointed-to data is `PAGE_SIZE` 2. Benefit from the features of D that I can The bounds-checking of slices saves a lot of

Re: Creating a pointer/slice to a specific-size buffer? (Handing out a page/frame from a memory manager)

2023-01-13 Thread Gavin Ray via Digitalmars-d-learn
On Friday, 13 January 2023 at 14:57:40 UTC, Ali Çehreli wrote: On 1/13/23 06:49, Gavin Ray wrote: > I am curious if you can return something like `ubyte[PAGE_SIZE]*` or > `ref ubyte[PAGE_SIZE]`? A simple cast seems to work: enum PAGE_SIZE = 4096; enum BUF_POOL_NUM_PAGES = 1024; alias

Re: Creating a pointer/slice to a specific-size buffer? (Handing out a page/frame from a memory manager)

2023-01-13 Thread Ali Çehreli via Digitalmars-d-learn
On 1/13/23 06:49, Gavin Ray wrote: > I am curious if you can return something like `ubyte[PAGE_SIZE]*` or > `ref ubyte[PAGE_SIZE]`? A simple cast seems to work: enum PAGE_SIZE = 4096; enum BUF_POOL_NUM_PAGES = 1024; alias frame_idx_t = size_t; ubyte[10_000] data; ubyte[PAGE_SIZE]*

Re: Creating a pointer/slice to a specific-size buffer? (Handing out a page/frame from a memory manager)

2023-01-13 Thread Gavin Ray via Digitalmars-d-learn
I probably should have mentioned, the equivalent in C++ is the below: ```cpp #include #include #include static constexpr size_t PAGE_SIZE = 4096; static constexpr size_t BUF_POOL_NUM_PAGES = 1024; class BufferPool { private: alignas(PAGE_SIZE) std::byte

Creating a pointer/slice to a specific-size buffer? (Handing out a page/frame from a memory manager)

2023-01-13 Thread Gavin Ray via Digitalmars-d-learn
Suppose that you have a memory manager, or arena-like class, which contains a buffer used to store memory. And you want to hand out chunks of this memory to other parts of your program. These chunks should all be `PAGE_SIZE`. You might have something like: ```d enum PAGE_SIZE = 4096; enum

Re: Mixin helper help

2023-01-13 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 12 January 2023 at 08:03:34 UTC, John Chapman wrote: Why does the commented code work but the mixin not? Thanks for any pointers. Why not directly use the mixin template for opDispatch()? ```d mixin template helper() { void opDispatch(string name)() { import std.stdio;

Why not allow elementwise operations on tuples?

2023-01-13 Thread Sergei Nosov via Digitalmars-d-learn
Hey, everyone! I was wondering if there's a strong reason behind not implementing elementwise operations on tuples? Say, I've decided to store 2d points in a `Tuple!(int, int)`. It would be convenient to just write `a + b` to yield another `Tuple!(int, int)`. I can resort to using `int

Re: Should importC fail on invalid C code?

2023-01-13 Thread Dennis via Digitalmars-d-learn
On Friday, 13 January 2023 at 12:50:44 UTC, kdevel wrote: Should importC fail on invalid C code? In general, no. The purpose is to build / interface with existing C code, not to develop new C code with it. ImportC also has its own extensions by borrowing D features such as __import, CTFE,

Should importC fail on invalid C code?

2023-01-13 Thread kdevel via Digitalmars-d-learn
After reading Walter's remark "ImportC sees those macro definitions and transforms them into manifest constant declarations" in Issue 23622 - ImportC #defines conflict with declarations I wondered how it was implemented: `myccode.c` ``` int getx () { return X; #define X 1 } ```

Re: Why does the importC example not compile?

2023-01-13 Thread Dennis via Digitalmars-d-learn
On Friday, 13 January 2023 at 12:33:28 UTC, kdevel wrote: What must be added or changed in order to test every example which is intended to produce an executable? Support for separate compilation / ImportC would need to be added to dspec_tester:

Re: Why does the importC example not compile?

2023-01-13 Thread kdevel via Digitalmars-d-learn
On Friday, 13 January 2023 at 12:20:23 UTC, Dennis wrote: I don't think there's a way to test examples of separate compilation in the spec currently. What must be added or changed in order to test every example which is intended to produce an executable?

Re: Why does the importC example not compile?

2023-01-13 Thread Dennis via Digitalmars-d-learn
Thanks for reporting this. PR: https://github.com/dlang/dlang.org/pull/3489 On Friday, 13 January 2023 at 11:10:23 UTC, kdevel wrote: I would have expected that each and every piece of code in the documentation is automatically compiled with any new compiler release. Individual D snippets

Re: Why does the importC example not compile?

2023-01-13 Thread novice2 via Digitalmars-d-learn
try to rename function to distinguish from source module

Why does the importC example not compile?

2023-01-13 Thread kdevel via Digitalmars-d-learn
https://dlang.org/spec/importc.html `square.c` ``` int square(int i) { return i * i; } ``` `demo.d` ``` import std.stdio; import square; void main() { int i = 7; writefln("The square of %s is %s", i, square(i)); } ``` ``` $ dmd --version DMD64 D Compiler v2.101.1 Copyright (C)

Re: Mixin helper help

2023-01-13 Thread bauss via Digitalmars-d-learn
On Thursday, 12 January 2023 at 08:03:34 UTC, John Chapman wrote: I'm obviously doing something wrong, but don't quite understand. ```d mixin template helper() { mixin("writeln(12);"); } struct Foo { void opDispatch(string name)() { import std.stdio; mixin helper!();