8. Language interactions
8. Language interactions
Section titled “8. Language interactions”Canonical source:
protos/tutorials/08-language-interactions/at locked revisione99b712ecf658a527b1fe82c8beba2c1286d8dc9.
These are the executable tutorial sources from the exact Protos revision consumed by this website. The fenced program bodies are copied without modification. The tutorials are explanatory and non-normative; the applicable specification remains authoritative.
01 — Receiver and capture
Section titled “01 — Receiver and capture”Canonical source protos/tutorials/08-language-interactions/01-receiver-and-capture.protos
/* * THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE: https://github.com/guillermomolina/protos * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. */
// An object body is not a lexical capture scope. The inherited method's bare// `value` therefore resolves against its dynamic receiver after lexical lookup.prototype: { value: 1
makeReader: () => { () => { value } }}
object: prototype { value: 10}
reader: object.makeReader()object.value = 20
// The nested closure escaped from the method but retained that receiver.print(reader())02 — Callability through delegation
Section titled “02 — Callability through delegation”Canonical source protos/tutorials/08-language-interactions/02-callability-through-delegation.protos
/* * THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE: https://github.com/guillermomolina/protos * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. */
// Parenthesized invocation looks up the ordinary `call` slot through// delegation. The original invocation receiver is preserved as `this`.callable: { value: 2 call: (factor) => { value * factor }}
specialized: callable { value: 6}
print(specialized(7))03 — Equality and identity keys
Section titled “03 — Equality and identity keys”Canonical source protos/tutorials/08-language-interactions/03-equality-and-identity-keys.protos
/* * THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE * ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC * DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF * DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE * OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF * THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY * OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING * THE CONTENTS OF THIS FILE. */
// Structural equality and individual identity compose differently with maps.left: Path.relative().child("docs")right: Path.relative().child("docs")print(left == right)print(left === right)byValue: Map()byValue[left] = 1byValue[right] = 2print(byValue.size())byIdentity: IdentityMap()byIdentity[left] = 1byIdentity[right] = 2print(byIdentity.size())