Skip to content

函数 ​

实现状态: func 声明 / 命名参数 / 默认参数 / async / throws / requires Pure / requires IO 全部已实现;@escaping / @autoclosure / 泛型类型擦除反射 未实现

函数是 SYX 程序的基本构件。本章覆盖声明语法、参数、返回、效果注解(requires)、async/await、throws、与跨模块可见性。

定义与调用 ​

swift
func greet(name: String) {
    print("Hello, \(name)")
}

func sumInts(a: Int, b: Int) -> Int {
    return a + b
}

func main() {
    greet(name: "SYX")               // "Hello, SYX"
    print(sumInts(a: 1, b: 2))       // 3
}

命名参数: 调用带标签的参数时 必须 写 label: value。形参可用 _ name 省略外部标签(调用处只能位置传参);可用 label name 指定与内部名不同的外部标签(调用处必须写 label:,不能写内部名)。

swift,errorS042
func greet(name: String) { print("Hello, \(name)") }
func move(to destination: String) { }
func join(_ task: String) -> String { return task }

greet(name: "SYX")           // ✅ 必须带 name:
move(to: "home")             // ✅ 外部标签 to:,内部名 destination
join("worker")               // ✅ _ 省略标签 → 位置传参
join(task: "worker")         // ❌ S042:_ 参数不能带标签
形参写法外部标签策略调用写法
name: Type同内部名 namename: value(必填)
label name: Type外部名 labellabel: value(必填)
_ name: Type省略只能位置传参
元素说明
func关键字
name函数名(lowerCamelCase)
(param: Type, ...)形参列表
-> ReturnType返回类型(缺省 Void)
block函数体

多返回元组 ​

SYX 元组可作为「多返回值」:

swift
func divmod(a: Int, b: Int) -> (Int, Int) {
    return (a / b, a % b)
}

let (q, r) = divmod(a: 17, b: 5)
print("\(q) rem \(r)")           // 3 rem 2

元组解构 (q, r) = … 在 let / var 绑定中合法(MVP GA;fixtures/lang/tuple.sy)。命名元素 (x: 1, y: 2) 与成员 .x / .0 可用;非完整 Swift 元组协议。

默认参数 ​

swift
func greet(name: String, greeting: String = "Hello") {
    print("\(greeting), \(name)")
}

greet(name: "SYX")                       // "Hello, SYX"
greet(name: "SYX", greeting: "Hi")       // "Hi, SYX"

限制: 默认值必须是编译期常量或裸字面量;不能引用运行时变量或调用函数。带默认值的形参可放在任意位置(无 Swift 的「默认参数放末尾」要求)。

访问控制 ​

swift
public func exported() { }               // 跨模块可 import
internal func moduleOnly() { }           // 模块内(默认)
private func hidden() { }                 // 文件 / 作用域内

详见 第 28 章 访问控制。

swift
// 单文件代替跨模块 `import math`(见 Ch28/Ch29)。
public func mathAdd(a: Int, b: Int) -> Int {
    return a + b
}

func main() {
    print(mathAdd(a: 1, b: 2))            // ✅ public
    // math.internal_helper()            // ❌ S005:跨模块访问 internal
}

async 函数 ​

swift
async func httpGet(_ url: String) -> String {
    return "ok"
}

async func fetch() -> String {
    return await httpGet("https://example.com")
}

async 关键字把函数标记为 协程;函数体内可使用 await:

swift
async func fetchA() -> String { return "a" }
async func fetchB() -> String { return "b" }

async func fetchTwo() -> (String, String) {
    let a = await fetchA()
    let b = await fetchB()
    return (a, b)
}
  • await 仅在 async 函数体内合法
  • 非 async 函数使用 await → 编译错误 S020
  • async func main() 是合法的程序入口

实现: 多 await 状态机(已实现,测试 runtime/async_native.rs)。底层通过 generator-based state machine;详见 第 18 章 并发。

async let ​

并行启动多个子任务,等所有完成:

swift
async func fetchA() -> Int { return 1 }
async func fetchB() -> Int { return 2 }

async func parallel() -> Int {
    async let a = fetchA()              // 启动子任务
    async let b = fetchB()              // 启动子任务
    let x = await a                     // 等待 a
    let y = await b                     // 等待 b
    return x + y
}

语义: async let desugar 为 detach + join(SYX 132 行为冻结)。a 是 AsyncValue<T>,await a 阻塞当前任务直到子任务完成。

throws 函数 ​

swift
import System

func loadFile(path: String) throws -> String {
    if (!File.exists(path)) {
        throw "not found: \(path)"
    }
    return File.read(path)
}
子句含义
throws函数可能抛出(不指定异常类型;MVP 是 String 或 Result)
throws T抛 T 类型(typed throws 部分实现)
throw expr在 throws 函数内抛异常
try expr调用 throws 表达式
do { } catch { }块级捕获
try?错误时返回 nil 或传播
try!强制(错误时 panic)

