Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: type aliases in Clarity #1171

Open
lgalabru opened this issue Nov 14, 2019 · 10 comments
Open

Proposal: type aliases in Clarity #1171

lgalabru opened this issue Nov 14, 2019 · 10 comments
Assignees
Labels
clarity icebox Issues that are not being worked on

Comments

@lgalabru
Copy link
Contributor

lgalabru commented Nov 14, 2019

In the context of the BNS contract, I'm dealing with the following type:

(price-function (tuple 
    (buckets (list 16 uint)) 
    (base uint) 
    (coeff uint) 
    (nonalpha-discount uint) 
    (no-vowel-discount uint)))

It could be nice, from a developer point of view, to have the ability to define type aliases, aiming at improving readability:

(define-type price-function-type (tuple (buckets (list 16 uint))
                                         (base uint)
                                         (coeff uint)
                                         (nonalpha-discount uint)
                                         (no-vowel-discount uint)))

It could be an edge case not worthing the effort - opening an issue for tracking this idea.

@njordhov
Copy link
Contributor

njordhov commented Dec 24, 2019

Custom types would be interesting. Also, perhaps we could have a generalized type test function like say (is ...) for custom types and in place of is-none, is-ok and the proposed is-err and is-some.

@stale
Copy link

stale bot commented Jun 21, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 21, 2020
@lgalabru
Copy link
Contributor Author

Would be addressed if the define-macro solution described in #1638 is adopted

@stale
Copy link

stale bot commented Dec 19, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Dec 19, 2020
@friedger
Copy link
Collaborator

Writing the same types in function parameters is long and tedious

@stale stale bot removed the stale label Dec 19, 2020
@MarvinJanssen
Copy link
Member

MarvinJanssen commented Jun 14, 2021

From the discussion: if implemented, these type definitions/aliases would be local to the current contract only.

Edit:

And an example where it would help:
https://github.com/MarvinJanssen/clarity-experiments/blob/95b5a77aed24fb401f7ce8b09c5e1d7772df953d/contracts/tiny-market.clar

(define-type listing
	{
		maker: principal,
		taker: (optional principal),
		token-id: uint,
		nft-asset-contract: principal,
		expiry: uint,
		price: uint,
		payment-asset-contract: (optional principal)
	}
)

(define-map listings {id: uint} listing)

(define-private (assert-can-fulfill (nft-asset-contract principal) (payment-asset-contract (optional principal)) (listing listing))
	(begin
		(asserts! (not (is-eq (get maker listing) tx-sender)) err-maker-taker-equal)
		(asserts! (match (get taker listing) intended-taker (is-eq intended-taker tx-sender) true) err-unintended-taker)
		(asserts! (< block-height (get expiry listing)) err-listing-expired)
		(asserts! (is-eq (get nft-asset-contract listing) nft-asset-contract) err-nft-asset-mismatch)
		(asserts! (is-eq (get payment-asset-contract listing) payment-asset-contract) err-payment-asset-mismatch)
		(ok true)
	)
)

@njordhov
Copy link
Contributor

njordhov commented Sep 23, 2021

ClarityTools brings type aliases to Clarity, generating the polyfill. Check it out:

https://shrtco.de/gYMidi

Use Generate Clarity from the Toolbox menu for the canonical code.

@MarvinJanssen
Copy link
Member

MarvinJanssen commented May 11, 2022

Edit: I just saw the label changes by @jcnelson. I'll keep the post though.

I'd like to bump this. Has any determination been made on whether or not to include such a feature?

I think it could be a fantastic additional also for standardisation of types. Now that it seems that SIP018 is coming to Stacks we will see more custom tuple times be part of a larger protocol.

Next to a define-type, I also want to suggest a use-type that works just like use-trait but for types. The signature can look like this:

(use-type type-alias type-identifier)

Which would allow us to do something like this:
contract-a.clar:

(define-type order {maker: principal, amount: uint, salt: uint})

contract-b.clar:

(use-type defi-order 'SP123.contract-a.order)

(define-public (some-func (order defi-order))
;; ...
)

Thoughts?

@jcnelson
Copy link
Member

Assigning to @obycode for now. Please re-assign as appropriate.

@obycode
Copy link
Contributor

obycode commented Mar 6, 2023

I like this idea too. I'm going to open an issue in the SIPs repo to track the proposal, then leave this one here for the implementation.

@obycode obycode added the icebox Issues that are not being worked on label Mar 6, 2023
@github-project-automation github-project-automation bot moved this to 🆕 New in Stacks Core Eng Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clarity icebox Issues that are not being worked on
Projects
Status: Status: 🆕 New
Development

No branches or pull requests

7 participants