shu--hung
2019-11-12 19:08:36

If I want to export a struct declaration and a function producing the same struct, do I need to repeat the struct contract twice?


shu--hung
2019-11-12 19:17:11

For example, is this necessary? #lang racket (provide (contract-out (struct S ([x number?])) [f (-> S/c)])) (struct S (x)) (define S/c (struct/c S number?)) (define (f) (S 123))


samth
2019-11-12 19:18:18

could you just use S?


shu--hung
2019-11-12 19:21:32

If f produces incorrect values, that will blame S-x instead of f


notjack
2019-11-12 19:21:53

What @samth said. Beware that this does make it possible for functions inside the module to put bad values into the struct fields, so just because the struct is provided with a contract does not mean it’s always impossible to get bad field values. If you want to prevent all instances from having bad field values, rather than just instances created by other modules, you should use the struct-guard/c contract and the #:guard option:

(struct S (x) #:guard (struct-guard/c number?))

samth
2019-11-12 19:22:01

yes, it all depends on who you’re trusting


shu--hung
2019-11-12 19:23:08

@notjack Cool! I did not know struct-guard/c existed


shu--hung
2019-11-12 19:23:47

I don’t struct both parties so :disappointed:


shu--hung
2019-11-12 19:23:53

*trust


notjack
2019-11-12 19:25:10

If your structs contain known types of data (as in, they’re not polymorphic), and they’re used just as plain transparent data carriers rather than like opaque holders of functions, you probably want to use struct-guard/c unless you have a specific performance reason not to.


shu--hung
2019-11-12 19:28:28

Thanks! I’ll go with that and use S? in the contract for functions


notjack
2019-11-12 19:30:01

oh, small wrinkle I forgot: struct-guard/c can’t express contracts like “the number in the min field must be less than the number in the max field”


notjack
2019-11-12 19:30:26

There’s no reason it couldn’t be extended to support that, that just hasn’t been asked for by anyone yet


rebornwwp
2019-11-13 03:36:16

@rebornwwp has joined the channel