Crystal 1.16.0 版本发布
[中文正文内容]
Crystal 1.16.0 is released!
2025年4月9日
Johannes Müller
我们很高兴地宣布 Crystal 1.16.0 版本发布,其中包含多项新特性和 Bug 修复。
预构建包已在 GitHub Releases 和我们的官方分发渠道上提供。请参阅 crystal-lang.org/install 获取安装说明。
统计
此版本包含了自 1.15.1 以来的 162 项变更,由 19 位贡献者完成。感谢所有贡献者为改进该语言所做的努力!❤️
变更
下面我们列出该语言、编译器和标准库中最值得关注的变更。有关更多详细信息,请访问 完整变更日志。
破坏性变更
以下变更会破坏编译器之前的行为,但我们希望它们不会对现有代码造成太大影响。如果并非如此,请在 issue tracker 或 论坛 中告知我们。
修复了 File.match?
的实现
[File.match?
](https://crystal-lang.org/api/1.16.0/File.html#match%3F(pattern%3AString%2Cpath%3APath|String)%3ABool-class-method>) 的实现不够完善,已替换为新的算法。此更改不应影响任何先前按文档记录正常工作的行为。
修正实现中的错误会导致观察到的行为发生以下变化:
- 通配符和 globstar 可以非贪婪地匹配。例如,
**/a
现在匹配a
。 - Globstar 仅匹配完整的路径段,否则它只是两个通配符(相当于一个)。例如,
a**
不再匹配ab/c
(但它匹配ab
)。 - 分支中的子模式根据其各自的语法上下文进行解析。例如,
{[}]}
现在匹配}
。它解析为带有描述字符集}
的子模式的分支。以前,这是一个解析错误。 - 特殊字符的转义符被识别。例如,
\\t
现在匹配\t
,而之前它匹配t
。 - 用于解析字符类的语法更加灵活。例如,
[a-]
现在匹配a
和-
。以前,这是一个模式错误(不完整的字符范围)。
注意:[Dir.glob
](https://crystal-lang.org/api/1.16.0/Dir.html#glob(patterns:Enumerable,match:File::MatchOptions=File::MatchOptions.glob_default,follow_symlinks:Bool=false):Array(String)-class-method>) 的实现方式不同,不受该无效行为的影响。它没有发生任何变化。
参数名称后缀已弃用
用于 defs、macros 和 blocks 的参数名称的后缀 ?
和 !
已弃用。这使它们与其他变量名保持一致。现在它们会产生警告 (#12197)。
感谢,@potomak
Enumerable#sum
和 #product
的隐式返回类型
当元素类型是 union 时,[Enumerable#sum
](https://crystal-lang.org/api/1.16.0/Enumerable.html#sum-instance-method>) 和 [#product
](https://crystal-lang.org/api/1.16.0/Enumerable.html#product-instance-method>) 不再解析隐式返回类型。这无法可靠地工作。相反,您需要通过带有预期返回类型值的 initial
参数显式指定 sum/product 类型 (#15314)。实际上,这会将运行时错误条件转换为编译时错误。
# Crystal 1.16.0
[1, 10000000000_u64].sum # Error: `Enumerable#sum` and `#product` do not support Union types.
# Instead, use `Enumerable#sum(initial)` and `#product(initial)`,
# respectively, with an initial value of the intended type of the call.
# Crystal < 1.16.0
[1, 10000000000_u64].sum # OverflowError: Arithmetic overflow
# Passing an explicit initial value works, before and after:
[1, 10000000000_u64].sum(0_u64) # => 10000000001_u64
感谢,@rvprasad
HTTP::Request
中的资源字符串
修复了 [HTTP::Request
](https://crystal-lang.org/api/1.16.0/HTTP/Request.html#-instance-method>),以正确解析看起来像绝对 URL 的 HTTP 资源字符串。这可能会破坏依赖于先前错误行为的代码 (#15499)。
# Crystal 1.16.0
HTTP::Request.new("GET", "http://example.com/foo").path # => "http://example.com/foo"
# Crystal < 1.16.0
HTTP::Request.new("GET", "http://example.com/foo").path # => "/foo"
子命令的环境变量变更
当为子命令运行进程时,编译器不会设置环境变量 $CRYSTAL
。此变量仅在 1.14.0 中的 #14953 中引入。我们添加了一个更灵活的替代方案 $CRYSTAL_EXEC_PATH
,并且等效于 $CRYSTAL
的现在是 $CRYSTAL_EXEC_PATH/crystal
。编译器还会将其路径添加到 $PATH
(#15186)。
Execution Contexts (执行上下文)
来自 RFC 0002 的 Execution Contexts 作为一个预览功能提供。它已经被证明相当强大,但可能存在一些粗糙的边缘。
您可以使用编译器标志 -Dpreview_mt -Dexecution_context
测试 Execution Contexts。默认上下文是单线程的(与独立的 -Dpreview_mt
不同)。但是您可以根据需要启动其他上下文(例如,[Fiber::ExecutionContext::MultiThreaded
](https://crystal-lang.org/api/master/Fiber/ExecutionContext/MultiThreaded.html>) 或 [Fiber::ExecutionContext::Isolated
](https://crystal-lang.org/api/master/Fiber/ExecutionContext/Isolated.html>))。
mt_context = Fiber::ExecutionContext::MultiThreaded.new("worker-threads", 4)
10.times do
mt_context.spawn do
do_something
end
end
gtk = Fiber::ExecutionContext::Isolated.new("Gtk") do
Gtk.main
end
gtk.wait
Execution Contexts 在大多数目标上都受支持,包括 Linux、macOS、Windows 和不同的 BSD,以及 X86 和 ARM 架构。这一新增功能标志着 持续改进多线程支持的项目 的顶峰,该项目得到了 84codes 的帮助。
语言
[Slice.literal
](https://crystal-lang.org/api/1.16.0/Slice.html#literal(*args)-class-method>) 可以推断元素类型 (#15529) 并在解释器中工作 (#15531)。
感谢,@HertzDevil
- 宏
[sizeof
](https://crystal-lang.org/api/1.16.0/Crystal/Macros.html#sizeof(type):NumberLiteral-instance-method>) 和[alignof
](https://crystal-lang.org/api/1.16.0/Crystal/Macros.html#alignof(type):NumberLiteral-instance-method>) 提供有关 stable types (稳定类型) 的信息 (#15497)。
感谢,@HertzDevil
标准库
- 多个针对
Path
处理的 Bug 修复和性能改进,特别是针对 Windows 路径。
感谢,@HertzDevil
- 新方法
[Indexable#find
](https://crystal-lang.org/api/1.16.0/Indexable.html#find(if_none=nil,offset:Int=0,&:T-%3E)-instance-method>) 和[#find!
](https://crystal-lang.org/api/1.16.0/Indexable.html#find%21%28offset%3AInt%3D0%2C%26%3AT-%3E%29-instance-method>) (#15552)。
感谢,@punteek
- 新方法
[EventLoop#wait_readable
](https://crystal-lang.org/api/1.16.0/Crystal/EventLoop/FileDescriptor.html#wait_readable%28file_descriptor%3ACrystal%3A%3ASystem%3A%3AFileDescriptor%29%3ANil-instance-method>),[#wait_writable
](https://crystal-lang.org/api/1.16.0/Crystal/EventLoop/FileDescriptor.html#wait_writeable%28file_descriptor%3ACrystal%3A%3ASystem%3A%3AFileDescriptor%29%3ANil-instance-method>) (#15376)。
感谢,@ysbaddaden
编译器
感谢,@HertzDevil, @straight-shoota
- 编译器遵循环境变量
$MACOSX_DEPLOYMENT_TARGET
,这消除了目标版本不匹配时烦人的链接器警告 (#15603)
感谢,@HertzDevil
编译器工具
文档生成器可以选择包含 private
和 protected
对象,以及 lib bindings (lib
、fun
、union
、cstruct
、external
和 type
) 中的对象,如 RFC 0011 中所提议的那样。:showdoc:
指令启用该功能。
# :showdoc:
#
# Documentation for LibFoo
lib Foo
# Documentation for function foo
fun foo : Void
end
# :showdoc:
#
# Documentation for method bar
private def bar
end
Thanks@nobodywasishere
依赖更新
- 支持 LLVM 20 (#15412)
感谢,@HertzDevil
弃用
[LLVM::ABI
](https://crystal-lang.org/api/1.16.0/LLVM/ABI.html>) (以及[LLVM::TargetMachine#abi
](https://crystal-lang.org/api/1.16.0/LLVM/TargetMachine.html#abi-instance-method>)) 已被弃用,没有替代品 (#15227)。
感谢
感谢 84codes 和其他所有 赞助商 的持续支持,我们才能够完成所有这些工作。为了保持并提高开发速度,捐款和赞助至关重要。OpenCollective 可用于此目的。
如果您想成为直接赞助商或找到其他支持 Crystal 的方式,请联系 crystal@manas.tech。我们提前感谢您!