{"componentChunkName":"component---src-lekoarts-gatsby-theme-minimal-blog-core-templates-post-query-tsx","path":"/binding-closure","result":{"data":{"post":{"slug":"/binding-closure","title":"Binding & Closure","date":"January 23, 2019","tags":[{"name":"Functional Programming","slug":"functional-programming"}],"description":null,"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Binding & Closure\",\n  \"date\": \"2019-01-23T00:00:00.000Z\",\n  \"tags\": [\"Functional Programming\"],\n  \"excerpt\": \"Binding != Assignment\"\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h2\", null, \"Binding & Closure\"), mdx(\"p\", null, mdx(\"em\", {\n    parentName: \"p\"\n  }, \"Binding\"), \" created a \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"(name, value)\"), \" pair. Here, values can also be functions.\"), mdx(\"p\", null, mdx(\"em\", {\n    parentName: \"p\"\n  }, \"Declarations\"), \" create bindings.\"), mdx(\"pre\", null, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {\n    \"className\": \"language-ocaml\"\n  }), \"let x = 3 in x+2 ;;\\n\")), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"let\"), \": declaration\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"in\"), \": scope\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"strong\", {\n    parentName: \"li\"\n  }, \"NOTE\"), \": Do not confuse this with assignment.\")), mdx(\"h3\", null, \"3 components of closure\"), mdx(\"ol\", null, mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"parameters\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"body\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"pointer to the environment when it is defined (\", mdx(\"strong\", {\n    parentName: \"li\"\n  }, \"static binding\"), \", \", mdx(\"strong\", {\n    parentName: \"li\"\n  }, \"lexical scoping\"), \")\")), mdx(\"p\", null, \"NOTE: Environment can only be added, so there are layers. The pointer is pointing to the correct environment. A layer will be swept by garbage collection when there is no pointer points to it.\"), mdx(\"h4\", null, mdx(\"a\", _extends({\n    parentName: \"h4\"\n  }, {\n    \"href\": \"https://beginnersbook.com/2013/04/java-static-dynamic-binding/\"\n  }), \"Side note\"), \": static binding and dynamic binding in Java:\"), mdx(\"blockquote\", null, mdx(\"ul\", {\n    parentName: \"blockquote\"\n  }, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"p\", {\n    parentName: \"li\"\n  }, \"Static binding\"), mdx(\"p\", {\n    parentName: \"li\"\n  }, \"\\u2026So whenever a binding of static, private and final methods happen, \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"type of the class\"), \" is determined by the compiler at \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"compile time\"), \" (\", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"not\"), \" determined by type of the object) and the binding happens then and there.\")), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"p\", {\n    parentName: \"li\"\n  }, \"Dynamic binding\"), mdx(\"p\", {\n    parentName: \"li\"\n  }, \"The \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"type of the object\"), \" is determined at the runtime(by the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"new someconstructor()\"), \" keyword).\")), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"p\", {\n    parentName: \"li\"\n  }, \"Overload and Override\"), mdx(\"p\", {\n    parentName: \"li\"\n  }, \"The binding of \", mdx(\"a\", _extends({\n    parentName: \"p\"\n  }, {\n    \"href\": \"https://beginnersbook.com/2013/05/method-overloading/\"\n  }), \"overloaded methods\"), \" is static and the binding of overridden methods is dynamic.\")))), mdx(\"hr\", null), mdx(\"h3\", null, \"Binding vs. Assignment\"), mdx(\"p\", null, \"In an imperative language, like Java, when we assign a value to a variable, we make an analogy between our \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"variable\"), \" and \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"box\"), \". A variable itself is a container, and it holds some value. However, when we \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"bind\"), \" a name to a variable, we are attaching a \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"label\"), \" on that \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"value\"), \".\"), mdx(\"p\", null, \"Consider the code snippet below.\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"y = x + 1\"), \" is not closed on the name \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x\"), \". It is \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"closed\"), \" on the value \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"3\"), \" which was bound to \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x\"), \". So \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"y\"), \" will never change with \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x\"), \".\"), mdx(\"p\", null, \"A really good explanation from \", mdx(\"a\", _extends({\n    parentName: \"p\"\n  }, {\n    \"href\": \"https://stackoverflow.com/questions/48100145/binding-vs-assignment\"\n  }), \"stack overflow\"), \":\"), mdx(\"blockquote\", null, mdx(\"p\", {\n    parentName: \"blockquote\"\n  }, \"You can think of binding as a label on a suitcase, and assignment as a suitcase.\")), mdx(\"p\", null, \"Now, let\\u2019s look at this example:\"), mdx(\"pre\", null, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {\n    \"className\": \"language-ocaml\"\n  }), \"let x = 3 in\\n    let f = fun x -> x in\\n    let x = 99 in\\n    f x\\n\")), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"Layers\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x | 99\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"f | (3 components)\"), \" parameter: x | body: x | pointer to \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x = 3\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x | 3\")), mdx(\"p\", null, \"When we evaluate \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"f x\"), \":\"), mdx(\"ol\", null, mdx(\"li\", {\n    parentName: \"ol\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"f\"), \" is a closure, should be a function\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Now its parameter is x! Let\\u2019s look for \", mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"x\")), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Ok, \", mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"x\"), \" is 99. Let\\u2019s evaluate. At this point, the layer \", mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"x | 99\"), \" will go along with the pointer to the layer \", mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"x | 3\"), \". \", mdx(\"strong\", {\n    parentName: \"li\"\n  }, \"IMPORTANT: 3 is masked because 99 is on the top layer.\"))), mdx(\"pre\", null, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {\n    \"className\": \"language-ocaml\"\n  }), \"let x = 3 in (* binding *)\\n    let y = x + 1 in\\n    let x = 2 in\\n    x + y\\n\")), mdx(\"p\", null, \"When you evaluate this, \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x\"), \" is not built-in. It is stored in a structure called \", mdx(\"u\", null, \"environment\"), \". So the compiler will look for what is \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x\"), \".\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x | 3\"), \" ---> base environment\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x + 2\"), \" to be evaluated. \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x\"), \" is found in the top layer.\"), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"Layers\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x | 2\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"y | 4\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x | 3\")), mdx(\"p\", null, \"|||BASE ENV|||\"), mdx(\"p\", null, mdx(\"strong\", {\n    parentName: \"p\"\n  }, mdx(\"u\", null, \"NOTE\")), \": Never put \", mdx(\"u\", null, \"bindings\"), \" in the value part. It looks for \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x\"), \" in the environment, and put the value of \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x\"), \" in \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x+1\"), \".\"), mdx(\"p\", null, \"Here, \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x-3\"), \" is \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"masked, but it is still there\"), \". During the evaluation, it picks the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x\"), \" in the top layer.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Binding != Assignment","banner":null}},"pageContext":{"slug":"/binding-closure"}}}