调用:

swift
import System

func loadFile(path: String) throws -> String {
    if (!File.exists(path)) {
        throw "not found: \(path)"
    }
    return File.read(path)
}

func main() {
    let s = try loadFile(path: "/tmp/x")        // 表达式级 try
    let s2 = try? loadFile(path: "/tmp/x")      // String?(nil 表示错误)
    let s3 = try! loadFile(path: "/tmp/x")      // String(错误 panic)

    do {
        let t = loadFile(path: "/tmp/x")
        print(t)
    } catch (err) {
        print("failed: \(err)")
    }
}

详见 第 17 章 错误处理。

效果注解(requires) ​

标记函数「纯」(无副作用)或「IO」:

swift
import System

func pure(x: Int) -> Int requires Pure {
    return x + 1
}

func load() -> String requires IO {
    return File.read("data.txt")
}
注解含义编译期检查
requires Pure无 I/O / 全局状态依赖Phase 50+ 检查调用链(syx-meta)
requires IO允许 I/O标记用
无注解默认(与 IO 等价)无检查

MVP 状态: syx-meta 部分实现(第 32 章 运算符 vs Core Host 提及)。完整效果系统 → 后续 Phase。应用层代码 可不写;服务层 / SDK 可用。

泛型函数 ​

swift
func id<T>(x: T) -> T { return x }
func swap<T>(a: T, b: T) -> (T, T) { return (b, a) }

let n = id(x: 42)            // Int
let s = id(x: "hi")          // String

泛型参数在函数名后;可加 where 约束:

swift
func eq<T>(a: T, b: T) -> Bool where T: Equatable {
    return a == b
}

详见 第 24 章 泛型 与 语言参考:泛型形参和实参。

运算符函数 ​

自定义运算符(GA):

text
infix operator |> { precedence: 50, associativity: left }
func |> &lt;T&gt;(value: T, f: (T) -> T) -> T { return f(value) }
let r = 5 |> { $0 * 2 }      // 10

详见 第 30 章 高级运算符。

函数作为值(一等公民) ​

swift
let f: (Int) -> Int = { x in x * 2 }
let r = f(3)                                  // 6

func apply(g: (Int) -> Int, x: Int) -> Int {
    return g(x)
}
print(apply(g: f, x: 4))                      // 8

函数类型 (P) -> R 是一等公民:可作变量、参数、返回值、Array 元素。

$0 / $1: 闭包简写({ $0 + $1 }),对应首个 / 二个参数;与 Swift $0 一致。部分实现。

嵌套函数 ​

text
func outer() -> Int {
    func inner(x: Int) -> Int { return x * 2 }
    return inner(x: 21)                       // 42
}

内部函数可捕获外层 let/var(详见 第 8 章 闭包)。

递归 ​

swift
func fib(n: Int) -> Int {
    if (n < 2) { return n }
    return fib(n: n - 1) + fib(n: n - 2)
}

SYX 递归使用 VM 调用栈;深递归触发 R014 stack overflow。

诊断 ​

代码触发修复
S001函数名重复改函数名
S004参数/返回类型不匹配修正类型
S005跨模块访问 internal 符号改 public
S042调用参数标签缺失、错误或 _ 参数带标签补正确 label: 或改形参为 _
S020非 async 函数使用 await加 async 或去掉 await
R007! 解包 nil改 ? 或 if let
R014递归过深改迭代或增加栈大小

与 Swift 的差异 ​

SwiftSYX
_ 省略参数标签已实现(_ name);仅当 全部 形参均为 _ 时可全位置调用
强制标签匹配已实现(S042);无「全位置逃逸」
参数默认值可省略尾部SYX 任意位置均可
@escaping未实现
@autoclosure未实现
rethrows未实现
inout未实现;用 &mut T 借用(部分实现)
async throws已实现
async let 语义desugar detach+join(Swift 是结构化并发)
闭包简写 $0/$1部分实现
泛型 where 子句已实现

实现状态 ​

  • 命名参数:已实现
  • 默认参数:已实现
  • async / await / async let:已实现(多 await 状态机 GA)
  • throws / try / try? / try!:已实现
  • requires Pure / requires IO:部分实现(syx-meta MVP)
  • 运算符函数:GA(三后端 parity)
  • 嵌套函数:已实现
  • 闭包简写 $0:部分实现

下一章 → 闭包

参见 → 语言参考:声明 · 第 17 章 错误处理 · 第 18 章 并发 · 第 30 章 高级运算符

SYX · Apache-2.0