clarity-lang / reference

The Clarity Reference
149 stars 34 forks source link

Unable to directly extract a trait from a tuple #55

Open obycode opened 1 year ago

obycode commented 1 year ago

In Clarity 2, we should be able to store a trait in a tuple and then extract it and use it as any other trait.

(define-trait trait-foo ((foo () (response uint uint))))
(define-public (call-foo-tuple (t { address: <trait-foo>, amount: uint }))
  (contract-call? (get address t) foo)
)

For the above contract, the type-checker reports missing contract name for call. As a workaround, the trait can be called indirectly by passing it first in a function call.

(define-private (call-foo (f <trait-foo>))
  (contract-call? f foo)
)
(define-public (call-foo-tuple-indirect (t { address: <trait-foo>, amount: uint }))
  (call-foo (get address t))
)

Corresponding issue: https://github.com/stacks-network/stacks-blockchain/issues/3566