V言語(Vlang)0.4.1がリリースされました。主な変更点は、言語機能、パーサー、標準ライブラリの改善、およびコンパイラー内部に関連する変更に焦点を当てています。
Enum.from_string(name string)
文字列から列挙値への変換を実装する- 初期化されていない関数ポインターの使用を禁止する
- デフォルトの expr を持つ匿名構造体を修正する
- const を列挙値として使用するためのサポート
- 静的関数をメソッド レシーバーとして宣言することを禁止します。
- 修理
for i++; i<10; i++ {
V は Go のシンプルさと Rust の安全機能を組み合わせた静的言語です。著者は、V は Go に非常に似ていると述べました。Go を知っている人は、V の 80% をすでに知っていることになります。Go に基づいた V の改良: https://vlang.io/compare#go。
Vの主な特長
- シンプル (V は 1 時間以内に習得できると著者は主張しています)
- 高速コンパイル (コンパイラーはわずか 400kb、サードパーティへの依存関係はありません)
- 開発の容易さ: V は 1 秒以内にコンパイルされます
- セキュリティ: null なし、グローバル変数なし、未定義値なし、境界検出、不変構造のデフォルトの使用
- C/C++変換のサポート
- 使いやすいクロスコンパイル
- クロスプラットフォームのUIライブラリを提供します
- 内蔵グラフィックライブラリ
- 内蔵ORM
- 組み込みの Web フレームワーク
- ……
サンプルコード
データベースアクセス:
struct User { /* ... */ }
struct Post { /* ... */ }
struct DB { /* ... */ }
struct Repo <T> {
db DB
}
fn new_repo<T>(db DB) Repo {
return Repo<T>{db: db}
}
fn (r Repo) find_by_id(id int) T? { // `?` means the function returns an optional
table_name := T.name // in this example getting the name of the type gives us the table name
return r.db.query_one<T>('select * from $table_name where id = ?', id)
}
fn main() {
db := new_db()
users_repo := new_repo<User>(db)
posts_repo := new_repo<Post>(db)
user := users_repo.find_by_id(1) or {
eprintln('User not found')
return
}
post := posts_repo.find_by_id(1) or {
eprintln('Post not found')
return
}
}
ウェブ開発:
struct Story {
title string
}
// Fetches top HN stories in 8 coroutines
fn main() {
resp := http.get('https://hacker-news.firebaseio.com/v0/topstories.json')?
ids := json.decode([]int, resp.body)?
mut cursor := 0
for _ in 0..8 {
go fn() {
for {
lock { // Without this lock the program will not compile
if cursor >= ids.len {
break
}
id := ids[cursor]
cursor++
}
resp := http.get('https://hacker-news.firebaseio.com/v0/item/$id.json')?
story := json.decode(Story, resp.body)?
println(story.title)
}
}()
}
runtime.wait() // Waits for all coroutines to finish
}