As far as I know, this doesn’t exist in typed racket currently, but I’m wondering: (I’m thinking of seeing if I can implement this when I have time this summer)
How should sub-typing interact with this? Should it be a completely separate type, not related by sub-typing at all, or should the new type be a subtype of the representation type? What should happen if values with these types crossed into untyped code? What should happen if an untyped program passes a normal flonum to a function that was supposed to accept Radians? Would you want to be able to supply a predicate to check properties of the values of the new type? If you want them to be completely separate types, then I think what you have with struct will work fine, and the link you provided does say that Haskell newtypes are similar to using data with a single variant with a single field. Although if it matters to you there will be the extra overhead of allocating the structs, accessing the fields and that kind of thing, which could be avoided if a newtype-like feature existed. Alex Knauth On May 23, 2015, at 4:21 PM, Stuart Hungerford <stuart.hungerf...@gmail.com> wrote: > Hi, > > Is there an idiomatic Typed Racket equivalent to the Haskell concept of > newtypes? > > https://www.haskell.org/tutorial/moretypes.html > > That is, some way to have separate types that share a common structure? > > For example I'd like to define "Radians" and "Degrees" types that are both > implemented as Flonums but have Typed Racket raise a type error when a > Radians value is used where a Degrees value is expected. I can see using > single field structs might work: > > (struct: Radians ([a : Flonum])) > > (struct: Degrees ([a : Flonum])) > > ; type checks > (: d-90 : Degrees) > (define d-90 (Degrees 90.0)) > > ; type mismatch! > (: r-PI : Radians) > (define r-PI (Degrees 180.0)) > > > Is there a better way of working with the Typed Racket idioms to do this kind > of thing? > > Thanks, > > Stu -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.