clarity-lang / reference

The Clarity Reference
146 stars 34 forks source link

Principal refinement types #24

Open njordhov opened 4 years ago

njordhov commented 4 years ago

Principal refinement types refine the native Principal type with optional traits.

Refined types of principals are declared to implement the specific traits, benefiting static analysis. They are similar to the Integer refinement types proposed in issue #13, the String refinement types proposed in #19, and the List type refined with an entry-type. They are suited to be used for the type aliases proposed in issue #6.

Example:

(define-trait can-sell
  ((sell (principal uint) 
         (response bool))))

(define-public (trade (market (principal can-sell)) 
                      (buyer principal) 
                      (price uint))
  (contract-call? market sell buyer price))

Related: https://github.com/blockstack/stacks-blockchain/pull/1679

psq commented 4 years ago

this would have the advantage of not requiring a special syntax to identify traits, i.e. <can-sell>

obycode commented 2 years ago

I like this syntax. It would also be easy to specify multiple traits (e.g. (principal can-sell can-auction)), instead of having to create a new trait that combines multiple traits.