@michaelmmacleod I’m not familiar with #:super
. Is that something new, or, a mistake? I’d just do (struct B A (b))
?
Oh. I guess #:super
is one of the many struct options I’ve never need to use and therefore about which I’m ignorant. :smile: Maybe contract-out
doesn’t understand how to use the struct-type struct:A
supplied with #:super
? Understanding why, more thoroughly, would be interesting (for me, someday). But meanwhile I am pretty sure (struct B A (b))
will just work.
I think 99% of the time I use struct subtypes, at all, is simply to express a kind of “union” or sum type, and you get the predicate for free.
(struct Message (id))
(struct Request Message (blah blurp))
(struct Response Message (bleep))
So now you get Message?
and you DRY the id
bit. That’s about as fancy as I get with struct subtypes. ¯_(ツ)_/¯
In that little example, make all the structs #:prefab
, and now you have a wire format to write
and read
. To work out some new system. Later, someday/maybe, if the raw bytes count really matters (sometimes it really doesn’t) then you can use something like bitsyntax
.
Meanwhile you’re not detoured by “premature ceremonialization”.
@greg Thanks! That’s definitely the problem — using the subtype struct syntax without #:super works. At first approximation #:super and super-id seem to do the same thing. It is a bit strange that #:super doesn’t work with contract-out. Actually, your example is pretty close to what I had in mind: (struct status (id))
(struct working status ())
(struct idle status ())
I’m using them to help manage the state of several place
s. Using subtype structs is nice because they work with match, ie (match (working 2)
[(status id) id])
Oh wow, @ryanc has a package for ASN.1. I think @tonyg and @ryanc are my top two Racket package heroes for tackling necessary but difficult things really well.
@michaelmmacleod Yep the situation where most or even all of the sub-structs don’t even add any fields, is something I’ve done, too.
It’s a nice way to express things.
@michaelmmacleod #:super
allows providing an arbitrary supertype dynamically, at runtime. The supertype syntax uses static, compile-time information associated with a particular binding.
You can, for example, write (define (f a) (struct b () #:super a) (b))
. You can’t do that with the supertype syntax.
You don’t usually want to use #:super
.
Okay it all makes sense now, thanks! I’ll stick with the super-id syntax, since I’m not doing any run-time struct creation.