Casting to List of Dictionary without Hardcoding the Type Number

Hey All,

It seems like a common way to cast a value to the type "List of Dictionary" is via:

cast(194, local!thingBeingCast)

Does anyone know of a way to do this without hardcoding the "194" value? This approach doesn't seem very explicit (I personally have to remind myself of what it's doing in a blank rule whenever I see it), and god-forbid that type id changes one day and needs to be changed one-by-one throughout the codebase.

Thanks!

  Discussion posts and replies are publicly visible

Parents
  • The other responses are great, but I'll also add in a shameless plug to use a map instead of a dictionary if at all possible. A map provides much the same functionality as a dictionary by allowing you to use custom key value pairs, but it also has additional benefits including:

    • If you try to use dot notation to index into a field that doesn't exist, a dictionary errors, a map just returns null
    • If you index into a value that has a type (eg an integer), the map maintains the type, while a dictionary makes it a Variant

    You can always cast to a map like this:

    cast(
      a!listType(type!Map),
      {
        {id: 1,  desc: "blah blah", },
        {id: 2 , desc: "blah blah blah", }
      }
    )

Reply
  • The other responses are great, but I'll also add in a shameless plug to use a map instead of a dictionary if at all possible. A map provides much the same functionality as a dictionary by allowing you to use custom key value pairs, but it also has additional benefits including:

    • If you try to use dot notation to index into a field that doesn't exist, a dictionary errors, a map just returns null
    • If you index into a value that has a type (eg an integer), the map maintains the type, while a dictionary makes it a Variant

    You can always cast to a map like this:

    cast(
      a!listType(type!Map),
      {
        {id: 1,  desc: "blah blah", },
        {id: 2 , desc: "blah blah blah", }
      }
    )

Children
No Data