The Windows Runtime PassArray is a read-only array, even though it isn’t declared
As I noted some time ago, the Windows Runtime PassArray pattern passes a read-only non-owning counted array which is nevertheless not declared as
Indeed, if you try to force the array type to be
const in your IDL declaration:
HRESULT SetData([in] UINT32 dataSize, [in, size_is(dataSize)] const INT32* data);
const is ignored, and the resulting metadata declares the parameter as non-
There are a few reasons for this, partly intentional, and partly a technicality.
The technicality is that the
const attribute is lost because Windows Runtime methods are described by metadata that physically takes the form of an ECMA-335 assembly (though restricted to a very limited subset of full ECMA-335), and ECMA-335 does not have
const. Therefore Windows Runtime metadata cannot have
Mind you, this is an unsatisfying explanation since it’s semi-circular. Windows Runtime metadata doesn’t have
const because the designers chose a format that doesn’t support
const, and it’s okay to have chosen a format that doesn’t support
const because Windows Runtime metadata doesn’t use
But really, if they really wanted
const, then they would have chosen some other file format that does support
The Windows Runtime does not have
const because the concept cannot be expressed in most programming languages,¹ 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.
¹ Indeed, it’s really only C, C++ and now Rust that have such a concept. The C++ projections do represent the array as
const: It is a
const Platform:: in C++/CX, and it is a
winrt:: in C++/WinRT. Similarly, the Rust projection represents the array as an immutable reference.
D, Delphi and Swift also have such concept.
And if we stray from mainstream path, languages like ML family, Active Oberon and Modula-3 also do have such concept.
Swift’s idea of const is as I understand it pretty much the same as const in c# or java but not the same concept as c++ has.
I assume the same is true for the other languages listed, at least I don’t remember const correctness ever being an issue when we learned Delphi in highschool (boy I’m getting old)
Swift is immutable by default, so actually is the same const as ML languages, which you also find in Rust.
If you wanted to be able to change data, then it would be
Delphi in 2021 is a bit different than those highschool days, similarly with the proper type alias it would be,
I’m not sure about Delphi. In Delphi const means, that value of pointer cannot be changed, pointed value can be changed. So Delphi
would be more like C
In Delphi it is more like,
In pure Delphi that dataSize would be superfluous.
And you cannot use open arrays on API functions that you want to be exported for use in another languages or when importing from C (as far as i know).
Of course it would be superfluous, the whole point of writing like that was to match the example being described, as per COM ABI.
Now, if there was a Delphi Projection generator for WinRT types, then eventually it might be smart enough to get rid of dataSize, which isn’t anyway the point I was making.
Everything that Delphi can is accessible to C++, via C++ Builder as VCL is mostly written in Delphi, including the new COM wrappers,
I’m not sure what Raymond meant by the concept, because const is in some ways extremely specific. But many languages that borrow from Fortran and Fortran itself have
argument (what C calls parameters) declarations. Those have mostly the same implications as const for non-nested types and are a bit more strict for stuff containing pointers. While C-derived languages don’t mind modifications to pointers passed as members of const declared structs, Fortran would disallow that.
It would be interesting to know for which language interoperability they were shooting for back then.
The ECMA-335 metadata format can actually express “const” via its concept of “custom modifiers”.
Quoting from ECMA-335, Partition II, 7.1.1: “For example, the const qualifier in the C programming language can be modelled with an optional modifier […].”
Indeed. This is heavily used by C++/CLI assemblies, where you can easily find modreq(System.Runtime.CompilerServices.IsConst).