1 point by yimingchen 3 months | flag | 1 comment
yimingchen 3 months | flag

table Sizes[size] = with 
  [| as size, as Weight |]
  [| "S",     0.5       |]
  [| "M",     0.75      |]
  [| "L",     1         |]
  [| "XL",    0.75      |]
 
table Colors[color] = with 
  [| as color, as Weight |]
  [| "white",  1         |]
  [| "red",    2         |]
  [| "green",  0.5       |]
  [| "blue",   0.75      |]
  [| "black",  1         |]
 
table Catalog = with 
  [| as Size, as Color, as AltColor, as Price |]
  [| "S",     "red",    "blue",      9.99     |]
  [| "M",     "blue",   "white",     10.99    |]
  [| "XL",    "black",  "green",     15.49    |]
 
expect Catalog.size = Catalog.Size
expect Catalog.color = Catalog.Color
 
table Variant = cross(Sizes, Colors)
Variant.W = Sizes.Weight * Colors.Weight 
 
// When both dimensions are provided (by name), behaves as a normal lookup.
WhiteLarge = Variant.Weight[size: "L", color: "white"]
Catalog.Alt = Variant.W[size: Catalog.size, color: Catalog.AltColor]
 
// If a single dimension is provided by name, behavior depends on whether key
// is a scalar or not. 
 
// If scalar, the result is a vector in the other table of the cross-table: 
Colors.Small = Variant.W[size: "S"]
Sizes.Blue = Variant.W[color: "blue"]
 
// If non-scalar, the lookup will automatically look for the other 
// dimension in the key's table. 
Catalog.Alt = Variant.W[color: Catalog.AltColor] // implicit 'size: Catalog.size'
 
// Note that if no label is provided, the rightmost dimension is assumed: 
Catalog.Alt = Variant.W[Catalog.AltColor] // same as the abvoe

Error message:
Error on line 17, column 18: Catalog.Color can only be assigned a value with dimension Color.

See https://try.lokad.com/34mkamdruk1ra