{"id":105606,"date":"2021-08-25T07:00:00","date_gmt":"2021-08-25T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=105606"},"modified":"2021-08-25T06:45:46","modified_gmt":"2021-08-25T13:45:46","slug":"20210825-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210825-00\/?p=105606","title":{"rendered":"The Windows Runtime PassArray is a read-only array, even though it isn&#8217;t declared <CODE>const<\/CODE>"},"content":{"rendered":"<p>As I noted some time ago, <a title=\"The various patterns for passing C-style arrays across the Windows Runtime ABI boundary\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200205-00\/?p=103398\"> the Windows Runtime PassArray pattern passes a read-only non-owning counted array which is nevertheless not declared as <code>const<\/code><\/a>.<\/p>\n<p>Indeed, if you try to force the array type to be <code>const<\/code> in your IDL declaration:<\/p>\n<pre>HRESULT SetData([in] UINT32 dataSize, [in, size_is(dataSize)] <u>const<\/u> INT32* data);\r\n<\/pre>\n<p>The <code>const<\/code> is ignored, and the resulting metadata declares the parameter as non-<code>const<\/code>.<\/p>\n<p>There are a few reasons for this, partly intentional, and partly a technicality.<\/p>\n<p>The technicality is that the <code>const<\/code> attribute is lost because Windows Runtime methods are described by metadata that <a href=\"https:\/\/github.com\/microsoft\/xlang\/blob\/master\/docs\/xplatwinrt\/XSPEC02%20-%20Metadata%20Representation.md\"> physically takes the form of an ECMA-335 assembly<\/a> (though restricted to a very limited subset of full ECMA-335), and ECMA-335 does not have <code>const<\/code>. Therefore Windows Runtime metadata cannot have <code>const<\/code>.<\/p>\n<p>Mind you, this is an unsatisfying explanation since it&#8217;s <a title=\"Unicode Character 'UPPER HALF CIRCLE' (U+25E0)\" href=\"http:\/\/www.fileformat.info\/info\/unicode\/char\/25e0\/index.htm\"> semi-circular<\/a>. Windows Runtime metadata doesn&#8217;t have <code>const<\/code> because the designers chose a format that doesn&#8217;t support <code>const<\/code>, and it&#8217;s okay to have chosen a format that doesn&#8217;t support <code>const<\/code> because Windows Runtime metadata doesn&#8217;t use <code>const<\/code>.<\/p>\n<p>But really, if they really wanted <code>const<\/code>, then they would have chosen some other file format that <i>does<\/i> support <code>const<\/code>.<\/p>\n<p>The Windows Runtime does not have <code>const<\/code> because the concept cannot be expressed in most programming languages,\u00b9 and the Windows Runtime intends to be language-independent. Limiting the feature set of the Windows Runtime type system makes it more likely that it can be consumed by a broad range of programming languages.<\/p>\n<p>\u00b9 Indeed, it&#8217;s really only C, C++ and now Rust that have such a concept. The C++ projections do represent the array as <code>const<\/code>: It is a <code>const Platform::<wbr \/>Array<\/code> in C++\/CX, and it is a <code>winrt::<wbr \/>array_view&lt;T const&gt;<\/code> in C++\/WinRT. Similarly, the Rust projection represents the array as an immutable reference.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because in most languages, there is no such thing as const.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-105606","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Because in most languages, there is no such thing as const.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105606","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=105606"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105606\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=105606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=105606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=105606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}