# User-defined Value Types

**URL:** <https://forum.soliditylang.org/t/user-defined-value-types/314>\
**Category:** Language Design\
**Created:** [June 14, 2021, 12:47pm UTC](https://forum.soliditylang.org/t/user-defined-value-types/314 "2021-06-14T12:47:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriseth](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/chriseth/32/24_2.png) [@chriseth](https://forum.soliditylang.org/u/chriseth)\
**Post date:** [June 14, 2021, 12:47pm UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/1 "2021-06-14T12:47:49Z")

</div>

@maurelian suggested a feature for Solidity that has a very good implementation cost / usefulness ratio in my opinion. It would be great to get some feedback on it from Solidity users:

> <https://github.com/ethereum/solidity/issues/11531>
>
> Originally proposed by @maurelian 
> 
> It should be possible to define a user-def…ined type that is identical to a provided value type (including operators, members, etc.), but cannot be implicitly converted to any other type, and can only be explicitly converted to and from the underlying value type.
> 
> This has the effect that arithmetic operations are impossible to perform across these types.
> 
> Example (syntax under discussion):
> 
> \`\`\`
> typedef DistanceInMeters uint;
> typedef DistanceInInch uint;
> typedef Price fixed128x10;
> 
> DistanceInMeters distanceToDestination;
> Price buyerPrice;
> Price sellerPrice;
> \`\`\`
> 
> In the above, you can do \`buyerPrice - sellerPrice\`, but you cannot do \`buyerPrice \* distanceToDestination\`.

(issue description copied here for convenience)

It should be possible to define a user-defined type that is identical to a provided value type (including operators, members, etc.), but cannot be implicitly converted to any other type, and can only be explicitly converted to and from the underlying value type.

This has the effect that arithmetic operations are impossible to perform across these types.

Example (syntax under discussion):

```auto
typedef DistanceInMeters uint;
typedef DistanceInInch uint;
typedef Price fixed128x10;

DistanceInMeters distanceToDestination;
Price buyerPrice;
Price sellerPrice;

```

In the above, you can do `buyerPrice - sellerPrice`, but you cannot do `buyerPrice * distanceToDestination`.

---

<div class="post-metadata">

**Author:** ![rumkin](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/rumkin/32/79_2.png) [@rumkin](https://forum.soliditylang.org/u/rumkin)\
**Post date:** [June 14, 2021, 2:53pm UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/2 "2021-06-14T14:53:36Z")

</div>

It looks good and very straightforward solution. What’s about expressions like `buyerPrice + 1`? Would `1` be converted to Price type? And how to convert two types from different codebases: Would it work like `PriceA(PriceB(1))` or with intermediate type conversion, like `PriceA(uint(PriceB(1))`?

---

<div class="post-metadata">

**Author:** ![wighawag](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/wighawag/32/43_2.png) [@wighawag](https://forum.soliditylang.org/u/wighawag)\
**Post date:** [June 15, 2021, 4:48am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/3 "2021-06-15T04:48:43Z")

</div>

Hi, I would love to see this. It would also be great if these type were exported as part of the abi too so type safety could be preserved at the calling side (in language that can support it)

We could also go further with conversion function between the type.

A language that I implement this entirely at complile time is [haxe](https://haxe.org), they call them ‘abstract type’ see [Abstract - Haxe - The Cross-platform Toolkit](https://haxe.org/manual/types-abstract.html)

---

<div class="post-metadata">

**Author:** ![MicahZoltu](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/micahzoltu/32/67_2.png) [@MicahZoltu](https://forum.soliditylang.org/u/MicahZoltu)\
**Post date:** [June 15, 2021, 11:03am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/4 "2021-06-15T11:03:47Z")

</div>

Just for clarity in communication, these are called Nominal Types. I’m a huge fan of it as it adds robustness to the type system.

---

<div class="post-metadata">

**Author:** ![chriseth](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/chriseth/32/24_2.png) [@chriseth](https://forum.soliditylang.org/u/chriseth)\
**Post date:** [June 16, 2021, 10:09am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/5 "2021-06-16T10:09:59Z")

</div>

So it looks like there is some interest in this feature, great!

IMO, `buyerPrice + 1` would not be possible, you would need to do `buyerPrice + Price(1)` and there is also no explicit conversion without going through the underlying type, although I guess it would be fine to allow this if the underlying type is the same.

Are there any suggestions for the syntax?

---

<div class="post-metadata">

**Author:** ![MicahZoltu](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/micahzoltu/32/67_2.png) [@MicahZoltu](https://forum.soliditylang.org/u/MicahZoltu)\
**Post date:** [June 17, 2021, 8:34am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/6 "2021-06-17T08:34:20Z")

</div>

Assuming it doesn’t create a bunch of implementation difficulty, I have a weak preference for _literals_ being implicitly converted, just not variables. So `buyerPrice + 1` is fine, but `buyerPrice + someConstantWithDifferentType` is not.

---

<div class="post-metadata">

**Author:** ![frangio](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/frangio/32/673_2.png) [@frangio](https://forum.soliditylang.org/u/frangio)\
**Post date:** [June 17, 2021, 5:22pm UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/7 "2021-06-17T17:22:20Z")

</div>

> [@chriseth](#):
>
> identical to a provided value type (including operators

If operators are inherited the feature loses most of its value IMO.

As an example you shouldn’t be able to add timestamps to one another. On the other hand, subtraction makes sense but the result is not a timestamp.

I agree with @hrkrshnn in [#11531](https://github.com/ethereum/solidity/issues/11531) that this should be more like Haskell’s `newtype`. Personally I think arithmetic operators don’t need to be overloaded for this, and normal functions should be used.

* * *

I’ve long wanted structs with private fields, but if this feature is easier to build, with `newtype` semantics it would solve some of the same problems.

---

<div class="post-metadata">

**Author:** ![rumkin](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/rumkin/32/79_2.png) [@rumkin](https://forum.soliditylang.org/u/rumkin)\
**Post date:** [June 18, 2021, 2:22am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/8 "2021-06-18T02:22:39Z")

</div>

I think `buyerPrice + 1` should be implicitly converted, because it makes code more readable and less noisy.

What’s about syntax. I’d like to use `type` instead of `typedef` to define custom types. And rename current [`type`](https://docs.soliditylang.org/en/v0.8.5/units-and-global-variables.html?highlight=type#type-information) into `reflect` as “reflection” is widely adopted name for this kind of functionality and better suits for function name.

---

<div class="post-metadata">

**Author:** ![alcuadrado](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/alcuadrado/32/251_2.png) [@alcuadrado](https://forum.soliditylang.org/u/alcuadrado)\
**Post date:** [June 19, 2021, 10:21pm UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/9 "2021-06-19T22:21:28Z")

</div>

> [@frangio](#):
>
> If operators are inherited the feature loses most of its value IMO.
> 
> As an example you shouldn’t be able to add timestamps to one another. On the other hand, subtraction makes sense but the result is not a timestamp.
> 
> I agree with @hrkrshnn in [#11531](https://github.com/ethereum/solidity/issues/11531) that this should be more like Haskell’s `newtype`. Personally I think arithmetic operators don’t need to be overloaded for this, and normal functions should be used.

I completely agree with this.

I think the hack mentioned by @hrkrshnn in that issue shows that the `newtype` pattern would be useful for types other than numeric values. Note that this hack is actually used in practice, for example, by Balancer v2.

---

<div class="post-metadata">

**Author:** ![chriseth](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/chriseth/32/24_2.png) [@chriseth](https://forum.soliditylang.org/u/chriseth)\
**Post date:** [June 21, 2021, 9:22am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/10 "2021-06-21T09:22:13Z")

</div>

It seems there are somehow conflicting opinions about operators, but I think it could be possible to find a common ground. For example by explicitly specifying which operators are included from the underlying type (see the issue).

Would it be useful to allow to define “custom operators”? For example an operator that is implemented through a user-defined function and defines “timestamp - timestamp → timedifference”?

---

<div class="post-metadata">

**Author:** ![hrkrshnn](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/hrkrshnn/32/22_2.png) [@hrkrshnn](https://forum.soliditylang.org/u/hrkrshnn)\
**Post date:** [June 21, 2021, 9:43am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/11 "2021-06-21T09:43:16Z")

</div>

I am not sure any more about defining arithmetic operators on such types. Mainly about the question on whether safemath should apply for such operators. If it does, should `unchecked` blocks use wrapping arithmetic?

Wrote about it in the issue: [User-defined value types · Issue #11531 · ethereum/solidity · GitHub](https://github.com/ethereum/solidity/issues/11531#issuecomment-864887429)

> Would it be useful to allow to define “custom operators”?

If we decide to define operators on custom types, explicitly defining it should be the way to go. And somehow the behaviour of operators in `unchecked` blocks should also be made clear from the declaration.

---

<div class="post-metadata">

**Author:** ![hrkrshnn](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/hrkrshnn/32/22_2.png) [@hrkrshnn](https://forum.soliditylang.org/u/hrkrshnn)\
**Post date:** [June 21, 2021, 9:54am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/12 "2021-06-21T09:54:43Z")

</div>

> [@rumkin](#):
>
> What’s about syntax. I’d like to use `type` instead of `typedef` to define custom types. And rename current [`type`](https://docs.soliditylang.org/en/v0.8.5/units-and-global-variables.html?highlight=type#type-information) into `reflect` as “reflection” is widely adopted name for this kind of functionality and better suits for function name.

Renaming the current `type` would be a breaking change and also an unnecessary change, in my opinion. Ideally, we could find a name that’s intuitive and doesn’t require breaking existing contracts.

---

<div class="post-metadata">

**Author:** ![frangio](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/frangio/32/673_2.png) [@frangio](https://forum.soliditylang.org/u/frangio)\
**Post date:** [June 23, 2021, 1:00am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/13 "2021-06-23T01:00:48Z")

</div>

Yeah I think if people want arithmetic operators it makes sense to be able to derive them, but I think it should be done by explicitly opting in to each operator as suggested in the issue.

Regarding `unchecked` for these operators I think it would make sense to inherit the same semantics that all operators currently have.

“Custom operators” as suggested by @chriseth don’t sound like a good idea to me at the moment, because it’s not very clear how they would behave in an `unchecked` block.

---

<div class="post-metadata">

**Author:** ![chriseth](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/chriseth/32/24_2.png) [@chriseth](https://forum.soliditylang.org/u/chriseth)\
**Post date:** [June 30, 2021, 1:08pm UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/14 "2021-06-30T13:08:55Z")

</div>

We have been discussing this issue in the latest meeting and did not really come to a conclusion. It would be very helpful for us if some people in this thread could share example code that would benefit from this feature. It does not have to be code that actually makes use of the feature, but code that you think could be rewritten using the feature would already be very very valuable for us.

---

<div class="post-metadata">

**Author:** ![frangio](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/frangio/32/673_2.png) [@frangio](https://forum.soliditylang.org/u/frangio)\
**Post date:** [July 1, 2021, 10:43pm UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/15 "2021-07-01T22:43:16Z")

</div>

Two examples from an upcoming `Timers` module:

> <https://github.com/OpenZeppelin/openzeppelin-contracts/blob/655f96b5e2e334f74e50a503ac053268c2548b3b/contracts/utils/Timers.sol#L9-L11>

> <https://github.com/OpenZeppelin/openzeppelin-contracts/blob/655f96b5e2e334f74e50a503ac053268c2548b3b/contracts/utils/Timers.sol#L41-L43>

---

<div class="post-metadata">

**Author:** ![chriseth](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/chriseth/32/24_2.png) [@chriseth](https://forum.soliditylang.org/u/chriseth)\
**Post date:** [July 5, 2021, 8:49am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/16 "2021-07-05T08:49:01Z")

</div>

Ah, thanks for that example! So you are also using `using Timers for Timestamp`, right?

---

<div class="post-metadata">

**Author:** ![hrkrshnn](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/hrkrshnn/32/22_2.png) [@hrkrshnn](https://forum.soliditylang.org/u/hrkrshnn)\
**Post date:** [July 5, 2021, 8:51am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/17 "2021-07-05T08:51:59Z")

</div>

Perhaps an example where disallowing operators is important is for library fixed points.

Say, a library designs a fixed point type based on `uint`.

```auto
type FixedPoint = uint deriving(==);

```

means that users wouldn’t accidentally use `+`, or `*` operator on these types.

---

<div class="post-metadata">

**Author:** ![chriseth](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/chriseth/32/24_2.png) [@chriseth](https://forum.soliditylang.org/u/chriseth)\
**Post date:** [July 5, 2021, 9:23am UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/18 "2021-07-05T09:23:36Z")

</div>

… although `+` would be totally fine 😉

---

<div class="post-metadata">

**Author:** ![frangio](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/frangio/32/673_2.png) [@frangio](https://forum.soliditylang.org/u/frangio)\
**Post date:** [July 5, 2021, 7:28pm UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/19 "2021-07-05T19:28:41Z")

</div>

Yeah, `using Timers for Timers.Timestamp` actually.

---

<div class="post-metadata">

**Author:** ![chriseth](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/chriseth/32/24_2.png) [@chriseth](https://forum.soliditylang.org/u/chriseth)\
**Post date:** [August 12, 2021, 4:55pm UTC](https://forum.soliditylang.org/t/user-defined-value-types/314/20 "2021-08-12T16:55:13Z")

</div>

We recently had some more ideas about operators and the using statement. Since we for now will implement user-defined value types simply without operators, I created a new thread for this more expansive and less specified topic: [User-defined types and operators](http://forum.soliditylang.org/t/user-defined-types-and-operators/456)
