3 ms·
constexpr/consteval/comptime/etc is a game changer for the systems programmer and it feels so close but yet so far. Can someone speak to what language has the b
by nickysielicki 4y ago
constexpr/consteval/comptime/etc is a game changer for the systems programmer and it feels so close but yet so far. Can someone speak to what language has the best support for this? Some things that I feel are missing in C++:
* arbitrary file I/O. I can take my compile time data and write a python script to put it in a std::array, but I shouldn’t have to.
* non-fixed-sized containers, ie vector
* a generic memoization utility in the standard library for caching. If I want my function to handle any input at runtime, but I want to pre-populate a cache at compile time for values that I know will be called, it’s doable but not as easy as it should be. (In general, given the amount of algorithms that rely on memoization, I’m somewhat surprised that Python is the only language I know that makes memoization as easy as a decorator).
- formerly_proven 4y ago> * arbitrary file I/O. I can take my compile time data and write a python script to put it in a std::array, but I shouldn’t have to. https://ziglang.org/documentation/0.9.1/#embedFile https://ziglang.org/documentation/0.9.1/#embedFile I haven't tried, but because it should just be equivalent to a string literal you should be able to further process it using comptime as well. > * non-fixed-sized containers, ie vector One way is to just have a function count how many entries you need and use arrays. This is all pretty straightforward because comptime. Real dynamic comptime containers are dependent on making a comptime allocator available. There's a ticket for that: https://github.com/ziglang/zig/issues/1291 https://github.com/ziglang/zig/issues/1291 > * a generic memoization utility in the standard library for caching. If I want my function to handle any input at runtime, but I want to pre-populate a cache at compile time for values that I know will be called, it’s doable but not as easy as it should be. (In general, given the amount of algorithms that rely on memoization, I’m somewhat surprised that Python is the only language I know that makes memoization as easy as a decorator). I don't think it's possible to use comptime to generate wrapper functions duplicating the signature of a given function, at least I haven't found a way to do so. The function signature has to be spelled out in the comptime code.
- dataangel 4y agoAll of this is already done in Lisp. Greenspun's 10th rule.
- ArtixFox 4y ago...is there any lisp that can be used for systems programming and that does NOT produce huge binaries, does not use GC, generates fast n small binaries and is still interactive? everyone claims lisp can be used for anything, same goes for forths, but i cant see a lisp like that, and about forths...none of the free ones are fast, and the fast ones are still slow on the same note, lets say if someone is creating something[i am thinking of] like this, what would be a better option, go the forth way or the lisp way cuz both offer same stuff to some extent
- throwawaymaths 4y agoI mean there was a "lisp machine" in the distant past that actually ran lisp on metal. Does that count?
- ArtixFox 4y agoobviously no, we dont care about a language that was once used in a machine specifically for it, cuz we cant go in past and program in that specific machine. i think ive realised what is up with lisps, forths, etc. theoretically you can build all kinds of beautiful abstractions over it and use them for anything but in reality these languages cant do that and/or their communities dont care to implement it/its not their goal it might sound bad but it is what it is if i would have to say it in a worse way, these languages are all talk
- ntoskrnl 4y ago> Can someone speak to what language has the best support for this? I can give you the Rust perspective, but I'm not sure it's the best. > arbitrary file I/O include_str! and include_bytes! make the contents of a file available as a string or a byte array. More complex types would need a build script (or transmute). > non-fixed-sized containers, ie vector No support in `const fn`, but you can again use a build script as an escape hatch. > a generic memoization utility in the standard library for caching There's nothing in the stdlib, but there are a few third party crates that give you the easy one-line syntax, such as https://crates.io/crates/memoize https://crates.io/crates/memoize
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- jdrek1 4y ago> arbitrary file I/O. I can take my compile time data and write a python script to put it in a std::array, but I shouldn’t have to. There's a proposal for that https://open-std.org/JTC1/SC22/WG21/docs/papers/2020/p1040r6.html https://open-std.org/JTC1/SC22/WG21/docs/papers/2020/p1040r6... > non-fixed-sized containers, ie vector You can already use them in a constexpr context, you just can't leave it yet. So creating a vector on compile time and then using it on runtime is sadly not working right now but I think that's being worked on as well.