1

I'm trying to define a contract for a struct which inherits from another struct.

#lang racket

(require racket/contract racket/contract/parametric)

(struct semigroup (op))

(struct monoid (mempty) #:super struct:semigroup)

(define (semigroup/c a) (struct/dc semigroup [op (-> a a a)]))

(define (monoid/c a) 
    (struct/dc monoid [mempty a] [(op #:parent semigroup) (-> a a a)]))

I get an error

struct/dc: expected an identifier that names a field or a sequence with a field name, the #:parent keyword, and the parent
struct
  at: (op #:parent semigroup)
  in: (struct/dc monoid (mempty a) ((op #:parent semigroup) (-> a a a)))

I fail to see what I'm doing wrong.

I'm using Racket 6.9.

Charles Langlois
  • 4,198
  • 4
  • 16
  • 25
  • 2
    I don’t have time to type a full answer write now, but your program will work if you write `(struct monoid semigroup (mempty))` instead of using a `#:super` clause. The gist is that `#:super` sets up a subtyping relationship dynamically, at runtime, but `struct/dc` needs static supertype information. – Alexis King Jul 25 '17 at 07:28
  • Ok. Thanks. If you post an answer, I'll accept it. – Charles Langlois Jul 26 '17 at 09:04

0 Answers0