ketos
ketos copied to clipboard
Specify struct type in struct fields
I tried to specify a struct name as a field type:
ketos=> (struct foo ((a string)))
foo
ketos=> (struct bar ((b foo)))
bar
ketos=> (new bar :b (new foo :a "fails"))
Traceback:
In main, lambda
In system function new
execution error: type error for field `b` of struct `bar`: expected foo; found struct: foo { a: "fails" }
ketos=> (struct baz ((b struct)))
baz
ketos=> (new baz :b (new foo :a "works"))
baz { b: foo { a: "works" } }
The error confused me for a while until I realized the type for all struct values is "struct". Could it allow you to specify a struct type for a field?
Also, the definition for bar
was accepted, even though there is no such type foo
- every attempt to create a bar
will fail to typecheck!
Yes, struct definitions are seriously half-assed. No doubt.
I've been kicking around different ideas for how to improve it, but I haven't yet arrived at a clear vision for a total re-design of the struct
operator. Until I do, you can leave this issue open to remind me (and maybe to collect other ideas to improve struct definitions).
Potentially a more ADT style approach could be used, like data
in Haskell or enum
in Rust?