[HN Gopher] Prep: Golang Comptime
___________________________________________________________________
Prep: Golang Comptime
Author : Seb-C
Score : 96 points
Date : 2024-09-27 17:23 UTC (3 days ago)
(HTM) web link (github.com)
(TXT) w3m dump (github.com)
| andyferris wrote:
| The cool thing that zig comptime does is that it interacts with
| the type system. This seems geared around computations, more than
| e.g. defining structs.
| pa7ch wrote:
| I wonder what Go would look like if it had generics removed but
| something like comptime added.
| fjasdddwsfasd wrote:
| I always felt like if and when Go chose to implement Generics
| it would be like what Zig did today.
| lenkite wrote:
| comptime really should have been the approach to Generics taken
| by Golang instead of trying to blindly follow Java/C++ . It
| would have also fit into Go's philosophy of keeping it standard
| and simple - no separate mini language for generics . Wish the
| language designers had looked at Zig before rolling out
| generics.
| Groxx wrote:
| TIL `go build -toolexec`. That... seems like it'd enable a lot of
| interesting shenanigans...
|
| I'm somewhat surprised by how little I see online about it.
| SPascareli13 wrote:
| I was thinking about that just the other day, who it would be
| really cool if Go had compile time code execution. I think Jai is
| making that a very prominent feature of the language.
| latenightcoding wrote:
| re: Jai. I have never seen a language that is not even
| available get so much love and benefit of doubt.
| whateveracct wrote:
| cult of personality
| jonathrg wrote:
| That's pretty much it. Jonathan Blow's strongly worded
| negative opinions on most things attracts a certain kind of
| person who will go on to evangelize.
| SPascareli13 wrote:
| I don't know if there's ever been a language that was
| announced 10 years before its first closed beta. I don't even
| know if having a "closed beta" for a language is something
| that really happens.
|
| So there's a lot that is different with Jai, it's more like a
| highly anticipated game than a language.
| lenkite wrote:
| Isn't Jai still vapourware after a decade ? How does one even
| get the compiler and build programs ?
| binwiederhier wrote:
| Fascinating.
|
| There are some other interesting repos from the same author,
| namely https://github.com/pijng/goinject, which lets you inject
| code as part of preprocessing. Feels a lot like Java's annotation
| magic.
|
| Thanks for sharing. I wasn't even aware Go had pre-processors, or
| that modifying the AST like that is even possible.
| tzury wrote:
| In an older project I have had a code file generated with
| constants to get same results.
|
| That is nice. Makes coding and build flow simple, clean and
| easier to maintain.
| chriscbr wrote:
| Neat concept.
|
| I had a similar idea a few months ago about whether it's possible
| to achieve some form of comptime with TypeScript. I didn't get
| too far unfortunately, but I think the implementation would need
| to interact with the TypeScript compiler API in some way.
| sluongng wrote:
| This is just integrated codegen into the build tool, right?
| Datadog has a similar project here
| https://github.com/DataDog/orchestrion that does the same thing.
|
| I think this kind of code gen is very wonky, and hard to debug
| unless there is some official API support for it. Using a
| sufficiently advanced build tool to handle this in a separate
| build step would help as well. But that usually causes problems
| with IDE support.
| jakewins wrote:
| Yeah, this reminds me a lot of similar practices in the JVM
| world with compile time annotation processors.
|
| Bugs in those systems are hell to trace.
|
| Coming to the Go world of "if you're gonna do code generation,
| write a dumb program that generates source code text, run it
| and check the result into source control" was a revelation -
| yeah, it's ugly, but guess what? It can be debugged by human
| beings.
| elcritch wrote:
| It depends more on whether the language tooling supports it.
| I used Lombok back in the day with Netbeans. When properly
| setup it would pop to the generated code when debugging or if
| you went to a definition, pretty easy.
| gwd wrote:
| Adding "-a" is probably going to slow down your compile times by
| quite a bit. If I were going to use something like this, I'd try
| to ensure the results only depended on things in the same file. I
| wonder if there's a way for `prep` to warn when the result
| depends on something outside the file.
|
| If there were language support for this sort of thing, it could
| track the dependencies itself.
| mst wrote:
| I'd been looking at https://github.com/cosmos72/gomacro/ on and
| off to get similar functionality.
|
| (and by "looking at" I mean "kept meaning to try it but totally
| haven't got round to it yet" but I still think it's worth a look)
| bsaul wrote:
| I don't know if zig is going to become mainstream, but comptime
| is such a brilliant yet simple idea (the best ones) that it's
| already written its name in the hallmark of programming
| languages.
| syngrog66 wrote:
| Other than a traditional compiler use case and stage, "codegen"
| is an obvious anti-pattern both for security and for a
| maintainer's complexity burden, and thus to be avoided.
|
| Just say no, kids. Life _will_ be better.
___________________________________________________________________
(page generated 2024-09-30 23:02 UTC)