Re: Storing interfaces as void[]

2021-03-12 Thread David Zhang via Digitalmars-d-learn
On Friday, 12 March 2021 at 22:18:59 UTC, tsbockman wrote: Why do you think you need a `void[]` slice? I think `void*` pointers are sufficient. This handles all normal data types, as long as they are allocated on the GC heap: I wanted to have the registry own the structs' memory, though using

Re: Storing interfaces as void[]

2021-03-12 Thread David Zhang via Digitalmars-d-learn
On Friday, 12 March 2021 at 18:14:12 UTC, Imperatorn wrote: On Friday, 12 March 2021 at 17:57:06 UTC, David Zhang wrote: On Friday, 12 March 2021 at 17:46:22 UTC, Imperatorn wrote: On Friday, 12 March 2021 at 17:37:43 UTC, David Zhang wrote: [...] Have you tried using Variant or jsvar (ht

Re: Storing interfaces as void[]

2021-03-12 Thread David Zhang via Digitalmars-d-learn
On Friday, 12 March 2021 at 18:50:26 UTC, tsbockman wrote: The idea is to implement a service locator s.t. code like this is possible: // struct (I didn't mention this in the top post, my mistake) auto log = Logger() api_registry.register!Logger(log); // class/interface

Re: Storing interfaces as void[]

2021-03-12 Thread David Zhang via Digitalmars-d-learn
On Friday, 12 March 2021 at 17:46:22 UTC, Imperatorn wrote: On Friday, 12 March 2021 at 17:37:43 UTC, David Zhang wrote: I want to store interfaces as untyped void[], then cast them back to the interface at a later time. However, it appears to produce garbage values on get(). Is this even po

Storing interfaces as void[]

2021-03-12 Thread David Zhang via Digitalmars-d-learn
I want to store interfaces as untyped void[], then cast them back to the interface at a later time. However, it appears to produce garbage values on get(). Is this even possible, and if so, what is happening here? The alternative would be a struct { CheckedPtr self; api_fns } e.g. void

Re: How to create an overwriteable struct that is always const?

2019-06-02 Thread David Zhang via Digitalmars-d-learn
Ah, fair enough.

Re: How to create an overwriteable struct that is always const?

2019-06-01 Thread David Zhang via Digitalmars-d-learn
On Saturday, 1 June 2019 at 13:00:50 UTC, ag0aep6g wrote: How is setting/replacing different from modifying? e.g.: S s; this() { s = ...; } update(S s) { this.s = s; } mod(int i) { s.i = i; } // illegal Kinda like how strings can be copied and assigned to, but not modifie

Re: How to create an overwriteable struct that is always const?

2019-06-01 Thread David Zhang via Digitalmars-d-learn
On Saturday, 1 June 2019 at 16:30:12 UTC, Jonathan M Davis wrote: If any member variable of a struct is const, then you can't modify that member ever, and assignment isn't possible unless you override opAssign so that it overwrites only the mutable members. It's very rare that it makes sense to

How to create an overwriteable struct that is always const?

2019-06-01 Thread David Zhang via Digitalmars-d-learn
Say I have a struct `S`: struct S { /*const*/ char* pointer; ... other members ... this(/*const*/ char* p, ... others ...) { pointer = p; ... } } What I want, is to be able to use `S` in other data structures with the following pr

Re: Getting a reference to an immutable string

2018-02-10 Thread David Zhang via Digitalmars-d-learn
On Saturday, 10 February 2018 at 22:59:18 UTC, ag0aep6g wrote: But there is a recent regression on Windows that might be related. Do you also have a static constructor (`static this`) that uses `wndclassName`? If so, you might be hitting issue 18412. https://issues.dlang.org/show_bug.cgi?id=1

Re: Getting a reference to an immutable string

2018-02-10 Thread David Zhang via Digitalmars-d-learn
Building with Visual Studio seems to be fine. This isn't an OptLink issue, is it?

Re: Getting a reference to an immutable string

2018-02-10 Thread David Zhang via Digitalmars-d-learn
On Saturday, 10 February 2018 at 22:36:41 UTC, ag0aep6g wrote: On 02/10/2018 11:26 PM, David Zhang wrote: I've got an immutable string declared in module scope, and I attempted to get a pointer to its characters through both &str[0] and str.ptr. However, it appears to me that the string is beh

Getting a reference to an immutable string

2018-02-10 Thread David Zhang via Digitalmars-d-learn
Hi, I've got an immutable string declared in module scope, and I attempted to get a pointer to its characters through both &str[0] and str.ptr. However, it appears to me that the string is behaving like a manifest constant, in that the pointer is null. The language reference indicates that i

Re: String copying fails when output from assert

2017-11-21 Thread David Zhang via Digitalmars-d-learn
On Tuesday, 21 November 2017 at 19:05:21 UTC, Jonathan M Davis wrote: Well, the assertion is going to throw an AssertError, which takes a string for its message. It doesn't copy the contents of the string. It's just taking a slice just like whenever you pass a string to any other function. So,

Re: String copying fails when output from assert

2017-11-21 Thread David Zhang via Digitalmars-d-learn
On Tuesday, 21 November 2017 at 18:56:03 UTC, Adam D. Ruppe wrote: On Tuesday, 21 November 2017 at 18:49:40 UTC, David Zhang wrote: assert(false, chars[0..str.length]); } What am I missing here? You're escaping a reference to a local variable there. chars[] is a pointer to the stack

