clarity-lang / reference

The Clarity Reference
149 stars 34 forks source link

Shorthand tuple properties #60

Open hugocaillard opened 1 year ago

hugocaillard commented 1 year ago

Allow shorthand properties syntax in tuples. If the value is a let variable, an argument or a constant that has the same name as the key, the value could be omitted.

For example, here is a function from the cost-voting contract:

;; before
(define-public (submit-proposal
  (funtion-contract principal)
  (function-name (string-ascii 128))
  (cost-function-contract principal)
  (cost-function-name (string-ascii 128))
)
  (begin
    (map-insert proposals 
      { proposal-id: (var-get proposal-count) }
      {
        cost-function-contract: cost-function-contract,
        cost-function-name: cost-function-name,
        function-contract: function-contract,
        function-name: function-name,
        expiration-block-height: (+ block-height VOTE_LENGTH)
      }
    )
    ;; ...
  )
)

It could be rewritten into

;; after
(define-public (submit-proposal
  (function-contract principal)
  (function-name (string-ascii 128))
  (cost-function-contract principal)
  (cost-function-name (string-ascii 128))
)
  ;; assigning it to a variable here for demonstration purpose
  (let ((expiration-block-height (+ block-height VOTE_LENGTH)))
    (map-insert proposals 
      { proposal-id: (var-get proposal-count) }
      {
        cost-function-contract,
        cost-function-name,
        function-contract,
        function-name,
        expiration-block-height,
      }
    )
    ;; ...
  )
)