Contracts on parameters

瀏覽次數:14 次
跳到第一則未讀訊息

David Storrs

未讀,
2020年3月23日 下午3:36:392020/3/23
收件者:Racket Users
(define/contract (foo x)
(-> boolean? any)
'ok)

(foo #t)
'ok
(foo 7)
; foo: contract violation
; expected: boolean?
; given: 7
; in: the 1st argument of
; (-> boolean? any)
; contract from: (function foo)
; blaming: top-level
; (assuming the contract is correct)
; at: readline-input:4.18
; [,bt for context]

Yup, all good.


(define/contract foo (make-parameter #f) boolean?)
(foo #t)
(foo)
#t
(foo 7)
#f


This isn't what I expected. What am I missing?

Ben Greenman

未讀,
2020年3月23日 下午4:12:062020/3/23
收件者:David Storrs、Racket Users
1. the contract comes first, right now foo = boolean?

2. try parameter/c

Eric Griffis

未讀,
2020年3月23日 下午4:13:502020/3/23
收件者:David Storrs、Racket Users
AFK, but it looks like the contract and function on your define/contract are swapped. Maybe the contract check on the one-arg call sets the parameter and then hilarity ensues?

Eric


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAE8gKocV-TC1U6g5sq7C%2BZAjSVqDc%2B4yZWobr245qx4fZ9%2Bi3w%40mail.gmail.com.

David Storrs

未讀,
2020年3月23日 下午4:17:392020/3/23
收件者:Racket Users
Thanks, that was exactly it.

On Mon, Mar 23, 2020 at 3:51 PM Michael MacLeod
<michael...@gmail.com> wrote:
>
> I think you are looking for parameter/c. See https://docs.racket-lang.org/reference/data-structure-contracts.html?q=parameterof#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._parameter%2Fc%29%29 for more information.
>
> Best,
> Michael
>
> On Mon, Mar 23, 2020, 12:36 PM David Storrs <david....@gmail.com> wrote:
>>
回覆所有人
回覆作者
轉寄
0 則新訊息