Summary#
An array literal [a, b, c] is a value expression — a list you can return, assign to a local, pass as an
argument, or hand to a UI control as a prop. The element type is inferred from the items (a homogeneous list is the
common case). Because object literals (new T { … }) are plain data, a list of them is a first-class value too — so
a UI prop like columns: [ new GridColumn<User> { Name = "Email", Value = u => u.Email } ] is expressed directly,
no workaround.
Signature#
[1, 2, 3] // int[]
["Email", "DisplayName"] // string[]
[ new GridColumn<User> { Name = "Email", Value = u => u.Email } ] // GridColumn<User>[]
[] // an empty listDescription#
- A value, anywhere a value fits — return it, assign it (
var xs = [1, 2, 3];), pass it as an argument, or use it as a render/state value in a component. - Element type is inferred from the items; a list of
new GridColumn<User> { … }is aGridColumn<User>[], matching a prop declaredGridColumn<T>[]. - Object literals inside are pure data. In a render position a
new T { … }builds a plain data object (it is not a stored row — that is what anew T { … }inside an action body does). This is why a control'scolumnsprop takes[ new GridColumn<T> { … } ]cleanly. - Distinct from set-membership. Testing membership is written
[a, b].Contains(x)(orx in [a, b]), which the compiler lowers to a set test — separate from using[a, b]as a value.
Examples#
int[] Small() { return [1, 2, 3]; }
string[] Names() { var xs = ["Ada", "Grace"]; return xs; }Supplying a control's typed columns from an array of object literals (a UI prop):
entity User {
[Required, MaxLength(200)] string Email;
[Required, MaxLength(120)] string DisplayName;
security { allow read when IsAuthenticated; }
}
[Page("/users")] [Render(CSR)]
component UsersPage() {
var users = User.ToList();
render {
DataGrid(
label: "Users",
rows: users,
columns: [
new GridColumn<User> { Name = "Email", Label = "Email", Value = u => u.Email },
new GridColumn<User> { Name = "DisplayName", Label = "Name", Value = u => u.DisplayName }
]
);
}
}See also#
- List indexer — reading an element by index (
xs[0]) - List OrderBy (in-memory) — sorting a collection
- control — foreign UI controls (charts, grids, maps) — foreign controls, whose typed props often take an array literal