5 ms·
Want to comment as someone who has written small amounts of code in V and followed it since near it's inception: V is essentially a much cleaner version of Go,
by gavinray 6y ago
Want to comment as someone who has written small amounts of code in V and followed it since near it's inception:
V is essentially a much cleaner version of Go, syntactically. Though it adds many features that turn it into one of the nicest (IMO) languages to write.
It adds:
- Hot reloading, on a compiled language (WOW)
- Pattern matching/match statements
- Option/Sum types
- A really clean approach to handling Maybe results that I haven't seen in other languages before -- EDIT: I take it back, apparently my own ignorance here
- Generics
- C interop and compile/build integration that's incredibly easy
Some of my favorite bits:
// Error-catch + propagation on Maybe's with "?"
fn f(url string) ?string {
resp := http.get(url) ?
return resp.text
}
// Identical to this -- Option types must be handled with "or" if not "?"
resp := http.get(url) or {
return error(err)
}
return resp.text
number := 2
s := match number {
1 { 'one' }
2 { 'two' }
else { 'many'}
}
Additionally, the devs/community on Discord are really friendly, and they spent an hour helping me compile Facebook's reference C++ GraphQL parser library, and interop with the C API from V. I'd never touched low-level stuff before and the experience was a blast.
This was all it took (minus figuring out the C++ build process):
// app.v
#flag -I libgraphqlparser_compiled
#flag -L /home/user/Projects/vlang-graphql/libgraphqlparser_compiled
#flag -l graphqlparser
#include <GraphQLParser.h>
#include <GraphQLAstToJSON.h>
struct C.GraphQLAstNode{}
fn C.graphql_parse_string(text charptr, error &charptr) &C.GraphQLAstNode
fn C.graphql_ast_to_json(node &C.GraphQLAstNode) byteptr
node := C.graphql_parse_string("query MyQuery { id email }", "Failed to decode")
json_str := cstring_to_vstring(C.graphql_ast_to_json(node))
println(json_str)
And because the primary compilation target atm is C, performance is generally screaming fast.
---
Edit: Want to clarify it's still experimental. As a concept/language, I found it fun.
See Christine Dodrill's negative experience and valid criticisms below for the other side of this coin.
- david2ndaccount 6y agoHot reloading in a compiled language is not that difficult - you just dlclose/dlopen/dlsym the shared library when it changes.
- lightgreen 6y agoBut making sure program won't crash after reloading (e. g. because of memory layout or invariants change) is much harder, and I doubt V prevents these failures.
- delian66 6y agoIt does not. The reloaded program can crash. It is very useful for tweaking code, but if you make more substantial changes to your state, currently hot code reload will not help you, and you may be better of just restarting your app.
- tlb 6y agoHah! You have to deal somehow with objects created by the old code, and pointers to static data or functions that move. Various systems solve that in various ways, but it's definitely complex and difficult.
- gavinray 6y agoI guess I'm just easy to impress then, because lower-level code might as well be magic to me haha.
- lightgreen 6y ago> A really clean approach to handling Maybe results that Rust has exactly the same question mark syntax, and obviously V copied it from Rust. Copying good ideas from other languages is right. > I haven't seen in other languages before But you did not know that the syntax was derived from Rust. And the value of a review of some programming language from a person without basic knowledge of the space is not very helpful. It's likely that you missed a lot of other important details. Like this one: > V is essentially a much cleaner version of Go, syntactically Syntactically only, yes. But it is very different from Go, because it does not do garbage collection. Sorry.
- gavinray 6y agoAh, I meant the "or" operator, in Rust you have to match on the option result right? match maybe_error { Ok/Some(val) => None/Err => }
- hardwaregeek 6y agoYou can do result.or_else(|err| ... ) Or result.map(|value| ... )
- gavinray 6y agoAh welp, fair enough that's on me for being clueless. Edited my answer.
- edflsafoiewq 6y agoYou can do result or { break }. You can't do that from a closure.
- edflsafoiewq 6y agoV's Maybes are different than Rust's, only the ? syntax is similar 1. Option/Result are unified into a single type, ?T. 2. It is not parameterized over the error type. 3. The Ok() case does not need to be marked (ex. you write 1, not Ok(1)). 4. In addition to the x? sugar, there is also x or { ... } sugar, which is like Rust's x.unwrap_or(|err| ...) except without the closure so you can eg. break or return from the block. There is also if y := x { .. } else { ... }, which is like Rust's match x { Ok(y) => ..., Err(err) => ... }. These are all reasons you could think V has cleaner Maybe handling than other languages.
- rowanG077 6y agoThe handling of the maybe result is similar to the do notation in Haskell for the Maybe monad.
- hardwaregeek 6y agoWhat's this clean way of handling Maybe? The question mark operator or something else?