# May existential containers contain existential containers

Hello fellow Swift enthusiasts,

It is my understanding that if you type a variable with a protocol, then it will be emitted as an existential container. For instance, I would assume `x` to be an existential container in the following code:

``````protocol A {}
struct S: A {}
let x: A = S()
``````

Assuming my understanding is correct, I was wondering what happens when one assigns an existential container to another existential container. In particular, I wonder if this would result in an existential containing the original container, or if it is reasonable to assume that there can never be nested levels of existential containers.

In other words, is `y` an existential that contains an instance of `S`, or an existential that contains an existential containing an instance of `S` in the following code:

``````protocol A {}
protocol B: A {}
struct S: B {}
let x: B = S()
let y: A = x
``````

As a subsequent question, I would like to know how I could have answer that by myself. Could this be understood by looking at SIL, or should I go deeper and look at LLVM IR?

It is possible to stack up existential containers, say by repeatedly calling this function:

``````func wrap<T>(x: T) -> Any { return x }

return wrap(wrap(wrap(1)))
``````

because `Any` is a valid binding for `T`, and the conversion from `Any` wraps the value. The runtime generally deals with this wrapping automatically, since things like dynamic casts will automatically look through multiple levels of existential container.

5 Likes
{"ft_lang_label":"__label__en","ft_lang_prob":0.92284685,"math_prob":0.8403357,"size":1035,"snap":"2020-34-2020-40","text_gpt3_token_len":221,"char_repetition_ratio":0.19398642,"word_repetition_ratio":0.04278075,"special_character_ratio":0.22222222,"punctuation_ratio":0.11219512,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98168063,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-22T08:43:46Z\",\"WARC-Record-ID\":\"<urn:uuid:f3e2f045-b1d6-40b8-a5e5-1cbe86ec00d2>\",\"Content-Length\":\"14860\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e507278a-729e-4d0f-9bfc-dd43a0ccd581>\",\"WARC-Concurrent-To\":\"<urn:uuid:a1a8d2b4-d487-494b-822d-44c00ba645a7>\",\"WARC-IP-Address\":\"216.218.159.25\",\"WARC-Target-URI\":\"https://forums.swift.org/t/may-existential-containers-contain-existential-containers/30835\",\"WARC-Payload-Digest\":\"sha1:AT6TMLHD4OLY4Q6UFFC2I4BOF43N3PM5\",\"WARC-Block-Digest\":\"sha1:ACBVNJK3CIHH5U6HRDNUAPEM672YL6JT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400204410.37_warc_CC-MAIN-20200922063158-20200922093158-00063.warc.gz\"}"}