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
Yes, using literal numbers like that is poor coding practice!
You can use the type! constructor to return the type you want to use. '194' is the type 'List of Dictionary' and can be defined like this:
'type!{http://www.appian.com/ae/types/2009}Dictionary?list'
Note: the Dictionary type is 'type!{http://www.appian.com/ae/types/2009}Dictionary'. By adding the '?list' you get the list version of tat type. This pattern applies across all types.
Stewart Burchell said:Note: the Dictionary type is 'type!{http://www.appian.com/ae/types/2009}Dictionary'. By adding the '?list' you get the list version of tat type. This pattern applies across all types.
It also bears pointing out that the Appian type! domain will try to autofill the type you're picking, which is fine, except when it does autofill, it'll include the "()" at the end as if you were going to use it as a type constructor. The parentheses will need to be removed if you want to use the type directly within the cast() call, with or without the "?list" suffix. This trick eluded me for years and caused lots of unnecessary headaches.
Mike Schmitt Thanks for mentioning this! Yes, the type! constructor can be used to either create an instance of a type - where you would include the () at the end of the type, or a reference to a type (i.e. a meaningful text-based version of the 194 in the original post!)
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:
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", } } )