News for hackers. Stuff that matters.
Re:LLVM 5.0.0 Release by mirekrusin on Thursday September 07, 2017 @04:18PM (#15195323)
This is the first time I can see Zig lang [1]. (Self-proclaimed?) C successor with manual memory management, ironed out edge cases, new take on error handling (that resembles well written error handling in C), generics, compile time reflection/execution (macros?), import .h works directly, exporting to C works directly, nullable types etc... all sound quite interesting actually. Anybody has experience/comments on the Zig lang, please?

[1] http://ziglang.org/

Re:LLVM 5.0.0 Release by tiehuis on Thursday September 07, 2017 @07:17PM (#15196664)
I've been writing a fair bit since a few months ago, and have written a few things for the stdlib. Here are some examples I like about it.

Hassle-free error management

Consider you write a function

  fn div(a: u8, b: u8) -> u8 {
      a / b
  }
If later you find an error case that needs to be handled updating the code usually will not require much extra addition.

  error DivideByZero;
  fn div(a: u8, b: u8) -> %u8 {
      if (b == 0) {
          error.divideByZero
      } else {
          a / b
      }
  }
The caller can choose to ignore errors using `%%div(5, 1)` or can propagate errors to the caller, similar to Rust's `try!`, `?` using `%return div(5, 2)`.

I find this so easy that I'm much more inclined to think about edge cases and handle errors up front. I find when writing Rust the extra setup and management of errors adds a fair bit of tedium (although to be fair, with error-chain and proper setup at the beginning of a project this isn't too bad).

Compile-time programming

Zig has pretty strong compile-time programming support. For example, its printf formatting capability is all written in userland code [1]. It doesn't at this moment support code-generation like D's mixins but I personally have not found this too problematic.

Generic functions can be written in a duck-typing fashion. With compile-time assertions the inputs can be limited to what they need pretty clearly and the errors during usage are pretty self-explanatory.

  error Overflow;
  pub fn absInt(x: var) -> %@typeOf(x) {
      const T = @typeOf(x);
      comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
      comptime assert(T.is_signed); // must pass a signed integer to absInt
      if (x == @minValue(@typeOf(x))) {
          return error.Overflow;
      } else {
          @setDebugSafety(this, false);
          return if (x < 0) -x else x;
      }
  }
Zig doesn't have any form of macros. Everything is done in the language itself.

[1]: http://ziglang.org/documentation/#case-study-printf

Re:LLVM 5.0.0 Release by bluejekyll on Thursday September 07, 2017 @07:29PM (#15196718)
>The caller can choose to ignore errors using `%%div(5, 1)` or can propagate errors to the caller, similar to Rust's `try!`, `?` using `%return div(5, 2)`.

>I find this so easy that I'm much more inclined to think about edge cases and handle errors up front. I find when writing Rust the extra setup and management of errors adds a fair bit of tedium (although to be fair, with error-chain and proper setup at the beginning of a project this isn't too bad).

How is this different than say:

   5_u8.checked_div(1).unwrap()
That unwrap() is performing the same thing as your %% example, if I understand it correctly.

Are the Error types in Zig on the stack or the heap? By default Rust puts everything, including errors, on the stack, which means that the size of the return type always needs to be known. To make this easier you can return Boxed errors:

   fn this_errors() -> Result<u32, Box<Error>> { ... }
And then error types can be very simple. Also, I recommend people getting into Rust really checkout error_chain!, which is a macro that helps in combining all the errors that your library might need to deal with: https://docs.rs/error-chain/0.11.0/error_chain/
Re:LLVM 5.0.0 Release by tiehuis on Thursday September 07, 2017 @07:54PM (#15196852)
Yes you have it right, `unwrap()` is equivalent to `%%`. It will panic if the result is an error.

Error values under the hood are just unsigned integers and are returned on the stack. In fact, the granularity at which allocators are exposed in the stdlib makes any possible dynamic allocation very explicit in the language.

This link [1] provides an overview of errors and some of the surrounding control flow.

[1]: http://ziglang.org/documentation/#errors

Re:LLVM 5.0.0 Release by bluejekyll on Thursday September 07, 2017 @08:05PM (#15196900)
Ah. Thanks for the link. I didn't see anything that section talk about passing values with the Errors, based on your comment, is that even possible?

If you had an error you wished to pass a reason string for, would you need to use a global static or something?

Re:LLVM 5.0.0 Release by tiehuis on Thursday September 07, 2017 @08:17PM (#15196961)
Not with the builtin error type. Its pretty much analogous to a c error code.

If you wanted to send data you would need some other means like you suggest. I'd be interested in finding an ergonomic solution to this but it probably wouldn't be at the language level.

Re:LLVM 5.0.0 Release by blorsh on Friday September 08, 2017 @05:39AM (#15198966)
You say that Zig has no macros, just like FORTRAN, but that isn't a language property at all. Technically, C has no macros, but most people use the C preprocessor. I've seen people preprocess with sed scripts, perl scripts, tr, python scripts, and m4. FORTRAN too was often preprocessed.

So we'll take your Zig and shove it through a preprocessor or several. I've seen code get preprocessed 3 times during builds, but I'm sure that isn't a record.

If a language designer fails to provide a suitable well-matched and effective preprocessor, we'll add something nasty. Oh well. Stuff has to get done.

Re:LLVM 5.0.0 Release by gpderetta on Friday September 08, 2017 @06:57AM (#15199299)
> Technically, C has no macros, but most people use the C preprocessor

technically the C preprocessor is a non-optional part of the C language and specification (and in fact embedded in the actual C compiler binary in many implementations). So both in theory and in practice C very much has a macros.

/pedantic

Re:LLVM 5.0.0 Release by chubot on Thursday September 07, 2017 @04:40PM (#15195504)
Previous threads:

https://news.ycombinator.com/item?id=12378922

https://news.ycombinator.com/item?id=11060282

Actually I'm surprised that Zig didn't come up on the "Some Were Meant for C" thread, because this strategy is exactly what I wished something like Rust had support for (importing header files directly, more direct support for unit-by-unit translation, etc.):

https://news.ycombinator.com/item?id=15179941

Re:LLVM 5.0.0 Release by bluejekyll on Thursday September 07, 2017 @05:18PM (#15195824)
Are you familiar with this?

https://github.com/rust-lang-nursery/rust-bindgen

It will translate a C header into Rust for all your bindings. Something could probably be built to do this more on the fly. If it doesn't already exist.

edit: docs link https://docs.rs/bindgen/0.30.0/bindgen/

edit2: and I should mention the inverse, Rust to C bindings,

https://github.com/eqrion/cbindgen

https://docs.rs/cbindgen/0.1.23/cbindgen/

Re:LLVM 5.0.0 Release by chubot on Thursday September 07, 2017 @10:55PM (#15197586)
Yeah that is part of it, but I also like the lighter design philosophy I see Zig. It's the difference between thinking about compatibility up front vs. clean-slate design + compatibility.

If you look at the Python vs. Lua C API you can see that difference. Lua was designed to interoperate with C (albeit after breaking the language 4+ times); Python just took whatever their implementation happened to be, and then exposed that API to users. With thousands of functions and macros and GIL and whatnot.

Then a bunch of people came along later and wrote 5 different binding generators, and all of them solved some the problem, but not all of the problem. And some of them created new problems (SWIG).

Also (to child comment), is it really possible to do bindgen as a macro rather than as a separate code generating tool? Because I assume bindgen has to invoke a C++ parser like Clang.

I looked at Zig and it indeed uses libclang to parse C. I don't know about Rust's macro system but it would be somewhat surprising if it can invoke external tools or run C++ code at compile time.

Re:LLVM 5.0.0 Release by bluejekyll on Thursday September 07, 2017 @11:54PM (#15197759)
Procedural macros aren't the macro_rules and custom derives that are currently in stable.

They allow you to do much more. You can see this put to use in the Rocket project quite heavily. I believe it should be possible to link against bindgen as described and generate the bindings inline.

The caveat here is that it would be fairly opaque to the user as you wouldn't see the generated bindings in code. For situations like that I rely on 'cargo doc' to generate the documentation such that I can see the actual interface generated.

Rocket codegen for example: https://github.com/SergioBenitez/Rocket/tree/master/codegen

Re:LLVM 5.0.0 Release by simias on Friday September 08, 2017 @03:50AM (#15198574)
You also have to take into account that Rust has a borrow checker that makes opaque C calls 2nd class citizens anyway. Zig doesn't have that as far as I can tell.

So it means that even if Rust had a standard C header include directive it still wouldn't be completely transparent because most of the time you want to wrap around C APIs in Rust to provide safe, borrow checked and pointer-free alternatives. There's a different philosophy I think.

Re:LLVM 5.0.0 Release by steveklabnik on Friday September 08, 2017 @11:08AM (#15200760)
> I looked at Zig and it indeed uses libclang to parse C. I don't know about Rust's macro system but it would be somewhat surprising if it can invoke external tools or run C++ code at compile time.

Bindgen is often invoked in a "build script", which Cargo runs before building your project. So not exactly, but basically.

Re:LLVM 5.0.0 Release by johncolanduoni on Thursday September 07, 2017 @06:11PM (#15196243)
Bindgen is excellent with C APIs but what I've found really impressive is that it can handle C++ pretty well too. Most of my issues with using C++ code via bindgen are due to deviations in calling conventions that are hard to solve at that level.
Re:LLVM 5.0.0 Release by JoshTriplett on Thursday September 07, 2017 @08:31PM (#15197038)
And as soon as procedural macros are stabilized, I'm sure that someone will provide a bindgen macro that would let you do something like bindgen!("somelib.h").
Re:LLVM 5.0.0 Release by kbenson on Thursday September 07, 2017 @04:41PM (#15195509)
Zig competes with C instead of depending on it. The Zig Standard Library does not depend on libc.

That's an interesting choice. I'm sure there are other semi-recent languages that have made the same choice, it would be interesting to hear the benefits and problems of that approach.

I say semi-recent, because there are of course many from before C or that competed with C and Unix initially, but unless they are still used much (Lisp?) it's not necessarily the best for a comparison of modern issues.

Re:LLVM 5.0.0 Release by tiehuis on Thursday September 07, 2017 @07:35PM (#15196752)
A useful thing of this is also the portability story. Go for example is pretty good as far as I'm aware of cross-compiling to a different target. The main problem would probably be the extra implementation needed, instead of just writing a binding to libc. It also is more popular lately to prefer static linking vs. dynamic for deployment ease too, which not requiring linking to libc may help.

For zig it is not all or nothing of course. It is pretty easy to link and use c if wanted. The following for example asks the compiler to link against libc.

  zig build_exe main.zig --library c
One other benefit that zig gets is more compile-time execution opportunity. The math library for example being written in zig means that we can determine `math.log(7)` and use it for compile calculations when using libm we couldn't.
Re:LLVM 5.0.0 Release by badsectoracula on Thursday September 07, 2017 @04:45PM (#15195547)
Free Pascal has its own standard library that talks directly with the underlying OS too.
Re:LLVM 5.0.0 Release by wahern on Thursday September 07, 2017 @08:45PM (#15197084)

  it would be interesting to hear the benefits and problems of
  that approach.
A big problem is that libc interfaces are more stable, which means with libc you don't need to recompile as much when the system is upgraded, if ever.

Solaris and Linux work hard to keep their kernel interfaces stable, so it's less of an issue there. But Apple, for example, very explicitly says that kernel interfaces (Unix syscalls and Mach ports) are unstable and unsupported, generally speaking. Consumable interfaces are provided via the userland runtime (e.g. libc). Notably, Apple's toolchains don't even support static compilation.

A system like OpenBSD isn't so gratuitous as Apple in terms of breaking compatibility, but because they're focused on improving and simplifying a cohesive system, they don't flinch about breaking compatibility when needed. For various reasons that happens more often with kernel interfaces than with libc interfaces.

Re:LLVM 5.0.0 Release by anonymousDan on Thursday September 07, 2017 @05:04PM (#15195697)
Go
Re:LLVM 5.0.0 Release by kbenson on Thursday September 07, 2017 @05:18PM (#15195820)
Are you sure? I'm seeing reports that go sometimes wouldn't run with musl instead of GNU libc, with a runtime segfault[1]. That seems to depend on libc. Are you saying that Go's standard library primitives are not shims (or more complex structures) built on top of libc?

1: https://github.com/golang/go/issues/14476

Re:LLVM 5.0.0 Release by Matthias247 on Thursday September 07, 2017 @05:26PM (#15195893)
Go uses libc optionally (for some things like DNS resolving). You can build Go programs which do not utilize libc by disabling cgo and building static binaries. By default the biggest part of Gos stdlib isn't based on libc, but some small parts are.
Re:LLVM 5.0.0 Release by minxomat on Thursday September 07, 2017 @04:50PM (#15195590)
Wow, that looks awesome. The syntax is pretty much what I look for in a language, much more approachable to me than Rust. Will tinker with it.
Re:LLVM 5.0.0 Release by Rusky on Thursday September 07, 2017 @04:56PM (#15195634)
The syntax actually looks fairly similar to Rust, just with generics replaced with `comptime`. What do you find more approachable about it?
Re:LLVM 5.0.0 Release by jandrese on Thursday September 07, 2017 @04:21PM (#15195351)
Interesting. I've been somewhat disappointed with Rust thus far and this looks a lot more like what I was hoping for.
Re:LLVM 5.0.0 Release by kbenson on Thursday September 07, 2017 @04:35PM (#15195465)
What did you find disappointing? What were you hoping for (so I can compare it to my own hopes)?

Rust is the one (out of Rust, Nim and D) that looks most promising to me from the outside for my goals[1], but I haven't really settled on one to devote my very limited free time to.

1: If I'm going to drop down from a high level dynamic interpreted language to a low level strongly typed compiled one, I might as well got the extra distance Rust is asking for the gains it promises.

Re:LLVM 5.0.0 Release by jandrese on Thursday September 07, 2017 @06:27PM (#15196363)
Mostly I ran into a lot of friction with shared data structures that are being accessed by multiple threads. Stuff like ring buffers, flow state databases, and similar systems.

There's probably Rust ways to do that stuff, but it was not obvious to me.

Re:LLVM 5.0.0 Release by littlestymaar on Thursday September 07, 2017 @06:54PM (#15196533)
If you're able to write a robust shared ring buffer in C, you should be able to do the exact same implementation in unsafe Rust using raw pointers, declare it as Sync and use it from safe Rust with no issue. Or am I missing something ?

Or, if you're like me and not confident enough to do this, you could check on crates.io[1] to see if no available lib already does what you need. In which case you basically have it for free.

[1] https://crates.io/keywords/ring-buffer

Re:LLVM 5.0.0 Release by gcp on Friday September 08, 2017 @03:48AM (#15198563)
I'm pretty sure that's intentional friction (having to prove what you're doing is safe).
Re:LLVM 5.0.0 Release by jandrese on Friday September 08, 2017 @11:37AM (#15201026)
Figuring out how to prove it is safe to the compiler is definitely a challenge.
Re:LLVM 5.0.0 Release by hasenj on Thursday September 07, 2017 @09:09PM (#15197193)
Wow, nice find! I've been looking recently for a language like that. I like Swift but I dislike the heavy coupling with macOS/iOS and ObjC. I like D but dislike its somewhat arcane syntax and bloat, plus it has a GC so it's not the same category. I like Rust but the borrow checker is brutal.

I will give ziglang a try!

Re:LLVM 5.0.0 Release by Mindless2112 on Thursday September 07, 2017 @09:21PM (#15197233)
D now has "Better C" mode, which might interest you.

https://dlang.org/blog/2017/08/23/d-as-a-better-c/