String copying fails when output from assert

2017-11-21 Thread David Zhang via Digitalmars-d-learn
Hi, I've been trying to copy a string, then output it from an `assert(false)` statement, but the copy seems to be corrupted somehow. void main() { string str = "some string"; //initializing `chars` with any value doesn't do anything char[64] chars; //char[64]

Re: @nogc deduction for templated functions

2017-11-18 Thread David Zhang via Digitalmars-d-learn
Huh, I think I did something weird. It compiles now, and I don't know why. Thanks for your answers.

@nogc deduction for templated functions

2017-11-18 Thread David Zhang via Digitalmars-d-learn
Hi, Is there a way for a templated function to deduce or apply the @safe/@nogc attributes automaticaly? I feel like I remember dmd doing so at one point, but it doesn't appear to work anymore. In particular, I need to call a function belonging to a templated type, but do not know what attribu

Re: Getting the size of a type tuple

2017-09-21 Thread David Zhang via Digitalmars-d-learn
On Thursday, 21 September 2017 at 19:49:14 UTC, ag0aep6g wrote: I don't have a one-liner, but here are some other solutions that might be interesting. None of them is particularly pretty, though. 1) Recursive template: 2) Using the std library: 3) Declaring a packed struct in a function li

Getting the size of a type tuple

2017-09-21 Thread David Zhang via Digitalmars-d-learn
Given the function F, where: F(Args...)(Args args) { ... } How can I statically determine the size of the argument list in bytes? Preferably, I would like a one-liner. However, std.algorithm doesn't seem to support tuples, or `Args.each!(T => T.sizeof).sum` would work. For the moment, I've

Re: Propagating constness through function results

2017-09-17 Thread David Zhang via Digitalmars-d-learn
Nevermind! I rediscovered the `inout`attribute. Though if I may say so, I have no idea how `inout` is supposed to indicate "whatever the constness of a".

Re: Propagating constness through function results

2017-09-17 Thread David Zhang via Digitalmars-d-learn
On Sunday, 17 September 2017 at 21:18:08 UTC, David Zhang wrote: Hi, I have a class `Image`, and I have a function called `getSubImage(Rect bounds)`. What I can't figure out is how to get the result of `getSubImage()` to take on the constness of the backing image. ie. //The Image class

Propagating constness through function results

2017-09-17 Thread David Zhang via Digitalmars-d-learn
Hi, I have a class `Image`, and I have a function called `getSubImage(Rect bounds)`. What I can't figure out is how to get the result of `getSubImage()` to take on the constness of the backing image. ie. //The Image class is really just a view over a buffer that's managed elsewhere

Re: DLL loading behaviors and pragma(lib)

2017-08-11 Thread David Zhang via Digitalmars-d-learn
On Friday, 11 August 2017 at 04:50:48 UTC, rikki cattermole wrote: On 11/08/2017 12:18 AM, David Zhang wrote: I've been working on getting OpenGL to load on windows without a library, and encountered something curious; Context creation fails when I try to use the function pointer retrieved th

DLL loading behaviors and pragma(lib)

2017-08-10 Thread David Zhang via Digitalmars-d-learn
I've been working on getting OpenGL to load on windows without a library, and encountered something curious; Context creation fails when I try to use the function pointer retrieved through GetProcAddress, but works just fine with the statically linked version provided through core.sys.windows

