{"id":107448,"date":"2022-11-24T07:00:00","date_gmt":"2022-11-24T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=107448"},"modified":"2022-11-24T06:21:37","modified_gmt":"2022-11-24T14:21:37","slug":"20221124-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20221124-00\/?p=107448","title":{"rendered":"How does JavaScript represent output parameters in the Windows Runtime?"},"content":{"rendered":"<p>The Windows Runtime allows parameters to be declared as <code>out<\/code>, which means that the variable is passed by reference and will be written to, but not read from, by the method. (At the ABI layer, the variable is passed by address.)<\/p>\n<pre>runtimeclass MyClass\r\n{\r\n    Boolean TryGetCount(out Int32 count);\r\n}\r\n<\/pre>\n<p>Many languages support passing variables by reference, and the projection aligns with those language features.<\/p>\n<pre>\/\/ C#\r\nint count;\r\nif (c.TryGetCount(out count)) ...\r\n\r\n\/\/ C# 7.0\r\nif (c.TryGetCount(out int count)) ...\r\n\r\n\/\/ Visual Basic\r\nDim count as Integer\r\nIf c.TryGetCount(count) Then\r\n    ...\r\nEnd If\r\n\r\n\/\/ C++\/WinRT\r\nint count;\r\nif (c.TryGetCount(count)) ...\r\n\r\n\/\/ Rust\/WinRT\r\nlet mut count = 0;\r\nif (c.TryGetCount(&amp;mut count)) ...\r\n<\/pre>\n<p>JavaScript, on the other hand, does not support passing variables by reference. To work around this, any method that has an <code>out<\/code> parameter is rewritten by returning a JavaScript object with a property called <code>result<\/code> which contains the original return value, and with additional properties, one for each <code>out<\/code> parameter. The original <code>out<\/code> parameters disappear from the formal parameter list.<\/p>\n<pre>var retVal = c.tryGetCount();\r\nif (retVal.result) {\r\n    console.log(retVal.count);\r\n}\r\n<\/pre>\n<p>The return value of the original <code>TryGetCount<\/code> method is recorded as the <code>result<\/code> property, and the <code>count<\/code> that was returned by the original method becomes a <code>count<\/code> property.<\/p>\n<p>The name of the formal parameter is not just a documentation nicety in JavaScript. The name of the formal parameter is part of the programming interface because it becomes the name of the property!\u00b9<\/p>\n<p>Python also doesn&#8217;t support passing variables by reference. It performs a transform similar to JavaScript, but instead of returning an Object, it returns a tuple.<\/p>\n<pre>\/\/ Python\/WinRT\r\nresult, count = c.try_get_count();\r\n<\/pre>\n<p>This awkwardness with output parameters in JavaScript and Python makes output parameters slightly less attractive in the Windows Runtime.<\/p>\n<p>\u00b9 In the Windows Runtime, parameter names are considered part of the interface, and changing parameter names is a breaking change. We saw how JavaScript can be affected by changing the name of a formal parameter. The name is also programmatically significant in C#, since C# lets you pass parameters by name, which is particularly handy for parameters of numeric or Boolean type.<\/p>\n<pre>var trigger = new TimeTrigger(freshnessTime: 60, oneShot: true);\r\n\r\nvar inkPoint = new InkPoint(position,\r\n                    pressure: 0.5, tiltX: 0.0, tiltY: 0.0, timestamp: 0);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It has to fake it.<\/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-107448","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It has to fake it.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107448","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=107448"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107448\/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=107448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=107448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=107448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}