Live data from Hacker News

Zig: programming language designed for robustness optimality and clarity [video]

youtube.com

51–60 of 73 posts

Re: Zig: programming language designed for robustness optimality and clarity [video]

#51

Earlier quoted context omitted.

Do nullable types and maybe types add much over checked dereferences (a la Java)? I imagine they might be good for performance (fewer checks), but does it really help with correctness/convenience/elegance/readability/etc?

Yes, because you get told about problems at compile-time rather than at runtime.

I think I get it now: use different types for nullable and non-nullable pointers. Only non-nullable pointers can be dereferenced, and the conversion from nullable to non-nullable must be done explicitly, with different flow in the case that the pointer turns out to be null (which is to say, a non-nullable pointer will never enter scope).

Dereferencing null is impossible, and the programmer is forced to explicitly handle null values.

This contrasts with C, where the same type is used for a nullable and a non-nullable pointer, so the compiler can't help out, the programmer is at risk of forgetting/failing to keep track of the difference between the two, and null-dereferences may occur (and give you undefined behaviour).

Java references take the same approach as C pointers, except all dereferences are checked at runtime, and dereferencing null throws an exception.

I like it! It does better than the Never null, only cromulent values approach (like C++ references), as this can be inconvenient in practice. It makes null-dereferences impossible, and doesn't do anything funky that would introduce needless runtime overhead.

Re: Zig: programming language designed for robustness optimality and clarity [video]

#52
post #12

Earlier quoted context omitted.

> - No hidden control flow That means no destructors, right? edit Ah, if I read the documentation correctly, there are destructors.

I think it means no destructors, no overloaded operators, no automatic getter/setters for properties. Nothing that does a function call unless you can immediately tell by looking at it that it’s a function call.

