{
    "componentChunkName": "component---src-components-layout-mdx-layout-tsx",
    "path": "/language/beginner/objects",
    "result": {"data":{"site":{"siteMetadata":{"title":"Toit documentation"}},"mdx":{"body":"const _excluded = [\"components\"];\n\nfunction _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }\n\nfunction _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }\n\nfunction _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nconst _frontmatter = {};\nconst layoutProps = {\n  _frontmatter\n};\nconst MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  let {\n    components\n  } = _ref,\n      props = _objectWithoutProperties(_ref, _excluded);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", {\n    \"id\": \"objects\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h1\",\n    \"href\": \"#objects\",\n    \"aria-label\": \"objects permalink\",\n    \"className\": \"table-of-contents-icon before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Objects\"), mdx(\"p\", null, \"This is lesson 3 of the \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/language/beginner\"\n  }, \"beginner tutorial\"), \". Start with\\n\", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/language/beginner/lists-and-functions\"\n  }, \"lists and functions\"), \" if you have not\\nyet written a function or used \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"do\"), \" to process a list.\"), mdx(\"p\", null, \"The previous lesson kept each room's name, readings, and warm limit separately.\\nWe will now group them, so we can pass a room's whole report around as one value.\"), mdx(\"h2\", {\n    \"id\": \"keep-related-values-together\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#keep-related-values-together\",\n    \"aria-label\": \"keep related values together permalink\",\n    \"className\": \"table-of-contents-icon before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Keep related values together\"), mdx(\"p\", null, \"An \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"object\"), \" can hold several named values. These values are called its \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"fields\"), \".\\nA \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"class\"), \" describes the fields its objects have and the operations they support.\\nOur class will be named \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"TemperatureReport\"), \". Class names use \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"UpperCamelCase\"), \":\\neach word starts with a capital letter. Variables and functions use \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"kebab-case\"), \",\\nas in \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"warm-limit\"), \" and \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"print-reading\"), \". We will introduce methods below; their names also use \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"kebab-case\"), \".\"), mdx(\"p\", null, \"The \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"constructor\"), \" runs when a new object is created. Here it receives three\\narguments and stores them in the new object's fields. The \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"?\"), \" in \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"room := ?\"), \"\\nmeans that the constructor must supply the field's initial value. It is not a\\nvalue stored in the field. We use the same notation for \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"readings\"), \" and \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"limit\"), \":\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-toit\"\n  }, \"class TemperatureReport:\\n  room := ?\\n  readings := ?\\n  limit := ?\\n\\n  constructor room readings limit:\\n    this.room = room\\n    this.readings = readings\\n    this.limit = limit\\n\\nmain:\\n  report := TemperatureReport \\\"Kitchen\\\" [21, 26] 25\\n  print report.room\\n  print report.readings\\n\")), mdx(\"p\", null, \"Expected output:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-text\"\n  }, \"Kitchen\\n[21, 26]\\n\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"TemperatureReport \\\"Kitchen\\\" [21, 26] 25\"), \" creates an object and runs its\\nconstructor with those three values. \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"this\"), \" refers to the object being created:\\nin \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"this.room = room\"), \", the left side is its field and the right side is the\\nconstructor's argument. \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"report.room\"), \" reads that object's \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"room\"), \" field.\"), mdx(\"p\", null, \"Change the room name or the readings and run again. Then create a second report\\nand print both room names. Each object keeps its own field values.\"), mdx(\"h2\", {\n    \"id\": \"initialize-fields-with-constructor-parameters\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#initialize-fields-with-constructor-parameters\",\n    \"aria-label\": \"initialize fields with constructor parameters permalink\",\n    \"className\": \"table-of-contents-icon before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Initialize fields with constructor parameters\"), mdx(\"p\", null, \"Toit lets a constructor parameter initialize a field directly by prefixing its\\nname with a dot. This version stores the same three arguments:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-toit\"\n  }, \"class TemperatureReport:\\n  room := ?\\n  readings := ?\\n  limit := ?\\n\\n  constructor .room .readings .limit:\\n\\nmain:\\n  report := TemperatureReport \\\"Kitchen\\\" [21, 26] 25\\n  print report.room\\n\")), mdx(\"p\", null, \"The constructor body is empty because the field assignments are expressed in\\nits parameters. This is shorthand for the explicit assignments used above.\"), mdx(\"h2\", {\n    \"id\": \"put-an-operation-on-the-object\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#put-an-operation-on-the-object\",\n    \"aria-label\": \"put an operation on the object permalink\",\n    \"className\": \"table-of-contents-icon before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Put an operation on the object\"), mdx(\"p\", null, \"A function inside a class is called a \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"method\"), \". It can use the object's fields,\\nso callers do not need to pass the room, readings, and limit each time.\"), mdx(\"p\", null, \"The \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"print-report\"), \" method below uses what we learned in the previous lessons:\\nprinting a string, iterating over a list, and making a decision.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-toit\"\n  }, \"class TemperatureReport:\\n  room := ?\\n  readings := ?\\n  limit := ?\\n\\n  constructor .room .readings .limit:\\n\\n  print-report:\\n    print room\\n    readings.do: |temperature|\\n      if temperature >= limit:\\n        print \\\"$temperature C: Warm\\\"\\n      else:\\n        print \\\"$temperature C: Comfortable\\\"\\n\\nmain:\\n  kitchen := TemperatureReport \\\"Kitchen\\\" [21, 26] 25\\n  bedroom := TemperatureReport \\\"Bedroom\\\" [22, 24] 23\\n  kitchen.print-report\\n  bedroom.print-report\\n\")), mdx(\"p\", null, \"Expected output:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-text\"\n  }, \"Kitchen\\n21 C: Comfortable\\n26 C: Warm\\nBedroom\\n22 C: Comfortable\\n24 C: Warm\\n\")), mdx(\"p\", null, \"Calling \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"kitchen.print-report\"), \" uses the kitchen object's fields. Calling\\n\", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"bedroom.print-report\"), \" uses the bedroom object's fields. The method takes no\\narguments, so its name is all we need to call it.\"), mdx(\"h2\", {\n    \"id\": \"handle-a-room-without-readings\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#handle-a-room-without-readings\",\n    \"aria-label\": \"handle a room without readings permalink\",\n    \"className\": \"table-of-contents-icon before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Handle a room without readings\"), mdx(\"p\", null, \"An empty list currently produces only the room heading. We can make the report\\nclearer by printing \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"No readings\"), \" when there are none.\"), mdx(\"p\", null, \"The list's \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"size\"), \" gives the number of values it contains. \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"readings.size == 0\"), \"\\nasks whether that number is zero. Use \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"==\"), \" to compare values; a single \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"=\"), \" is\\nan assignment, as in the first constructor we wrote.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-toit\"\n  }, \"class TemperatureReport:\\n  room := ?\\n  readings := ?\\n  limit := ?\\n\\n  constructor .room .readings .limit:\\n\\n  print-report:\\n    print room\\n    if readings.size == 0:\\n      print \\\"No readings\\\"\\n      return\\n    readings.do: |temperature|\\n      if temperature >= limit:\\n        print \\\"$temperature C: Warm\\\"\\n      else:\\n        print \\\"$temperature C: Comfortable\\\"\\n\\nmain:\\n  kitchen := TemperatureReport \\\"Kitchen\\\" [21, 26] 25\\n  bedroom := TemperatureReport \\\"Bedroom\\\" [] 23\\n  kitchen.print-report\\n  bedroom.print-report\\n\")), mdx(\"p\", null, \"This prints the kitchen report, then:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-text\"\n  }, \"Bedroom\\nNo readings\\n\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"return\"), \" stops the method immediately and resumes execution after the call.\\nHere there is no reason to continue into the loop once we know the list is empty.\\nThe kitchen report still reaches the loop because its list is not empty.\"), mdx(\"h2\", {\n    \"id\": \"practice\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#practice\",\n    \"aria-label\": \"practice permalink\",\n    \"className\": \"table-of-contents-icon before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Practice\"), mdx(\"p\", null, \"Add readings to the bedroom report and check that \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"No readings\"), \" disappears.\\nThen create a third report for another room. You should only need to add its\\ncreation and method call under \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"main\"), \"; the class can stay the same.\"), mdx(\"h2\", {\n    \"id\": \"continue-when-you-need-more\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#continue-when-you-need-more\",\n    \"aria-label\": \"continue when you need more permalink\",\n    \"className\": \"table-of-contents-icon before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Continue when you need more\"), mdx(\"p\", null, \"You can now build a program using variables, decisions, lists, functions,\\nand objects. You do not need to learn every feature before using these tools.\"), mdx(\"p\", null, \"The \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/language/toitversus\"\n  }, \"programmer's tour\"), \" introduces function results, type\\nannotations, collection transformations, error handling, imports, and tasks.\\nIt assumes the concepts you have now practiced, but moves more quickly.\\nUse the \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/language/reference\"\n  }, \"language reference\"), \" to look up an individual\\nfeature. For hardware, continue with \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/getstarted/device\"\n  }, \"Run on your device\"), \"\\nand the \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"/tutorials/hardware/led\"\n  }, \"first LED tutorial\"), \".\"));\n}\n;\nMDXContent.isMDXComponent = true;","tableOfContents":{"items":[{"url":"#objects","title":"Objects","items":[{"url":"#keep-related-values-together","title":"Keep related values together"},{"url":"#initialize-fields-with-constructor-parameters","title":"Initialize fields with constructor parameters"},{"url":"#put-an-operation-on-the-object","title":"Put an operation on the object"},{"url":"#handle-a-room-without-readings","title":"Handle a room without readings"},{"url":"#practice","title":"Practice"},{"url":"#continue-when-you-need-more","title":"Continue when you need more"}]}]}}},"pageContext":{"title":"Objects","id":"c0c110f1-1252-5119-af9b-32aeee65ca81"}},
    "staticQueryHashes": ["2197327828"]}