# I'm not sure how to create a simple Currency type

**URL:** <https://forum.crystal-lang.org/t/im-not-sure-how-to-create-a-simple-currency-type/6102>\
**Category:** Help & Support\
**Created:** [October 20, 2023, 4:46am UTC](https://forum.crystal-lang.org/t/im-not-sure-how-to-create-a-simple-currency-type/6102 "2023-10-20T04:46:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MyrddinE](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/myrddine/32/2195_2.png) [@MyrddinE](https://forum.crystal-lang.org/u/MyrddinE)\
**Post date:** [October 20, 2023, 4:46am UTC](https://forum.crystal-lang.org/t/im-not-sure-how-to-create-a-simple-currency-type/6102/1 "2023-10-20T04:46:40Z")

</div>

I want to extend Int (or UInt64 specifically, if needed) and do a couple things:

Override the comparison (\<=\>) operators to compare against the value divided by 1000.

Override the to\_s method to format it appropriately.

But you can’t inherit from Int, since it’s a struct. How do you create a new reference type based on another reference type? I’m sure there’s a way, but I got lost. Do I need to box it, even though it’s actual data is still a reference type?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [October 20, 2023, 4:49am UTC](https://forum.crystal-lang.org/t/im-not-sure-how-to-create-a-simple-currency-type/6102/2 "2023-10-20T04:49:33Z")

</div>

I think your best bet would be to use the decorator pattern and wrap an integer value to expose your API. Then if you wanted you could monkey patch in helper methods like `7.dollars`, similar to how `4.hours` works.

```crystal
class Dollar
  include Comparable(self)

  def initialize(@value : UInt64); end

  # ...
end

```

The catch of course being it wouldn’t be compatible with your usual `Int` type restrictions, but I don’t think there’s a way around that.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [October 20, 2023, 6:24am UTC](https://forum.crystal-lang.org/t/im-not-sure-how-to-create-a-simple-currency-type/6102/3 "2023-10-20T06:24:08Z")

</div>

> [@Blacksmoke16](#):
>
> The catch of course being it wouldn’t be compatible with your usual `Int` type restrictions, but I don’t think there’s a way around that.

Id consider that a feature. Such value types are distinct types exactly for the reason that they are not just a dimensionless number value.

---

<div class="post-metadata">

**Author:** ![MyrddinE](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/myrddine/32/2195_2.png) [@MyrddinE](https://forum.crystal-lang.org/u/MyrddinE)\
**Post date:** [October 20, 2023, 4:06pm UTC](https://forum.crystal-lang.org/t/im-not-sure-how-to-create-a-simple-currency-type/6102/4 "2023-10-20T16:06:22Z")

</div>

I appreciate the assistance. I’m attempting to move from Ruby to Crystal for my own personal coding, and learning the differences in best-practices is a challenge that their superficial similarity shrouds.

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [October 20, 2023, 6:29pm UTC](https://forum.crystal-lang.org/t/im-not-sure-how-to-create-a-simple-currency-type/6102/5 "2023-10-20T18:29:19Z")

</div>

Also I would use a `struct` not a class for this:

```crystal
struct Dollar
  include Comparable(self)

  def initialize(@value : Int64); end

  # ...
end

```

That way it’s a value type, GC is not bothered and you can still customize the to\_s as wanted.  
Doing this composition instead of inheritance of Int allows you to not treat values of Dollar as Int which I agree with @straight-shoota is an upside.
