[OT] C vs C++

H. S. Teoh hsteoh at qfbox.info
Tue Sep 13 20:59:48 UTC 2022


On Sat, Sep 10, 2022 at 05:05:18PM +0000, JG via Digitalmars-d wrote:
> On Saturday, 10 September 2022 at 13:58:51 UTC, H. S. Teoh wrote:
[...]
> > This is another disadvantage of using templates/mixins: external
> > tools have to be able to parse templates and magically infer what
> > exactly is the mixed-in string, in order to help you.  An
> > externally-generated .d file is just regular D code, you just use
> > standard code utilities on it and you're ready to go.
[...]
> I agree in part with what you say, perhaps I will try it again
> sometime.  My last attempt was a few years ago being fed up writing go
> and not having templates. So I wrote a program that transformed "my
> templated go" into go.  For small things it was fine but when I tried
> to use it more seriously I found trying to bug fix it painful.
[...]

If I'm not mistaken, sounds like what you had wasn't so much a code
generator as a Go preprocessor, of sorts.  I usually don't bother with
codegen in this case, because D's templates work wonderfully.  The kind
of codegen that I'd need an external codegen to do, is usually when
there's some external data source or API driving the codegen (which in
theory I can do purely in D with CTFE and reflection, but at
considerable compile-time cost).

For example, automatically generating D APIs for GLSL shader uniform /
vertex attribute bindings by inspecting GLSL code.  The generated D code
allow me to use nice object.field syntax to set these bindings,
eliminating a whole bunch of fiddly boilerplate that would be very
error-prone to write manually.

Or processing input 3D data files and auto-generating code for uploading
vertex data converted to the appropriate format to the GPU, and
exporting a nice API for manipulating these objects. Especially in this
case, although in theory it's possible to load the data files with
string imports and process them in CTFE, the result would be compilation
unacceptably slow.  By processing them separately, I only need to
generate the target .d file once, and it can be compiled as a normal
source file, and only need to be regenerated when something changes in
the input data files.


T

-- 
You are only young once, but you can stay immature indefinitely. -- azephrahel


More information about the Digitalmars-d mailing list