-
-
Notifications
You must be signed in to change notification settings - Fork 804
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
VIP: Allow declaring custom units #752
Comments
I think this a decent idea. Perhaps we should consider sometype of predefined unit table ? units = (
('m', 'meter'),
('cm', 'centimeter')
) This would force a description on each unit used in the contract. |
Sounds good, I think it will be better. I will update the VIP in accordance with a syntax for that. I will also add it to the list of subjects for the next meeting. Sent with GitHawk |
I wamted the unit labels to be double quoted so it's obvious that those are labels and not types or variables |
Example: stocks_count: int128("stock")
wei_per_stock: int128("wei" / "stock") I don't think we need an extra syntax to declare a unit, if you made a spelling mistake it will tell you when you compile, because that's the whole point of units. |
I think a syntax like @jacqueswww proposed would be better both because it gives a dedicated section with specifications (which makes it more readable IMO) and to prevent spelling mistakes (the compiler can't always recognize the spelling mistakes and even if it does it can't point out where is the mistake and it can make it harder for debugging). |
Hmm, a hybrid of the two? units = (
("stock", "one ownership share"),
# "wei" comes for free (built-in)
)
stocks_count: int128("stock")
wei_per_stock: int128("wei" / "stock") |
If there's ever a misspelling it could find it at the front because it won't exist in the table |
On that note, we definitely need the built-in units to be listed in the documentation under 'Types/Unit Types'. units: (
("stock", "one share")
) units: {
"stock": "one share",
"m": "meter",
"int128": "hahaha" # this should fail obviously.
} |
Idea is that each file extends the base units for itself |
The second syntax looks good IMO. Sent with GitHawk |
I'm not opposed to |
Approved, we should only allow basically one layer deep operator (divide/multiply/exponential).
|
Closing as it's implemented. Great work! |
Preamble
Simple Summary
Add a new keyword
unit
to allow declaring custom units like so:Abstract
Adding a way to create custom units can make the code more readable and improve security (type-safety).
Motivation
The motivation for this came as I saw that the Casper contract uses labels incorrectly. they use the
m
which stands for meter where they clearly didn't meant to represent meters. This make the code very confusing and might be a bigger issue in the future.Currently, there are a few units Vyper support, but there are much more measurements Vyper doesn't take in count. For example, in the company.v.py the stocks are labeled as
currency
while it actually should be astock
.Specification
The proposed
unit
declaration is as below:However, it may be declared in a different way such as with a dedicated function.
Backwards Compatibility
This change is backward compatible
Copyright
Copyright and related rights waived via CC0
Cute Animal Picture
The text was updated successfully, but these errors were encountered: