←back to thread

My Foray into Vlang

(kristun.dev)
87 points Bogdanp | 1 comments | | HN request time: 0s | source
Show context
torginus ◴[] No.45082970[source]
I am of two minds on V

On one hand, I think there needs to be an applications programming language that's both fast, statically typed, and minimalistic (like C). C# and Java are unwieldy and carry too much baggage. I hoped Go would be that language. Unfortunately Go's weird choice to use green threads and channels made it very difficult and slow to interop with native code, especially desktop frameworks, which usually rely on a pumped message loop. V was supposed to be that language, that keeps the excellent syntax, but replaces much of the weirdness with much more convenient stuff, while adding a few extra features.

I first learned of V after reading the hit piece someone wrote on it, which has formed the majority of people's opinion's on the language. Back then I though most of the criticisms were unnecessarily harsh and belligerent, most of it boiling down to the compiler/stdlib having bugs, and one asserting that it's 'autofree' implementation leaked memory, based on an incorrect understanding of how valgrind and C memory management works.

I decided to get the truth for myself, and delve into the V language source code (after all, it's up on github). Oh boy.

- The 'compiler' itself doesn't seem to have a a codegen backend, it just produces C code, with every code generation call essentially becoming a stringbuilder concat pushing C code into a buffer. So it's more of a transpiler than a compiler.

- The compiler's code is very worrying - commented out snippets of code, TODOs like 'TODO: this isn't supposed to be null here' over a stray if statement

- The vaunted 'autofree' which (to be fair never claimed to be 100% effective, relying on GC for cases it can't figure out) is just checking objects allocated in the function scope, and frees them at the end of scope. (to be fair, autofree seems to have been deprioritised)

https://github.com/vlang/v/blob/master/vlib/v/gen/c/autofree...

While it works in some cases (including the original critical post where it claimed to fail), and I don't pretend to understand all the nuances of the compiler, I feel like the engineering behind this project is somewhat unsound.

It's super impressive just how much stuff they managed to do over the past few years, and I think a simple and pragmatic desktop language (that produces small binaries, has few dependencies and is easy to write and read) is still needed, I'm kinda reluctant to give V that role until the engineering behind it becomes more solid.

replies(2): >>45083361 #>>45084816 #
creata ◴[] No.45083361[source]
Generating C is fine tbh. Lots of respectable language implementations do that, or did that at some point in their life.

V has plenty of other issues, though.

replies(1): >>45083725 #
1. torginus ◴[] No.45083725[source]
I don't think it's fine the way V does it. What V's compiler is essentially doing is that it parses the V code into an AST, then it walks the AST and tries to generate an equivalent C code on the fly:

https://github.com/vlang/v/blob/master/vlib/v/gen/c/match.v

How a proper compiler should work (and how they actually do from what I've seen), is that they first take the AST, try to remove all fancy syntatic sugar by converting it to simpler constructs, and then they generate an intermediate representation that's either stack, or SSA based. It might skip the lowering of syntatic sugar, and generate IR directly. At some point either on the AST, or the IR, you might want to do control or data flow analysis to support some of your features.

From that point on, the backend generates either LLVM IR, C or might even attempt to generate machine code itself.

The problem with targeting C is that you lose access to stuff not accessible in C, like precise variable tracking on the heap/stack (needed for a good GC), or unwinding support needed for exceptions.