sml

SML { ❄ }

中文 EN

Appendix: Comparison and Investigation

Appendix: Comparison and Investigation

This appendix compares SML with common formats and lists the most common errors and solutions encountered by beginners. Can be used as a reference book to flip through at any time.

A.1 Comparison with JSON/YAML/TOML

AbilitySMLJSONYAMLTOML
Quotation marks optional✅ Naked words are strings❌ Must be✅ (But there are type inference pits)
Comma optional
Block colon can savea { }a: { }
Indent sensitive❌ Relying on {}❌ Relying on {}✅ Easy to make mistakesPartial
Fragment reuse@base/&base❌ (Complex anchor points)
Namespace includeas ns
Contract/Schema✅ Built in
Environment variable injection$env

A.2 Common Error Troubleshooting

PhenomenonReasonSolution
String truncatedValue containing spaces but using bare wordsQuoted "..."
Fragment not unfoldedBlock naked writing &baseWrite as key: &base (value position)
Contract report ‘undeclared field’Default strict modeAdd loose after the contract name, or remove unnecessary fields
port out of range errormin/max constraint effectiveCorrection value, or interval relaxation
$env.X empty stringVariable not set (normal)Confirm that environment variables have been exported during runtime
Include file not foundPath relative to included file directoryCheck relative path; Confirm that the file exists
Loop include errorA contains B, and B contains A in turnBreak loop dependency
@is report contract undefinedContract written after @isMove @contract to before @is
Namespace conflictSame ns duplicate definition @name/@contractRename, or use different as ns

A.3 Grammar Quick Check

键值:      key: value
裸词串:    state: NY
引号串:    name: "John Doe"
整数:      age: 27
浮点:      ratio: 0.75
布尔:      on: true
空值:      x: null
对象块:    a { b: 1 }    ≡   a: { b: 1 }
数组:      list: [ a b c ]
行注释:    #  --  //
块注释:    /* ... */     _* ... *_
片段定义:  @name { ... }
片段引用:  key: &name
契约定义:  @contract Name loose { field: type ... }
契约应用:  @is Name
include:   include "x.sml"        (内联)
            include "ui" as ui     (命名空间)
            include "a", "b" as y  (多目标,需 feature)
环境变量:  secret: $env.API_KEY
转义:      "line1\nline2 \u{2744}"

A.4 version and features

The beginning of the file can declare the version:

@version v1

Complex abilities can be activated as needed:

@feature enable glob regex
include "widgets/*.sml"

By default, include+namespace+implicit-ns (minimalist three piece set) is enabled; multi/glob/regex/ext-rewrite are turned off by default and require explicit opt in.



Feedback and Version

This book is released with swsml 0.4.0. Source code and updates: /book/. Please go to the issue area of SML warehouse for feedback.

The whole book is finished. Return to the homepage of the textbook (/book/).

Hands on practice

After reading this chapter, directly modify SML in the editor below and click “Run” to immediately see the parsing results or validation errors - having output is necessary for efficient learning.

✍ 动手练习 写出一段 SML:name(John)、age(27)、address 块含 city=NY、tags 数组长度 2。这段在任意语言解析结果都应一致。
💡 提示:把这段分别想象成 Rust/C/JS/Lua 解析,字段名与嵌套结构应完全相同。
✍ 自测考题:附录自测 得分 0 / 3
Q1. 与 JSON 相比,SML 的显著区别是?
Q2. 判断:SML 的契约系统(@contract)在 JSON Schema 里没有直接等价物。
Q3. 排查“字段未生效”时,优先检查?