stefan.kruger
2019-9-19 08:03:20

Trying to understand contracts. Looking at https://docs.racket-lang.org/guide/contracts-first.html I am in equal parts awestruck and disheartened — is that the expectation in terms of depth and complexity? It seems like contracts like that would exceed the complexity of the function implementation itself. Also, presumably contracts that validate every assumption made by the function would impact the performance?


jerome.martin.dev
2019-9-19 08:09:01

The guide seems intentionally over-engineering the contract to show all the possibilities, I guess.


jerome.martin.dev
2019-9-19 08:09:50

For what it’s worth, I personally never had to write such complex contracts.


notjack
2019-9-19 08:13:05

I have used dependant contracts but never to the level of complexity in that example


stefan.kruger
2019-9-19 08:27:34

I think my expectation would be to specify the signature, but leave the invariants to unit tests.


samth
2019-9-19 13:25:02

The guide there is showing a bunch of different variants of the contract, to show different possibilities. You can of course choose which level you want to specify contracts to.


stefan.kruger
2019-9-19 16:52:09

Feeling pleased with having discovered I can do this: (set-add! (hash-ref! ht key mutable-set) value)


stefan.kruger
2019-9-19 16:52:33

Like Python’s defaultdict(set)


notjack
2019-9-19 18:16:43

And there’s even a recommendation in the racket style guide about how to do that best: contacts by default, and add a no-contract submodule



notjack
2019-9-19 18:29:49

@stefan.kruger Huh, that looks like a mutable multidict https://docs.racket-lang.org/rebellion/Multidicts.html