Follow

I'm falling (more deeply) in love with macros.

I just reduced the number of lines in a small file by 35% by adding a single macro.

And the file is, if anything, *more* readable than before: with the macro handling the repetitive parts, it's more clear where the differences are.

So, I guess all I can say is, `macro_rules!`

@codesections Macros are nice, as long as you don't go and `define begin {` or `define ONE 1`.

(also languages with actual macro support help)

@Sturmflut

> What does the macro do?

It declares a `struct` with a given name, field and data type, and then implements a method on the stuct that converts it into a particular data type to work with an external dependency.

Pretty simple stuff, but an area where getting good type-checked guarantees would require a decent bit of boilerplate without a good macro system.

@codesections is a pretty neat language with its metaprogramming too. By using only the stdlib I'm able to get the JSON API from a site, cache it, and be ready to parse it into native Nim types using only 40 or so lines of code.

By simply defining Nim types that match the JSON data, it's literally one line to parse it all into native types using the "to" macro:

var native = to(json_data, NativeType)

Pretty awesome stuff!

@kungtotte

> By using only ['s] stdlib I'm able to get the JSON API from a site, cache it, and be ready to parse it into native Nim types using only 40 or so lines of code.

Hmm, now I'm wondering how many lines of code that would take. I think it would be pretty similar.

Of course, in Rust you wouldn't be able to do it using just the standard library, but that's a philosophical decision to have a small core.

And is so well established that it's practically in std

@codesections has moved a ton of things out of the stdlib as well in preparation for v1.0, but it has a httpclient and json library still in there. As far as that stuff goes I'd say the philosophies are pretty similar, it's just that is further ahead.

@bugaevc @kungtotte

> is a pretty neat language with its metaprogramming too. By using only the stdlib I'm able to get the JSON API from a site, cache it, and be ready to parse it into native Nim types using only 40 or so lines of code.

> Hmm, now I'm wondering how many lines of code that would take. I think it would be pretty similar.

> Challenge accepted

17 lines of code. Nicely done, @bugaevc!

@bugaevc @codesections @kungtotte If you install cargo-edit, you can type `cargo add reqwest serde` to add the latest version of reqwest and serde to your Cargo.toml file.

@bugaevc @kungtotte

Also, looking at ^^^ again, I like how it's fair to describe it as "basically just the standard library" or as "using 185 packages"

@codesections @bugaevc those compile times tho...

Weren't they working to improve those recently? I seem to recall something about that in one of the recent release threads on HN. Or is that an on-going thing that's not finished yet.

@kungtotte @bugaevc

> Weren't they working to improve [ compile times[ recently?

Yeah, it's an ongoing process. They have gotten a lot better (not that you believe it from that video…)

One big improvement a while back was enabling incremental compilation. Now that we have that, *most* builds are pretty snappy, but starting from scratch like in that video is pretty brutal.

But Rust is never going to play in, e.g., 's league for fast compile times on massive projects.

@codesections @kungtotte @bugaevc but that's only because golang is a simple language without safety guarantees... the #rust compiler does a _lot_ more work during compilation!

@musicmatze @codesections @bugaevc has fast builds even though it does safety checks. In debug builds it has safety checks and in release builds it removes those but has optimization instead.

It's not a simple language like is either; it has a lot of features and still manages fast builds.

@kungtotte @codesections @bugaevc Sure that this is comparable? If #Nim can remove safety checks, I would assume these are runtime checks, are they?

In #rust we have compiletime checks and these are of course also enabled in release mode (because why not)! The difference between debug and release mode in rust is debug symbols and optimization, but not safety guarantees!

@musicmatze @codesections @bugaevc yes, they're runtime checks. You can turn them on for release builds if you want (granularly, by check type and function by function) but you'll incur a slight runtime performance penalty obviously.

@kungtotte @musicmatze @bugaevc

> but [ compiles quickly] only because golang is a simple language without safety guarantees

Not really. Go compiles fas because it was built to, from the ground up. Fixing slow build times on massive programs was one (probably *the*) major reasons Google even built Go.

> the compiler does a _lot_ more work during compilation!

Meh. `cargo check` is fast—that extra work isn't what slows us down.

@kungtotte @musicmatze @bugaevc

Also, the main reason has slower compile times is that we send a *ton* of code to LLVM (largely, though not entirely, due to monomorphisization).

Most of the compile time actually comes at the LLVM stage, which is *after* we've performed all our safety checks

Sign in to participate in the conversation
Fosstodon

Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.