Search code examples
python-2.7dropbox-api

What is a Dropbox API (v2) Union Tag?


While starting to develop a Dropbox app using the Python SDK, I'm tripping conceptually over what the AccessLevel documentation calls a union tag. (The concept extends beyond the AccessLevel class, but this seems like as good an example as any.)

I'm trying to understand why the _tag attribute basically appears to be intended for internal use only. Why, if I want to know if a user has editor, owner, or viewer permission—-and it can only be one of those--I seem to be supposed to call the is_owner(), is_editor(), and is_viewer() methods until I get a True response.

What am I missing? Why wouldn't it be a good idea to just access the _tag attribute and go my merry way?


Solution

  • API docs define .tag as:

    The .tag field in an object identifies the subtype of a struct or selected member of a union.

    Tagged union appears to be a generic concept rather than Dropbox-specific one:

    In computer science, a tagged union ... is a data structure used to hold a value that could take on several different, but fixed, types

    Wikipedia clarifies this applicability further:

    Only one of the types can be in use at any one time, and a tag field explicitly indicates which one is in use. This is critical in defining recursive datatypes ... where it is necessary to distinguish multi-node subtrees and leaves.

    A good example in case of Dropbox API endpoint is list_folder where return can be either file or folder, i.e. .tag can take value of file or folder.

    I believe this can be valuable for strongly typed code where returned files/folders are parsed into classes.