pattern-matching - 字符串选项 ref 上的 OCaml 匹配
问题描述
如何对具有string option ref
OCaml 类型的变量进行模式匹配。我需要提取这个变量的字符串部分,但我无法让它工作。
解决方案
Aref
只是一个带有可变字段的记录类型,称为contents
:
type 'a ref = { mutable contents: 'a }
因此,您可以像任何其他记录一样对其进行模式匹配:
match foo with
| { contents = Some str } -> str
| { contents = None } -> ...
虽然我更喜欢打开第ref
一个而不是匹配它:
match !foo with
| Some str -> str
| None -> ...
推荐阅读
- graphicsmagick - Graphicsmagick 转换:在所有 4 个边上添加 -border 作为高度的百分比
- python - 使用 Python 从 html 网站获取数据
- react-native - 在 react native 中使用 props in pressable 进行导航
- java - 如何在java中将unicode字符串转换为ascii字符串?
- cakephp - Cakephp 在 postgres 中获取关联总和组
- linux - Asp.Net Core 无法从 apsettings.json 解析依赖值
- python - 在包含数组元素的数据帧上应用 numpy 操作和矢量索引的自然方式
- python - 当我尝试将变量分配给列表索引时,它会自发增长
- angularjs - FirebaseError:仅在页面重新加载时缺少权限或权限不足
- mongodb - 如何在 mongodb compass 上插入具有当前日期和类型日期的文档