1. 32

  2. 2

    Can you combine the constraints that affine types let you encode with the constraints that ADTs let you encode (link was about F# but technique is applicable to most anything with ADTs, which should include Rust), to achieve encodings of maybe even quite complicated invariants?

    1. 2

      I don’t see any particular reason that you couldn’t as long as these affine rules still apply for enums.

      For example …

      enum Item{
      pub struct EmptyEnvelope { }
      /// A closed envelope containing a letter.
      /// perhaps some rust afficionado can tell me if this is correct
      pub struct ClosedEnvelope {
          item: Item,
      impl EmptyEnvelope {
          /// Put a letter in the envelope and seal it.
          pub fn wrap(self, item: Item) -> ClosedEnvelope {
              ClosedEnvelope { item: Item }

      However I feel that you’d find the lack of recursive sum types to be pretty limiting if you went forward with this style for defining a structural hierarchy of your valid data representations. Also I could be mistaken I don’t think you can put reference types in your enum there.

      1. 2

        You would usea smart pointer combined with Option to represent recursive enums. Usually Option<Box<MyEnum>>