You're right - there's no proper RAII (but there's a 'defer' keyword, which runs your expression on scope-exit, like BOOST_SCOPE_EXIT), no operator overloading, no exceptions, and all function calls look like function calls. It does have a language feature for handling error-codes though. [0] [1] [2]

Contrast with C#'s 'properties', where a method call (which might throw) is disguised as reading/writing a member.

My favourite example of unexpected semantics is D's lazy keyword, where, at the call-site, you have no idea whether your argument will be evaluated lazily or eagerly! [3]

C# has a pass-by-reference keyword which modified the way an argument is treated, but it has the sense to force use of the keyword at the call-site too, so that everything is clear. [4]

I like the language's philosophy, I'll have to keep an eye on it. I suspect they'd do well to have the language compile to C, though. Is there any reason that wouldn't be a good fit? I see it has a templates system, but at (very) first glance I don't see anything that wouldn't map cleanly to C.

[0] https://ziglang.org/documentation/master/#defer [1] https://ziglang.org/download/0.1.1/release-notes.html [2] https://andrewkelley.me/post/intro-to-zig.html [3] https://dlang.org/articles/lazy-evaluation.html [4] https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...

Re: Zig: programming language designed for robustness optimality and clarity [video]

#53
post #43

I've been advised to a write a preprocessor so that I can make a language that compiles to C. I'm mostly fine with C, except I want to make it more readable and with a 'sweeter' syntax. Features I want are: * pythonic indent * no trailing semicolon * vector, hashmap and map containers * immutable strings

> Features I want are: * pythonic indent * no trailing semicolon

I would recommend against making up a completely new syntax, since there would be no support in any existing tools (parsers, linters, IDEs, formatters, ...). Instead, I'd suggest picking a general-purpose syntax with the properties you want (indentation for grouping, newlines for sequencing). One excellent example is called "sweet-expressions", which was originally invented for Scheme but can actually be used to represent any structured data (especially code):

https://srfi.schemers.org/srfi-110

You can use tools like "sweeten" and "unsweeten" from https://sourceforge.net/projects/readable/files to convert between sweet-expressions and s-expressions. S-expressions are just raw syntax trees, so "unsweeten" is basically a ready-made parser which you can pipe into any other program you like, e.g. linters, macro expanders, pretty-printers, etc.

To make your language compile to C, you would need some way to "render" those syntax trees into C code. There are existing tools to do this, for example the C-Mera system can read in C syntax trees and write out C code https://github.com/kiselgra/c-mera

Your preprocessor could then be as simple as `unsweeten | cm c`

To add completely new features like immutable strings you would just need to define some representation for them in the syntax trees (or, if you want to make them the default for "double quotes" you should define an alternative representation for non-immutable strings), then write some find/replace macros to convert occurrences of this representation into an equivalent (but more verbose and elaborate) set of C syntax trees. Stick this find/replace step in between `unsweeten` and `cm` and you've got brand new syntax for very little work.

Re: Zig: programming language designed for robustness optimality and clarity [video]

#54

> So let's talk about memory. I know it's every programmer's least favourite topic I thought that was time zones? ( https://news.ycombinator.com/item?id=17181046 )

What the hell are we going to do when humans are multiplanetary? Keeping computers in track of time is already a mess and we aren't even on mars yet.

I've worked in some dirty gross code, but messing around with time is one of things I can just live without.

Re: Zig: programming language designed for robustness optimality and clarity [video]

#55
post #43

I've been advised to a write a preprocessor so that I can make a language that compiles to C. I'm mostly fine with C, except I want to make it more readable and with a 'sweeter' syntax. Features I want are: * pythonic indent * no trailing semicolon * vector, hashmap and map containers * immutable strings

Have you tried Nim? It has (almost?) everything you're looking for and compiles to C, C++ or JS.

https://nim-lang.org

Re: Zig: programming language designed for robustness optimality and clarity [video]

#56
I experimented with Zig for embedded programming. The language is very promising for this application. It still needs some features and maturity, but it's getting there. The cross-platform support is also has some missing stuff and bugs, but many of those were actually in LLVM in my case.

To me, Zig is the most promising replacement to C right now. It seems to do everything right.

Rust is of course another candidate, with a different philosophy, but I don't consider it a pure C replacement due to core language features requiring an advanced compiler. Zig, for the moment, should be pretty simple to write a compiler for. I consider this a strength of C, that something like TCC can exist.

Re: Zig: programming language designed for robustness optimality and clarity [video]

#57
post #43

I've been advised to a write a preprocessor so that I can make a language that compiles to C. I'm mostly fine with C, except I want to make it more readable and with a 'sweeter' syntax. Features I want are: * pythonic indent * no trailing semicolon * vector, hashmap and map containers * immutable strings

Have you tried Nim? It has (almost?) everything you're looking for and compiles to C, C++ or JS. https://nim-lang.org

> It has (almost?)

No, it has all the features mentioned in GP. It's worth noting, though, that Nim is a higher-level language than Zig appears to be. Nim is garbage-collected, has an object system with multi-methods, has proper lambdas and higher-order functions, uses a kind of uniform access principle (`foo.func()` is the same as `func(foo)`, basically), has destructors and defer blocks, exceptions, iterators, generics, operator overloading, AST based (but procedural) macros and templates, a kind of type-classes (called concepts), built-in concurrency (thread pool) support, and more.

I'm not sure how well it would work on microcontroller, for example, although its garbage-collector is tunable in terms of memory and times constraints. But for anything higher-level than that, Nim is a really nice language, which reads very similar to Python but is natively compiled and much faster (among other features). A quick example to back up the similarity claim:

    proc getMem() : tuple[total: int, free: int] =
      let
        (output, _) = execCmdEx("free")
        fields = output
          .split("\n")[1]
          .split()
          .filterIt(it != "")
      return (fields[1].parseInt(), fields[^1].parseInt())
Really worth taking a look at, if you want conciseness and performance without compromising readability.

Re: Zig: programming language designed for robustness optimality and clarity [video]

#58
His distinction with hidden memory allocations vs perfect software fails even in his C boolean example.

Calling a C function takes up stack space. That's a hidden memory allocation which can exhaust just like heap does. But it's even worse because stack allocation failures don't have a place that return a nice clean NULL like malloc does. It's pretty strong to still call this case "perfect software" under his definitions.

Re: Zig: programming language designed for robustness optimality and clarity [video]

#59

His distinction with hidden memory allocations vs perfect software fails even in his C boolean example. Calling a C function takes up stack space. That's a hidden memory allocation which can exhaust just like heap does. But it's even worse because stack allocation failures don't have a place that return a nice clean NULL like malloc does. It's pretty strong to still call this case "perfect software" under his definit…

Right, he answers a question about this. His plan seems to be to pre-calculate the stack space required at compile time using static callgraph analysis.

This is not yet implemented.

Re: Zig: programming language designed for robustness optimality and clarity [video]

#60

Earlier quoted context omitted.

Have you tried Nim? It has (almost?) everything you're looking for and compiles to C, C++ or JS. https://nim-lang.org

> It has (almost?) No, it has all the features mentioned in GP. It's worth noting, though, that Nim is a higher-level language than Zig appears to be. Nim is garbage-collected, has an object system with multi-methods, has proper lambdas and higher-order functions, uses a kind of uniform access principle (`foo.func()` is the same as `func(foo)`, basically), has destructors and defer blocks, exceptions, iterators, gene…

It works on microcontrollers just fine using the new GC or by disabling it.
Post reply on HN