Re: Implicit conversion from 'Ok' to 'Result' type when returning functions

2017-05-22 Thread David Zhang via Digitalmars-d-learn
On Sunday, 21 May 2017 at 10:03:58 UTC, Nicholas Wilson wrote: As in the function signature of the function you call `ok` or `error` in. Result!(int, SomeEnum) myfunc(bool foo) { if(!foo) return ok(42); else return error(SomeEnum.fooHappened); } should work. This is w

Re: Implicit conversion from 'Ok' to 'Result' type when returning functions

2017-05-21 Thread David Zhang via Digitalmars-d-learn
On Sunday, 21 May 2017 at 09:37:46 UTC, Nicholas Wilson wrote: On Sunday, 21 May 2017 at 09:29:40 UTC, David Zhang wrote: Well then it becomes Result!(T, E) ok(T,E) (T t) { return Result(t); } Result!(T, E) error(T,E)(E e) { return Result(e); } and then provided it can be inferred (e.g. f

Re: Implicit conversion from 'Ok' to 'Result' type when returning functions

2017-05-21 Thread David Zhang via Digitalmars-d-learn
On Sunday, 21 May 2017 at 09:15:56 UTC, Nicholas Wilson wrote: have free functions Result!(T, ErrorEnum) ok(T)(T t) { return Result(t); } Result!(T, ErrorEnum) error(T)(ErrorEnum e) { return Result(e); } then go if (!foo) return ok(42); else return error(Error.fooHappened); Ah, I

Implicit conversion from 'Ok' to 'Result' type when returning functions

2017-05-21 Thread David Zhang via Digitalmars-d-learn
Hi, I was reading a bit about this in Rust, and their enum type. I was wondering if this is replicate-able in D. What I've got right now is rather clunky, and involves using `typeof(return).ok` and `typeof(return).error)`. While that's not too bad, it does involve a lot more typing, and

Re: Converting a string[] to char**

2017-05-09 Thread David Zhang via Digitalmars-d-learn
On Tuesday, 9 May 2017 at 07:59:19 UTC, Stanislav Blinov wrote: On Tuesday, 9 May 2017 at 07:50:33 UTC, David Zhang wrote: If indeed there is no way to avoid allocation, do the allocations have to remain 'alive' for the duration of the instance? Or can I deallocate immediately afterwards? I c

Re: Converting a string[] to char**

2017-05-09 Thread David Zhang via Digitalmars-d-learn
On Tuesday, 9 May 2017 at 05:52:28 UTC, Mike Parker wrote: On Tuesday, 9 May 2017 at 05:38:24 UTC, ag0aep6g wrote: You have to create a new array of pointers. As rikki cattermole has pointed out, you also have to null-terminate the individual strings, and pass the amount of pointers in a sepa

Converting a string[] to char**

2017-05-08 Thread David Zhang via Digitalmars-d-learn
Hi, I'm playing around with Vulkan, and part of its initialization code calls for an array of strings as char**. I've tried casting directly (cast(char**)) and breaking it down into an array of char*s (char*[]) before getting the pointer to its first element (&a[0]). It provides the correct t

Implementing casting outside of the target struct

2017-03-16 Thread David Zhang via Digitalmars-d-learn
Hi, I have two structs and want to cast between them. The cast is generalizable, so I want to keep it outside of the structs themselves (I'll be adding more later). How can I do this? I've tried the following so far: struct Event { EventID id; ubyte[32 - EventID.sizeof] p

Re: Declaring interfaces with a constructor

2017-03-14 Thread David Zhang via Digitalmars-d-learn
On Tuesday, 14 March 2017 at 02:14:53 UTC, evilrat wrote: like this? -- [snip] - there is also way to do this using templates and duck typing, I think it will be more idiomatic way since ranges and stuff heavily use it

Re: Declaring interfaces with a constructor

2017-03-13 Thread David Zhang via Digitalmars-d-learn
On Monday, 13 March 2017 at 17:52:09 UTC, XavierAP wrote: On Monday, 13 March 2017 at 02:15:21 UTC, David Zhang wrote: What it says on the tin. Is there a way to create interfaces with a constructor or must I use an abstract class. What do you want to do in your constructor? I can't think of

Declaring interfaces with a constructor

2017-03-12 Thread David Zhang via Digitalmars-d-learn
What it says on the tin. Is there a way to create interfaces with a constructor or must I use an abstract class. Additionally, is there a way to force the linker to link a function in a class without an implementation with another that does have an implementation? i.e. --- //module a;

Re: Declaring constant references in struct members

2017-02-15 Thread David Zhang via Digitalmars-d-learn
On Thursday, 16 February 2017 at 00:49:45 UTC, Adam D. Ruppe wrote: On Thursday, 16 February 2017 at 00:43:30 UTC, David Zhang wrote: struct S { O object; } import std.typecons; Rebindable!O object; http://dpldocs.info/experimental-docs/std.typecons.Rebindable.html Is there a similar

Declaring constant references in struct members

2017-02-15 Thread David Zhang via Digitalmars-d-learn
Hi, Say I have a struct S that holds a reference to an object O. Is there a way to express that I want to be able to change the reference, but not what the reference points to? Thanks. struct S { O object; } class O { size_t things. }

Re: Creating an array of immutable objects

2017-02-14 Thread David Zhang via Digitalmars-d-learn
Thanks for your answers. Out of curiosity though, how could something like this be done with classes instead?

Creating an array of immutable objects

2017-02-13 Thread David Zhang via Digitalmars-d-learn
Hi, I have a struct with two immutable members, and I want to make an array of them. How do I to this? I'm using allocators for this. string[] paths; struct FileDesc { immutable string path; immutable uint index; } _fileDesc = /*something*/; You can't use alloc.makeArray because it

How does cast(SomeObj) (cast(void*) ptrFromC) work?

2017-01-25 Thread David Zhang via Digitalmars-d-learn
I was trying to figure out why calling an object's function from a wndProc that modified the object's state didn't actually change anything. Wrapping the GetWindowLongPtr in a cast(void*) seems to make it work. What am I missing about this? I though that object references were really just point

Object function cannot change member when called from callback?

2017-01-24 Thread David Zhang via Digitalmars-d-learn
So I have a window (Windows), and my wndProc is basically the same as the one on the windows guides. However, even though WM_CLOSE gets passed (and I can use if(msg == WM_CLOSE)), I can't seem to set my shouldClose flag. I've confirmed that I still get the event within my processMessage method.

Re: Accessing a function within an object's superclass from the outside

2017-01-14 Thread David Zhang via Digitalmars-d-learn
On Saturday, 14 January 2017 at 22:17:23 UTC, ketmar wrote: class ClassB: ClassA { alias fun = super.fun; override void fun(uint a) {} } I tried that, but it seems to think I mean to override super.fun(uint) instead of super.fun(uint, float). Looking at my code again, one of them is temp

Accessing a function within an object's superclass from the outside

2017-01-14 Thread David Zhang via Digitalmars-d-learn
Hello, Say I have a class, and it overrides a function from its superclass. However, the superclass has two overloaded versions of the same function. My intent is to override only one of the functions, but the second is rendered inaccessible from outside of the class. How can I make the overl

Re: Unittest hangs on completion

2016-12-30 Thread David Zhang via Digitalmars-d-learn
On Saturday, 31 December 2016 at 02:36:01 UTC, rikki cattermole wrote: No, my understand is thus: next = current.next; theAllocator.dispose(current); When current is deallocated, current is pointing to free'd memory. After that point it should be segfaulting when you try to access it *I thi

Re: Unittest hangs on completion

2016-12-30 Thread David Zhang via Digitalmars-d-learn
On Saturday, 31 December 2016 at 02:03:07 UTC, rikki cattermole wrote: As it should, current is never reassigned. You only need one var, next. Of course I didn't read the entire thread chain so, I'm probably missing something. import std.experimental.allocator; void main() { struct S

Re: Unittest hangs on completion

2016-12-30 Thread David Zhang via Digitalmars-d-learn
Extracting everything into a main() also causes the application to hang. ie: struct S { S* next; } S* _foo; foreach (e; 0 .. 10) _foo = theAllocator.make!S(_foo); S* next, current; next = current = _foo; while (next) { next = current.next; theAllocator.dispose(current); }

Re: Unittest hangs on completion

2016-12-30 Thread David Zhang via Digitalmars-d-learn
On Friday, 30 December 2016 at 22:42:07 UTC, Steven Schveighoffer wrote: What is actually happening is that the D main function returns. Then the D runtime tears down everything, including joining all threads, running all module static dtors, terminating the GC, etc. Then it returns to the OS

Re: Unittest hangs on completion

2016-12-30 Thread David Zhang via Digitalmars-d-learn
On Friday, 30 December 2016 at 20:59:30 UTC, Seb wrote: On Friday, 30 December 2016 at 18:03:44 UTC, David Zhang wrote: On Friday, 30 December 2016 at 14:12:35 UTC, Steven Schveighoffer wrote: [snip] It depends on what is actually hanging the process. If it's something in your code base only,

Re: Unittest hangs on completion

2016-12-30 Thread David Zhang via Digitalmars-d-learn
On Friday, 30 December 2016 at 14:12:35 UTC, Steven Schveighoffer wrote: [snip] It depends on what is actually hanging the process. If it's something in your code base only, then nobody else would be seeing it. -Steve So, what should I do with it? I'd submit a bug report, but I don't know

Re: Unittest hangs on completion

2016-12-29 Thread David Zhang via Digitalmars-d-learn
On Friday, 30 December 2016 at 01:25:50 UTC, Steven Schveighoffer wrote: Looks like that comes from here: https://github.com/dlang/dub/blob/master/source/dub/dub.d#L577 I have serious doubts that this is the correct way to run tests, as share ctors are supposed to have run BEFORE unit tests a

Re: Unittest hangs on completion

2016-12-29 Thread David Zhang via Digitalmars-d-learn
On Friday, 30 December 2016 at 00:44:50 UTC, Steven Schveighoffer wrote: Where does the "All unit tests have been completed successfully." message come from? That's not standard D, which prints nothing. -Steve I should have mentioned that I use dub then, shouldn't I? Anyway, this is what

Re: Unittest hangs on completion

2016-12-29 Thread David Zhang via Digitalmars-d-learn
On Thursday, 29 December 2016 at 20:50:54 UTC, David Zhang wrote: On Thursday, 29 December 2016 at 20:33:33 UTC, Stefan Koch wrote: It would be very helpful if you could provide example code that triggers that behavior. I'd love to, but I'm not actually sure just what it is that breaks it. I

Re: Unittest hangs on completion

2016-12-29 Thread David Zhang via Digitalmars-d-learn
On Thursday, 29 December 2016 at 20:33:33 UTC, Stefan Koch wrote: It would be very helpful if you could provide example code that triggers that behavior. I'd love to, but I'm not actually sure just what it is that breaks it. I can provide the git repo for one of them though though: https://

Unittest hangs on completion

2016-12-29 Thread David Zhang via Digitalmars-d-learn
Hi, I've noticed recently, that whenever I unittest, it program hangs either at the very end, or right before they start. When using vanilla unit tests, the program appears to hang after the "All unit tests have been completed successfully." message, and I have to force to program to exit. Ho

Re: Allocating a class within another class during object init w/o passing in an allocator

2016-12-16 Thread David Zhang via Digitalmars-d-learn
I haven't considered alignment here. I'm not sure if you have to. I though all classes were aligned to sizeof(size_t) boundaries? Wouldn't it then just be align(sizeof(size_t)) byte[__traits(classInstanceSize, SomeClass)] scStorage;

Re: Allocating a class within another class during object init w/o passing in an allocator

2016-12-15 Thread David Zhang via Digitalmars-d-learn
On Thursday, 15 December 2016 at 21:08:51 UTC, ag0aep6g wrote: On 12/15/2016 09:51 PM, David Zhang wrote: However, it leaves me with another question, how much (if any) space would the static array require from the class? Depends on SomeClass. The array's size is just the value of __traits(c

Re: Allocating a class within another class during object init w/o passing in an allocator

2016-12-15 Thread David Zhang via Digitalmars-d-learn
Thank you for your responses. Visitor, I don't want any reference to an allocator within the class if I can avoid it. ag0aep6g, thanks! That's what I was looking for. However, it leaves me with another question, how much (if any) space would the static array require from the class? It's not a s

Allocating a class within another class during object init w/o passing in an allocator

2016-12-15 Thread David Zhang via Digitalmars-d-learn
Hello, It is my understanding that a class can have a struct as one of its members, and it will be allocated in-line with the rest of the class' members. My question is this; how might I be able to do this with another class? I want to be able to allocate Foo using std.experimental.allocator