# Dictionnary of Dictionnaries issue: {UInt32:{UInt32:String}}

**URL:** <https://forum.flow.com/t/dictionnary-of-dictionnaries-issue-uint32-uint32-string/2181>\
**Category:** 🏄🏻‍♀️ Cadence\
**Created:** [July 12, 2021, 5:09pm UTC](https://forum.flow.com/t/dictionnary-of-dictionnaries-issue-uint32-uint32-string/2181 "2021-07-12T17:09:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Creator](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/creator/32/580_2.png) [@Creator](https://forum.flow.com/u/Creator)\
**Post date:** [July 12, 2021, 5:09pm UTC](https://forum.flow.com/t/dictionnary-of-dictionnaries-issue-uint32-uint32-string/2181/1 "2021-07-12T17:09:31Z")

</div>

Using a dictionnary containing dictionnaries as values ex {UInt32:{UInt32:String}} may be quite harmful

Please find the code below explaining the problem and a suggested solution

```
pub fun main() {

    let dictionnary:{UInt32:{UInt32:String}} = {}
    dictionnary[0] = {1 : "Value1", 2 : "Value2"}

    // Following code: Won't work and throw "cannot index into value which has type: `{UInt32: String}?`"
    let test2 = dictionnary[0][2]
    log(test2)

    // Following code: Won't work and throw "cannot index into value which has type: `{UInt32: String}?`"
    // Force Unwrap operator ! will not work here, has it is applied on second index in the inner dictionnary
    let test2 = dictionnary[0][2]!
    log(test2)

    // Following code: Will work but :
    // dictionnary[0] has to be fully copied to a new constant and unwrapped
    // may be time and resource consuming if dictionnary[0] contains many data
    let test1 = dictionnary[0]!
    log(test1[2])

    // Also for assiging new values you need to assing value to the unwrapped copy (test1) then assign the copy to the parent dictionnary

    // Conclusion
    // Using an inner dictionnary in a dictionnary may be Harmful has it requires a copy to access/set its values
    // Proposed Solution
    // Update the operator unwrap ! to cascade unwrap
}

```

Thanks to Drapper Labs team

---

<div class="post-metadata">

**Author:** ![juyeon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/juyeon/32/273_2.png) [@juyeon](https://forum.flow.com/u/juyeon)\
**Post date:** [July 19, 2021, 6:32pm UTC](https://forum.flow.com/t/dictionnary-of-dictionnaries-issue-uint32-uint32-string/2181/2 "2021-07-19T18:32:01Z")

</div>

To solve this problem, I think Cadence should update its fundamental structures.  
All the problem start from here: _the values in dictionaries are all optionals._

Optional is a really powerful concept in Cadence, but it would be nice to have a new grammar something like `!!` which enables cascade unwrapping!

---

<div class="post-metadata">

**Author:** ![bluesign](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/bluesign/32/1359_2.png) [@bluesign](https://forum.flow.com/u/bluesign)\
**Post date:** [July 20, 2021, 4:37pm UTC](https://forum.flow.com/t/dictionnary-of-dictionnaries-issue-uint32-uint32-string/2181/3 "2021-07-20T16:37:52Z")

</div>

`let test2 = (dictionnary[0]!)[2]!` is the way, but the problem is this looks ugly if you code ugly. Normally let’s imagine:

```
let metadatas:{UInt32:{UInt32:String}} = {}
var packID = 0 
var NAME = 1
var DESCRIPTION = 2 
metadatas[itemID] = {NAME : "Some Name", DESCRIPTION : "Some Description"}

```

it turns out:

```
fun getProperty(itemID, property): String{
    var metadata = metadatas[itemID] ?? panic("Item not found)
    var value = metadata[property] ?? panic("Item does not have the property)
    return value
}

```

I mean in my opinion, in most cases if you need cascade unwrapping you are writing ugly code.

---

<div class="post-metadata">

**Author:** ![Creator](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/creator/32/580_2.png) [@Creator](https://forum.flow.com/u/Creator)\
**Post date:** [July 21, 2021, 7:51am UTC](https://forum.flow.com/t/dictionnary-of-dictionnaries-issue-uint32-uint32-string/2181/4 "2021-07-21T07:51:35Z")

</div>

Thank you! But I don’t know why your are calling ugly coding the need for using Dictionnary of Dictionnaries. This use is not a question of coding, but just a question of functional requirements … We are not all coding only for the simple metadata / PinataParty examples. Data structures representing Trees are more than basics in many fields. And very efficient for saving ressources and storage compared to linear collections.

---

<div class="post-metadata">

**Author:** ![bluesign](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/bluesign/32/1359_2.png) [@bluesign](https://forum.flow.com/u/bluesign)\
**Post date:** [July 21, 2021, 9:58am UTC](https://forum.flow.com/t/dictionnary-of-dictionnaries-issue-uint32-uint32-string/2181/5 "2021-07-21T09:58:01Z")

</div>

I mean it is not ugly to use Dictionary of Dictionaries. even you can use Dictionary of Dictionary of Dictionaries.

Problem is accessing them:

`dictionary[0][2][1]` etc part is ugly.

I compared:

```
fun getProperty(itemID, property): String{
    var metadata = metadatas[itemID] ?? panic("Item not found)
    var value = metadata[property] ?? panic("Item does not have the property)
    return value
} 

```

ve

```
fun getProperty(itemID, property): String{
    return (metadatas[itemID]!)[property]!
} 

```

cadence runs like Exception based programming (with panics)

It is good practice to catch your exceptions and behave accordingly.

Instead of just throwing them.

---

<div class="post-metadata">

**Author:** ![Creator](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/creator/32/580_2.png) [@Creator](https://forum.flow.com/u/Creator)\
**Post date:** [July 21, 2021, 2:22pm UTC](https://forum.flow.com/t/dictionnary-of-dictionnaries-issue-uint32-uint32-string/2181/6 "2021-07-21T14:22:18Z")

</div>

Oh ok I do understand but you have here a paradox:

Here you don’t know about what will cause the exceptions but the code is very very efficient:

```
fun getProperty(itemID, property): String{
    return (metadatas[itemID]!)[property]!
} 

```

Here you know how to track the exception but your code needs to copy in memory all the content of metadatas[itemID], with your line here

```
var metadata = metadatas[itemID] ?? panic("Item not found)

```

if metadatas[itemID] contains a lot of entries, and yous metadatas a lot of Items it is a nightmare to execute, you can try, you will see a huge difference in the execution duration.

May be the best solution to let the operator ?? cascading through sub Dictionnaries

`var metadata = metadatas[itemID][property] ?? panic("Item not found)`

---

<div class="post-metadata">

**Author:** ![bluesign](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/bluesign/32/1359_2.png) [@bluesign](https://forum.flow.com/u/bluesign)\
**Post date:** [July 21, 2021, 3:51pm UTC](https://forum.flow.com/t/dictionnary-of-dictionnaries-issue-uint32-uint32-string/2181/7 "2021-07-21T15:51:19Z")

</div>

Actually, nothing is copied, unless you change [1]. Execution performance should be the same.

[1] [Copy-on-write - Wikipedia](https://en.wikipedia.org/wiki/Copy-on-write)
