I cannot find some sort of user defined structured data type like Record or Type in other languages. I always use nested list or associations instead of that, which is not really comfortabel. Is it true that I cannot define my data types at will?
As an example in, say, VBA I would write something like:
' --- User-defined Types
Type T_POINT
x As Double
y As Double
End Type
Type T_STATE
p As T_POINT
s As Double
End Type
' --- User-defined data
Dim myState As T_STATE
' --- Use such data types
Sub CB_init_Click()
init myState
With myState
Range("s_px") = .p.x
Range("s_py") = .p.y
Range("s_s") = .s
End With
End Sub
Sub init(state As T_STATE)
With state
.p.x = 1
.p.y = 2
.s = Rnd()
End With
End Sub
In WL I would have to use lists, something like:
p = {1, 2};
state = {p, RandomReal[]};
Print["state: \t", state];
Print["state.p: \t", state[[1]], "\tstate.p.y: ", state[[1, 2]]];
Print["state.s: \t", state[[2]]];
state: {{1,2},0.851364}
state.p: {1,2} state.p.y: 2
state.s: 0.851364
Or associations, something like:
p = <|"x" -> 1, "y" -> 2|>;
state = <|"p" -> p, "s" -> RandomReal[]|>;
Print["state: \t", state];
Print["state.p: \t", state["p"], "\tstate.p.y: ", state["p"]["y"]];
Print["state.s: \t", state["s"]];
state: <|p-><|x->1,y->2|>,s->0.198831|>
state.p: <|x->1,y->2|> state.p.y: 2
state.s: 0.198831
Both are certainly fast, but not very readable (in particular the list-form).
And I cannot really force a variable assignment to a certain data type, i.e. a pattern. For example in the association-case I could define named patterns like:
Pattern[TPOINT, <|"x" -> _Number, "y" -> _Number|>];
Pattern[TSTATE, <|"p" -> _TPOINT, "s" -> _Number|>];
But how can I then force the assignments above to p and state to obey those patterns? Favourably at compiletime, not runtime?