# \[BUG\] HashMap assignable to TrieMap

**URL:** <https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577>\
**Category:** Developers\
**Created:** [July 27, 2022, 9:44pm UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577 "2022-07-27T21:44:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [July 27, 2022, 9:44pm UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/1 "2022-07-27T21:44:01Z")

</div>

I think this should throw an error:

```auto
    private var _registry : TrieMap.TrieMap<Types.TokenIndex, Types.AccountIdentifier> = HashMap.fromIter(state._registryState.vals(), 0, ExtCore.TokenIndex.equal, ExtCore.TokenIndex.hash);

```

@diegop @claudio

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [July 27, 2022, 10:42pm UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/2 "2022-07-27T22:42:21Z")

</div>

Due to structural subtyping, this is allowed if the structure of the two types are compatible.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [July 27, 2022, 10:44pm UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/3 "2022-07-27T22:44:23Z")

</div>

Yes, it should. Do you have errors above this line that prevents the compiler from checking the error?

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [July 28, 2022, 6:11am UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/4 "2022-07-28T06:11:00Z")

</div>

No errors, everything compiles fine and I can deploy the canister as well.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [July 28, 2022, 6:13pm UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/5 "2022-07-28T18:13:13Z")

</div>

Paul is right. Both Map happens to have exactly the same type…It is confusing that you might be thinking one data structure and the code is using another one. But from the typing perspective, it is correct.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [July 28, 2022, 8:14pm UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/6 "2022-07-28T20:14:24Z")

</div>

Although there’s definitely room for improvement in this implementation, I think this might be one of the reasons why it’s preferable to use “exposed” data structures and functions that operate on those data structures instead of class wrappers.

For example, something like [StableHashMap/FunctionalStableHashMap.mo at main · canscale/StableHashMap · GitHub](https://github.com/canscale/StableHashMap/blob/main/src/FunctionalStableHashMap.mo)

The [StableHashMap type](https://github.com/canscale/StableHashMap/blob/main/src/FunctionalStableHashMap.mo#L37) exposed here is different than the [underlying data structure/size variables in the TrieMap](https://github.com/dfinity/motoko-base/blob/master/src/TrieMap.mo#L27), which were the TrieMap converted from a class to a record + collection of functions, you wouldn’t be able to assign these types interchangeably.

@chenyan it would be nice though if the instance variable `var` types within classes were compared, and not just the function signatures. Maybe a key word to denote instance variables, which may not be public, but that are part of the underlying class-related data stored within.

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [July 29, 2022, 6:50am UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/7 "2022-07-29T06:50:56Z")

</div>

TBH I find this very odd, even though it’s correct. I almost missed setting the variable to a TrieMap, as I’m heavily relying on the compiler as my best friend and saviour 😃

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [July 29, 2022, 9:04am UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/8 "2022-07-29T09:04:17Z")

</div>

This is probably a good example of why tests are still necessary in typed languages 🙂

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [July 29, 2022, 9:33am UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/9 "2022-07-29T09:33:00Z")

</div>

Out of curiosity, how would you test for something like this?

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [July 29, 2022, 10:30am UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/10 "2022-07-29T10:30:47Z")

</div>

I suppose in this case you can’t since whether it’s a HashMap or TrieMap appears to be an implementation detail.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [July 29, 2022, 1:03pm UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/11 "2022-07-29T13:03:30Z")

</div>

I should perhaps capitalize on this opportunity to (yet again) shill variants 😄

The “newtype” pattern exists for situations where the type checker sees types as the same but you’d like to attach some additional meaning to them _and_ get some help from the type system.

For example, this isn’t going to help much:

```auto
type Kilometers = Nat;
type Miles = Nat;

```

With the above we can still easily provide a value representing kilometers somewhere we should be providing a value in miles.

However, we can do the following instead:

```auto
type Kilometers = { #km : Nat };
type Miles = { #miles : Nat };

```

Now we have to tag values with the desired units and the type checker will prevent us from providing incorrect values.

We can even do this:

```auto
type Distance = Kilometers or Miles;

```

I mention this in my talk from Motoko Bootcamp:

[![](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/9/954fb4600169a53c5ffc576046cfccee6ce66a39.jpeg "Motoko Bootcamp V1 2022 | Motoko Variant types") ](https://www.youtube.com/watch?v=GFenqSGhj7I)

So, perhaps you’d prefer to do this for the various Map implementations. e.g.

```auto
type HashMap<K, V> = { #hashMap : HashMap<K, V> };
type TrieMap<K, V> = { #trieMap : TrieMap<K, V> };

```

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [July 29, 2022, 2:01pm UTC](https://forum.dfinity.org/t/bug-hashmap-assignable-to-triemap/14577/12 "2022-07-29T14:01:02Z")

</div>

For the same reasons as above, it wouldn’t prevent programming mistakes like this though:

```auto
private var _registry
  : TrieMap<Types.TokenIndex, Types.AccountIdentifier>
  = #trieMap HashMap.fromIter(state._registryState.vals(), 0, ExtCore.TokenIndex.equal, ExtCore.TokenIndex.hash);

```
