Live data from Hacker News

Zig's Lovely Syntax

matklad.github.io

21–30 of 246 posts

Re: Zig's Lovely Syntax

#21
post #8

"Zig doesn’t have lambdas" This surprises me (as a C++ guy). I use lambdas everywhere. What's the standard way of say defining a comparator when sorting an array in Zig?

Same as C, define a named function, and pass a pointer to the sorting function.

Re: Zig's Lovely Syntax

#22
post #12

> Like Rust, Zig uses 'name' (':' Type)? syntax for ascribing types, which is better than Type 'name' I'm definitely an outlier on this given the direction all syntactically C-like new languages have taken, but I have the opposite preference. I find that the most common reason I go back to check a variable declaration is to determine the type of the variable, and the harder it is to visually find that, the more annoy…

> In C or C++ the type would take the place of that unnecessary `let`

In my opinion the `let` is not so unnecessary. It clearly marks a statement that declares a variable, as opposed to other kind of statements, for example a function call.

This is also why C++ need the "most vexing parse" ambiguity resolution.

Re: Zig's Lovely Syntax

#23
post #18

Having @ and .{ } all over the place is hardly lovely, as is having modules like JavaScript's CJS.

It's especially bad on a qwertz keyboard as well.

That's why "real programmers" use English keyboard layout regardless of the physical keyboard ;)

Curly brace syntax would never have been invented in Europe (case in point: Python and Pascal).

Re: Zig's Lovely Syntax

#24
post #21
post #8

"Zig doesn’t have lambdas" This surprises me (as a C++ guy). I use lambdas everywhere. What's the standard way of say defining a comparator when sorting an array in Zig?

Same as C, define a named function, and pass a pointer to the sorting function.

And what if you need to close over some local variable?

Re: Zig's Lovely Syntax

#25
post #21
post #8

"Zig doesn’t have lambdas" This surprises me (as a C++ guy). I use lambdas everywhere. What's the standard way of say defining a comparator when sorting an array in Zig?

Same as C, define a named function, and pass a pointer to the sorting function.

Unlike C you can stamp out a specialized and typesafe sort function via generics though:

https://ziglang.org/documentation/master/std/#std.sort.binar...

Re: Zig's Lovely Syntax

#26

Earlier quoted context omitted.

It's especially bad on a qwertz keyboard as well.

That's why "real programmers" use English keyboard layout regardless of the physical keyboard ;) Curly brace syntax would never have been invented in Europe (case in point: Python and Pascal).

Oh for sure. I am using qwerty myself. And my fav language (f#) has relatively few curly braces.

Re: Zig's Lovely Syntax

#27
post #14
post #7

Earlier quoted context omitted.

I am an exception then. Rust may be my favourite language but the syntax is pretty awful and one of its biggest weaknesses. I also love Ruby but I am pretty meh about its syntax.

I don't think many people would start to use a language unless the found the syntax at least a little simpatico - but I guess they could be drawn by the semantics it provides.

I (almost) don't consider syntax at all when considering a language, just whether it's suitable for the task.

The exception is languages with syntax that require a non-standard keyboard.

Re: Zig's Lovely Syntax

#28
I like Zig as well, but I won't call its syntax lovely. Go shows you can do pretty well without ; for line breaks, without : for variable types etc.

But sure, if you only compare it with Rust, it is a big improvement.

Re: Zig's Lovely Syntax

#29
post #21

Earlier quoted context omitted.

Same as C, define a named function, and pass a pointer to the sorting function.

And what if you need to close over some local variable?

Not possible, you'll need to pass the captured variables explicitly into the 'lambda' via some sort of context parameter.

And considering the memory management magic that would need to be implemented by the compiler for 'painless capture' that's probably a good thing (e.g. there would almost certainly be a hidden heap allocation required which is a big no-no in Zig).

Re: Zig's Lovely Syntax

#30
post #5

In my experience, everyone finds the syntax of their favourite language lovely - I love (mostly) C++.

Not me!

I don't like the syntax of Lisps, with the leading parenthesis to begin every expression.

I use it anyway because it's so useful and powerful. And I don't have any better ideas.

Post reply on HN