Soppo - Go, with the features it's missing

Soppo - Go, with the features it’s missing

Soppo - 补全 Go 语言缺失特性的新语言

Soppo Go, with the features it’s missing. Type safety and ergonomics, with full interop. Take the tour GitHub Playground Docs Soppo 是一款补全了 Go 语言缺失特性的新语言。它在保持完全互操作性的同时,提供了类型安全和更佳的工程体验。欢迎查看:导览 | GitHub | Playground | 文档

Familiar syntax Soppo uses Go syntax wherever possible. If you know Go, you already know most of Soppo. Use any Go library directly, keep your existing tooling, and introduce Soppo gradually into existing projects. 熟悉的语法:Soppo 尽可能沿用了 Go 的语法。如果你熟悉 Go,那么你已经掌握了 Soppo 的大部分内容。你可以直接使用任何 Go 库,保留现有的工具链,并将 Soppo 逐步引入到现有项目中。

package main
import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", handleHome)
    http.HandleFunc("/hello", handleHello)
    fmt.Println("Server starting on :8080")
    http.ListenAndServe(":8080", nil)
}

Enums and pattern matching Tagged unions with struct variants, and pattern matching that ensures you handle every case. The compiler catches missing branches before your code ever runs, so you can refactor with confidence. 枚举与模式匹配:支持带有结构体变体的标签联合(Tagged unions),以及确保你处理每种情况的模式匹配。编译器会在代码运行前捕获缺失的分支,让你能够自信地进行重构。

type Shape enum {
    Circle struct { radius float64 }
    Rectangle struct { width, height float64 }
}

func area(s Shape) float64 {
    match s {
        case Shape.Circle{radius: r}: return 3.14159 * r * r
        case Shape.Rectangle{width: w, height: h}: return w * h
    }
}

Error handling with ? No more repetitive if err != nil blocks. The ? operator propagates errors concisely while keeping error handling explicit and visible. Add custom handling when you need it. 使用 ? 进行错误处理:告别重复的 if err != nil 代码块。? 操作符可以简洁地传播错误,同时保持错误处理的显式与可见性。你可以在需要时添加自定义处理逻辑。

func processFile(path string) error {
    // Propagate error directly
    data := os.ReadFile(path) ?
    
    // Custom handling with named error
    result := parse(data) ? err {
        return fmt.Errorf("parse failed: {err}")
    }
    
    fmt.Println("Processed {len(result)} items")
    return nil
}

Nil safety Nil pointer dereferences are a common source of runtime panics in Go. Soppo tracks nilability at compile time, so you catch these bugs before they reach production. After a nil check, the type narrows automatically. 空值安全(Nil safety):空指针解引用是 Go 语言中运行时恐慌(panic)的常见来源。Soppo 在编译时跟踪空值可能性,因此你可以在 Bug 进入生产环境之前就将其捕获。经过空值检查后,类型会自动收窄。

func findUser(id int) ?*User { // ? marks nilable
    if id == 0 { return nil }
    return &User{name: "Alice"}
}

func greet(id int) {
    user := findUser(id)
    if user != nil {
        // user is *User here, not ?*User
        fmt.Println("Hello {user.name}")
    }
}

Clear error messages When something goes wrong, Soppo tells you exactly what happened and where. Error messages show the relevant code, highlight the problem, and suggest how to fix it. Debugging should be straightforward, not a guessing game. 清晰的错误信息:当出现问题时,Soppo 会准确告诉你发生了什么以及位置。错误信息会展示相关代码、高亮问题所在,并建议修复方法。调试应该是直观的,而不是一场猜谜游戏。

× Non-exhaustive match
╭─[main.sop:11:5]
10 │ var message string
11 │ ╭─▶ match colour {
12 │ │ case Colour.Red:
13 │ │ message = "Stop"
14 │ │ case Colour.Yellow:
15 │ │ message = "Wait"
16 │ ├─▶ }
   · ╰──── missing variants: Green
17 │ }
╰──── help: Ensure all enum variants are handled, or add a `default` case

Get started The recommended way to install Soppo is through SOPMOD, a version manager that handles installing, updating, and switching between versions. It also manages Go, which Soppo uses internally for compilation. 开始使用:推荐通过 SOPMOD 安装 Soppo,这是一个版本管理器,用于处理安装、更新和版本切换。它还负责管理 Soppo 内部编译所需的 Go 环境。

curl -fsSL https://soppolang.dev/install.sh | sh

Then add to your PATH and install: 然后将其添加到 PATH 并安装:

export PATH="$HOME/.sopmod/bin:$PATH"
sopmod install sop latest

Or install just the sop binary using Cargo, without SOPMOD (requires Rust and Go): 或者在不使用 SOPMOD 的情况下,仅通过 Cargo 安装 sop 二进制文件(需要 Rust 和 Go 环境):

cargo install soppo