Add required array element constraints

This commit is contained in:
2026-08-11 17:24:04 +09:00
parent 5a05da6fe4
commit 46b2b3b1b6
35 changed files with 6328 additions and 5042 deletions
@@ -124,6 +124,19 @@ String & /^a$/ & /^b$/
Host = IPv4Address;
```
## 配列制約
配列制約には要素制約が必須であり、`[...T]` と書く。
```dcdl
Names = [...String];
PositiveInts = [...(Int & > 0)];
```
複数の配列制約を `&` で合成した場合、各 concrete 要素へすべての要素制約を合成する。
要素が object schema の場合は、その schema に含まれる default も各要素へ適用される。
要素制約のない `Array` primitive type は存在しない。
## default
`default` は制約ではない。
+17
View File
@@ -31,6 +31,23 @@ disabled_config = NewConfig & {
enabled_config = NewConfig;
```
## 配列スキーマ
```dcdl
Services = [...{
name = String;
port = Int default 8080;
}];
Services & [
{ name = "api"; },
{ name = "worker"; port = 9000; },
]
```
抽象配列には要素制約が必須である。
この例では、1 番目の要素の `port``8080` に materialize される。
## 関数と制約
```dcdl
@@ -7,6 +7,46 @@ array expression は、順序付きの値の列を表す。
["a", "b", "c"]
```
配列リテラルは concrete value であり、要素型を揃える必要はない。
```dcdl
["api", 8080, true]
```
## Array constraint
配列制約は `[` の直後に ellipsis を置き、その後へ要素制約を記述する。
```dcdl
Names = [...String];
Ports = [...(Int & >= 1 & <= 65535)];
```
`[...T]` は、すべての要素が `T` を満たす長さ 0 以上の配列を表す。
空配列は任意の配列制約を満たす。
```dcdl
[...String] & []
[...String] & ["api", "worker"]
```
要素制約は object schema にもできる。
要素 schema の default は、配列へ制約を合成するときに各要素へ適用される。
```dcdl
Services = [...{
name = String;
enabled = Bool default true;
}];
Services & [{ name = "api"; }]
```
要素制約のない抽象配列型は提供しない。
旧来の `Array` primitive type は使用できず、`[...T]``T` は必須である。
`[String]` は配列制約ではなく、未解決の `String` 制約を 1 要素に持つ concrete array になる。
長さ制約、位置別の tuple 制約、unique 制約は現在サポートしない。
## Array concat
`++` は concrete array 同士を連結する。
@@ -11,6 +11,7 @@ Expr
├─ path reference
├─ object
├─ array
├─ array constraint
├─ function
├─ function call
├─ let
+2
View File
@@ -57,6 +57,7 @@ primary_expression = literal
| identifier
| comparison_constraint
| object
| array_constraint
| array
| let_expression
| function_expression
@@ -73,6 +74,7 @@ field_definition = field_path , "=" , expression ;
field_path = identifier , { "." , identifier } ;
array = "[" , [ expression , { "," , expression } , [ "," ] ] , "]" ;
array_constraint = "[" , "..." , expression , [ "," ] , "]" ;
let_expression = "let" , { field_definition , ";" } , "in" , expression ;
function_expression = "(" , [ parameter_list ] , ")" , "=>" , expression ;
+1
View File
@@ -121,6 +121,7 @@ false
default fallback 指定
=> 関数
. フィールド参照 / ドットパス定義
... 配列の要素制約
```
演算子の優先順位は [合成演算子](./operators.md) で定義する。
+3 -2
View File
@@ -9,10 +9,11 @@ name = String;
retry = Int default 3;
ratio = Float;
enable = Bool default true;
tags = Array;
tags = [...String];
```
現在の primitive type は `String``Int``Float``Bool``Array` である。
現在の primitive type は `String``Int``Float``Bool` である。
配列は primitive type ではなく、必須の要素制約を持つ `[...T]` で表現する。
各型の個別仕様へのリンクは [Manual Index](../../index.md) に集約する。
primitive type と制約合成の詳細は [制約と default](../constraints-and-defaults.md) も参照する。