Aiken equivalent for Haskell's newtype
#830
Replies: 4 comments 2 replies
-
@fallen-icarus you can do this
|
Beta Was this translation helpful? Give feedback.
-
Nice! I just tested it and it does look like it works for my needs. I did not know opaque types 1) have the same uplc representation as the underlying type and 2) unwrapping the type with functions has zero cost. This is exactly what I was looking for. Thanks! The documentation doesn't seem to have any mention of this, though. Given the resource constraints of smart contracts, I think this should be included in the documentation for opaque types. Specifically, I would include that you can use it to create types that only exist at compile time and are fully optimized away in the uplc. Enforcing invariants is not the only use case. |
Beta Was this translation helpful? Give feedback.
-
@fallen-icarus it's a directly little secret that if the type is opaque and has a single field then it will be zero cost |
Beta Was this translation helpful? Give feedback.
-
I just tried using the latest commit (ad48409) to build my project, but it spit out this error:
I understand the reasoning behind this, but this breaks the abstraction of a I've been using opaque types like this extensively since I just want the type separation; I still manually check things when needed. As an example where this really helps, in my datum of 10+ fields, several of them are just If you really want this restriction, I'd like to suggest that this becomes a warning, and have the warning urge developers to double check they are using it correctly. For example, you could create an |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if this has been discussed before; I have not found any mention of it. I would like to be able to create a type that only exists at compile time. I don't want any unwrapping to occur in production. I just want to leverage the type checker to guarantee I use things properly. The behavior I want is identical to Haskell's
newtype
where the declared type has the same performance as the underlying type.As an example use case, I have user identities represented by
ByteString
but they should not be used the same way. If user 1 is a borrower, then theByteString
for that user id should not be used the same way as user 2, who is a lender. If I could declare something like:then, I could leverage the compiler's type checking like this:
The compiler should throw an error if I pass in a
LenderId
, but performance wise, it should be identical toByteString
.Beta Was this translation helpful? Give feedback.
All reactions