mirror of
https://github.com/polserver/polserver
synced 2026-08-13 08:23:08 -04:00
66 lines
2 KiB
Text
66 lines
2 KiB
Text
// A two-index subscript is a slice: start position and length. Reading one is supported on
|
|
// arrays and strings; assigning to one is supported on strings only, and every other type
|
|
// hands back an error object rather than raising one.
|
|
|
|
// Reading a slice of an array.
|
|
var a := array{ 1, 2, 3, 4 };
|
|
print( a[1, 2] );
|
|
print( a[2, 3] );
|
|
print( a[3, 99] );
|
|
print( a[0, 2] );
|
|
print( a[9, 1] );
|
|
|
|
// A slice of a nested array keeps the elements themselves.
|
|
var nested := array{ array{ 1, 2 }, array{ 3, 4 } };
|
|
print( nested[1, 2] );
|
|
|
|
// Assigning to a slice of an array is not supported. The array is left alone, and the value
|
|
// of the assignment is the error.
|
|
var flat := array{ 1, 2, 3 };
|
|
var res := ( flat[1, 2] := 99 );
|
|
print( res );
|
|
print( flat );
|
|
|
|
// The same when the element the slice starts at is itself an array.
|
|
res := ( nested[1, 2] := 99 );
|
|
print( res );
|
|
print( nested );
|
|
|
|
// The same for a dictionary, whose value is an array.
|
|
var d := dictionary{ "k" -> array{ 7, 8 } };
|
|
res := ( d["k", 1] := 55 );
|
|
print( res );
|
|
print( d );
|
|
|
|
// The same for a struct.
|
|
var st := struct{ x := array{ 1, 2 } };
|
|
res := ( st["x", 1] := 5 );
|
|
print( res );
|
|
print( st );
|
|
|
|
// Three indexes are refused the same way.
|
|
res := ( flat[1, 2, 3] := 1 );
|
|
print( res );
|
|
print( flat );
|
|
|
|
// A string is the one type that does assign through a slice.
|
|
var s := "abcdef";
|
|
s[2, 3] := "zyx";
|
|
print( s );
|
|
|
|
// A single subscript assignment is unaffected.
|
|
flat[2] := 42;
|
|
print( flat );
|
|
|
|
// On a dictionary or a struct several indexes chain instead of slicing, to any depth.
|
|
var deep := dictionary{ "a" -> dictionary{ "b" -> dictionary{ "c" -> 3 } } };
|
|
print( deep["a", "b"] );
|
|
print( deep["a", "b", "c"] );
|
|
print( struct{ x := struct{ y := 7 } }["x", "y"] );
|
|
|
|
// The last step of such a chain lands on an array or a string with one index left over, and
|
|
// takes the element or character at that position rather than a slice.
|
|
print( dictionary{ "k" -> array{ 7, 8 } }["k", 1] );
|
|
print( struct{ x := array{ 7, 8 } }["x", 1] );
|
|
print( dictionary{ "k" -> "abcdef" }["k", 2] );
|
|
print( dictionary{ "k" -> 1 }["k", 1] );
|