News for hackers. Stuff that matters.
Re:Hobby x86 kernel written with Zig by gw on Monday January 06, 2020 @10:17AM (#21969720)
comptime does not replace macros though...there is no way to manipulate AST in zig, nor will there ever be, according to the author.
Re:Hobby x86 kernel written with Zig by curtisf on Monday January 06, 2020 @11:24AM (#21970343)
You can't manipulate ASTs (ie, Zig code), but nothing stops you from parsing string literals however you want.

For example, I wrote a PEG-like parser combinator library in Zig. Using it currently [looks like this](https://github.com/CurtisFenner/zsmol/blob/87de4c77dd8543011...). However, as a library, I ^could provide a function that looks like

    pub const UnionDefinition = comb.fromLiteral(
        \\ _: KeyUnion
        \\ union_name: TypeIden
        \\ generics: Generics?
        \\ implements: Implements?
        \\ _: PuncCurlyOpen
        \\ fields: Field*
        \\ members: FunctionDef*
        \\ _: PuncCurlyClose 
   );
etc. But, I find reading the code as it is good enough for now that I didn't want to spend time implementing such a library.

[^]: Being able to create brand new types at `comptime` isn't [yet implemented](https://github.com/ziglang/zig/issues/383), so this can't quite be done yet, though you could fake it with `get`/`set` methods instead of real fields

Re:Hobby x86 kernel written with Zig by pron on Monday January 06, 2020 @10:48AM (#21969988)
Well, it is intentionally weaker than macros (and I agree with Zig's designer that that's a very good thing, though it is a matter of taste), but it does replace many of the cases where in Rust you'd have to use macros (or the preprocessor in C/C++). So it replaces macros everywhere where it deems their usage reasonable.
Re:Hobby x86 kernel written with Zig by gw on Monday January 06, 2020 @11:19AM (#21970299)
It is a legit design choice, but it does detract from your comment about language complexity. Not having AST macros inherently adds complexity to a language by requiring features to be built into the compiler rather than be implemented as libraries.
Re:Hobby x86 kernel written with Zig by pron on Monday January 06, 2020 @11:34AM (#21970460)
Those are different kinds of complexity. You're talking about the effort required by the implementor of the compiler. I'm talking about the effort required by the programmer using the language.
Re:Hobby x86 kernel written with Zig by gw on Monday January 06, 2020 @11:43AM (#21970554)
Then you aren't talking about complexity (an objective quality), you are talking about difficulty to read (a subjective quality relative to the reader). There is no doubt that macros can make a given piece of code harder to read, if the reader is unfamiliar with the macro being used. Complexity describes how intertwined different pieces of something are internally, which has nothing to do with a given vantage point.
Re:Hobby x86 kernel written with Zig by pron on Monday January 06, 2020 @11:47AM (#21970592)
No, that's just how Rich Hickey describes complexity; it's hardly a universal definition. For example, in computer science, the complexity of a task is often a measure of the effort, in time or memory, required to perform it.
Re:Hobby x86 kernel written with Zig by gw on Monday January 06, 2020 @12:00PM (#21970724)
English is certainly not free of ambiguity, but in the past i've seen you heavily emphasize precision in word choice, so it's surprising to see you de-emphasize it here. Nobody is a final arbiter of definitions, but the distinction i'm making is not a trivial one, and Hickey isn't the only one to have made it. Even thinking about it colloquially, how often do you follow the word "complex" with an infinitive verb describing an action? A rube goldberg machine is complex...and hard to build!
Re:Hobby x86 kernel written with Zig by pron on Monday January 06, 2020 @12:09PM (#21970815)
I'm not deemphasizing it, I'm just saying that we're talking about different meanings of "complexity" here and there is no well-accepted definition. My "complexity" refers to the effort required by the programmer when understanding programs written in the language.
Re:Hobby x86 kernel written with Zig by gw on Monday January 06, 2020 @12:41PM (#21971132)
Fair enough, ron. I won't belabor it further. I'll just leave this: Long ago, after coming across a very useful distinction between the words "practical" and "pragmatic," i intentionally changed my usage of those words as a result. Not because a charismatic person told me to, but because it was useful. If a distinction is useful to make, start making it, my man!
Re:Hobby x86 kernel written with Zig by AnIdiotOnTheNet on Monday January 06, 2020 @10:52AM (#21970023)
Which is the right call really. It is valuable to have the code you're looking at actually be what it appears to be.
Re:Hobby x86 kernel written with Zig by mratsim on Monday January 06, 2020 @11:08AM (#21970201)
I don't agree.

For many domains, being able to implement a domain-specific language with a set of expressive rules for the domain is an incredible productivity boost and also prevents many mistakes because you cannot represent them.

Not being able to manipulate the AST means that you are restricted on the embedded DSLs that you can provide. And embedded DSLs encompasses code generation for:

- state machines

- parsing grammars (PEGs for example)

- shader languages

- numerical computing (i.e. having more math like syntax to manipulate indices)

- deep learning (neural network DSL)

- HTML templates / generators

- monad composition

There is a reason most people are not building in Assembly anymore, there is a right-level of abstraction for every domain. A language that provides building block for domain expert to build up to the right level of abstraction is very valuable.

Re:Hobby x86 kernel written with Zig by pron on Monday January 06, 2020 @11:17AM (#21970284)
The question is, as always, at what cost?

Low-level languages (aka "systems programming" languages) already suffer from various constraints that increase their accidental complexity. Is it really necessary to complicate those particular languages further to support embedded DSLs?

I don't think there's a universal right or wrong answer here, but there is certainly a big question.

Re:Hobby x86 kernel written with Zig by gw on Monday January 06, 2020 @11:32AM (#21970432)
Ironically, the lack of macros leads to an explosion of ad-hoc, extra-language DSLs. Look at rules engines, for example. In Drools (java), you have to write rules with a special language, DRL. Meanwhile in Clara (clojure) you write your rules in clojure. Macros simplify languages, they don't complicate them.
Re:Hobby x86 kernel written with Zig by simias on Monday January 06, 2020 @03:51PM (#21973170)
It's always a trade-off and placing the cursor correctly is tricky. Things like macros, operator overloading, metaprograming, virtual calls, exceptions or even function pointers are effectively "obfuscating" code by having non-obvious side effects if you don't have the full context. On the other hand if you push the idea too far in the other direction you end up with basically assembly, where you have an ultra-explicit sequence of instruction to execute.

It's very easy to come up with examples of terrible abuse of these features that lead to bad code (like for instance if somebody was insane enough to overload the binary shift operator << to, I don't know, write to an output or something) but it also gives a lot of power to write concise and expressive code.