djtango
2016-11-10 17:07:10

Hi - I am working with typed/racket and had a question about subtyping…

Is it possible to define a subtype with the intention of making your code more specific such that the following would work: (define-type ABCD String) -> (define (only-ABCD [abcd : ABCD]) : ABCD abcd) -> (only-ABCD (ann "a" ABCD)) - : ABCD "a" But: (only-ABCD "a") ; readline-input:1:1 Type Checker: type mismatch ; expected: ABCD ; given: String ; in: "a" ; [,bt for context]


djtango
2016-11-10 17:13:03

The use-case I have in mind is if a function has 5 string arguments, you could subtypes in this manner to make your code more expressive and make the type-check prove more about your program than if you simply had (-> String String String String String) rather than (-> ABC DEF GHI JKL MNO)


lexi.lambda
2016-11-10 17:21:36

@djtango: You can use a struct to create a wrapper with a single field, but that won’t let you use that value as a String as well. If you want that, you could use the define-new-subtype form: http://docs.racket-lang.org/ts-reference/Experimental_Features.html#%28form._%28%28lib._typed-racket%2Fbase-env%2Fprims..rkt%29._define-new-subtype%29%29


djtango
2016-11-10 17:24:15

@lexi.lambda thanks for that - didn’t see the define-new-subtype in the docs. Will take a look at that. If I go down the struct path, I will need to go over my code and use an accessor, right?


lexi.lambda
2016-11-10 17:33:41

@djtango: Yes, that’s correct.


djtango
2016-11-10 17:34:09

yep thought so - looks like define-new-subtype is the way to go then. Thanks for the help!