Can’t use UFCS to create InputRange?
struct R {} int front(R r) { return 42; } void popFront(R r) {} bool empty(R r) { return false; } void main() { import std.range.primitives : isInputRange; static assert(isInputRange!R); } Error: static assert: `isInputRange!(R)` is false Whats really weird is that if I replace isInputRange with its definition from std.range.primitives, it returns true: import std; struct R {} int front(R r) { return 42; } void popFront(R r) {} bool empty(R r) { return false; } void main() { static assert(is(typeof(R.init) == R) && is(ReturnType!((R r) => r.empty) == bool) && is(typeof((return ref R r) => r.front)) && !is(ReturnType!((R r) => r.front) == void) && is(typeof((R r) => r.popFront))); } This compiles. What’s going on here?
So how do I find and remove an element from DList?
auto list = DList!int([1, 2, 3, 4]); list.remove(list[].find(2).take(1)); Error: function std.container.dlist.DList!int.DList.remove(Range r) is not callable using argument types (Take!(Range)) It works if I replace `remove` with `linearRemove`, but that defeats the whole purpose of using a doubly linked list.
Re: So how do I find and remove an element from DList?
On Friday, 10 July 2020 at 19:23:57 UTC, Steven Schveighoffer wrote: It's not linear over the size of the list, it's linear over the size of the range. If you are always removing 1 element, it's effectively O(1). -Steve I see. Thanks!
Re: Is there any writeln like functions without GC?
If your goal is to debug your @nogc code, you can use writeln in debug statement: @nogc void main() { debug writeln("hello, debug world!"); }
Compile time int to string conversion in BetterC
It’s 2022 already and BetterC still imposes limits at compile time and makes things awkward. I have 3 integer enums that represents my library version. And I want to generate a "1.2.3" enum from them. This is a trivial thing to do in standard D but I can’t find a way to do it with BetterC enabled. I can’t use `to`, `text`, `sformat` or `toChars`, as none of them is compatible with BetterC. And I can’t use `sprintf` either because ‘stdc’ is not available at compile time. On the other hand, string to integer conversion is simple: just ‘mixin’ it! So as a workaround, I put numbers into private string enums, and the integer enums are generated from them. But this is not nice. Maybe I’m missing something?
Re: Compile time int to string conversion in BetterC
On Wednesday, 17 August 2022 at 10:38:33 UTC, Dennis wrote: I had the same problem, and came up with the following trick: ```D enum itoa(int i) = i.stringof; enum major = 3; enum minor = 2; enum patch = 1; enum versionString = itoa!major ~ "." ~ itoa!minor ~ "." ~ itoa!patch; static assert(versionString == "3.2.1"); ``` Nice! Thank you, I’ll use this.
Re: Interfacing with basic C++ class
On Wednesday, 28 September 2022 at 19:57:10 UTC, Riccardo M wrote: I think I am stuck in the easiest of the issues and yet it seems I cannot get around this. I have a C++ file: ``` class MyClass { public: int field; MyClass(int a) : field(a) {} int add(int asd) { return asd + 1; } }; ``` Ali is correct. However, you don’t have to change anything on the C++ side, just map C++ class to D struct: ```D extern(C++, class) { struct MyClass { public: int field; @disable this(); int add(int asd); //struct fields are always `final` } MyClass* instantiate(int asd); //mind the * } ```
Re: Interfacing with basic C++ class
On Thursday, 29 September 2022 at 12:49:06 UTC, Riccardo M wrote: On Thursday, 29 September 2022 at 11:13:15 UTC, Ogi wrote: So it turns out that D's structs are a much better match for C++'s classes in this case. But why is this? Can you elaborate? It must have to do with the fact that D structs are passed by value? In C++, class and struct are basically the same thing (AFAIK the only difference is that struct members are public by default). In D, they are two very different things: classes are reference types — structs are value types, classes support inheritance — structs only support `alias this`, and so on. When interfacing to C++, disregard the keyword and look at the implementation instead. If all its member functions are non-virtual, map it to struct. Otherwise map it to class. If it defines at least one pure virtual member function, map it to abstract class. If all its member functions are either pure virtual or non-virtual and it contains no fields, map it to interface. Sounds complicated? Well, that’s because C++ is complicated. However the 'add' function only links correctly if C++ has the function body defined outside of its class. ``` // C++ int MyClass::add(int asd) { return field + asd; } ``` If the function is defined inside its class, it is an undefined reference at link time. Once again I ask for clarifications and workarounds, if possible. In C++, member functions defined inside its class are called *inline* member functions. In contrast to normal functions which must be defined once and only once in your program, inline functions must be defined in every translation unit that uses them. Let’s replicate your linking error in C++: ```C++ //c.h class C { public: int foo() { return 42; } void bar(); }; ``` ```C++ //c.cpp #include "c.h" void C::bar() { /* ... */ } ``` ```C++ //main.cpp #include //Let’s see what happens if we forget `C::foo` definition: class C { public: int foo(); void bar(); }; int main() { auto c = new C(); printf("%d\n", c->foo()); c->bar(); return 0; } ``` ``` $ clang++ -c c.cpp $ clang++ -c main.cpp $ clang++ main.o c.o main.o : error LNK2019: unresolved external symbol "public: int __cdecl C::foo(void)" (?foo@C@@QEAAHXZ) referenced in function main a.exe : fatal error LNK1120: 1 unresolved externals clang++: error: linker command failed with exit code 1120 (use -v to see invocation) ``` Copying `C::foo` definition to `main.cpp` will fix this. Of course, we could just include `c.h`. Same goes for D. `MyClass.add` must be defined in your D module.
Solving optimization problems with D
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: better video rendering in d
On Tuesday, 21 March 2023 at 16:57:49 UTC, monkyyy wrote: My current method of making videos of using raylib to generate screenshots, throwing those screenshots into a folder and calling a magic ffmpeg command is ... slow. Why not use ffmpeg as a library? Here are the [bindings](https://code.dlang.org/packages/ffmpeg-d).