{"id":4900,"date":"2024-06-08T13:52:31","date_gmt":"2024-06-08T11:52:31","guid":{"rendered":"https:\/\/oopm.org\/?page_id=4900"},"modified":"2024-12-02T16:22:50","modified_gmt":"2024-12-02T15:22:50","slug":"8-1-new-submethods","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=4900","title":{"rendered":"8.2 Submethods"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages4900&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages4900&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">In this section, we introduce\u00a0<em>submethods<\/em>, which makes it possible to organise methods in a hierarchy similar to a class hierarchy using subclasses. As for subclass and superclass, submethod and supermethod are dual terms. A submethod is a method that has another method as a supermetod, and thus a supermethod is a method being used to define submethods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One primary use of methods as supermethods is to define new control abstractions, which may be used as control structures like if-then, etc. In chapter <script>mkRef(\"Control structure methods\")<\/script>, we show examples of how to define such control abstractions, and chapter&nbsp;<script>mkRef(\"Building concurrency abstractions\")<\/script> have further examples.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In section <script>mkRef(\"Composite values\")<\/script> we introduced class <code>Transaction<\/code> and augmented <code>deposit<\/code> and <code>withdraw<\/code> to record each deposit and withdraw on a given <code>Account<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>deposit(amount: <strong>var<\/strong> float):\n   balance := balance + amount\n   transactions.insert(\n      Transaction(\"deposit\", clock.today, clock.now, amount))\nwithdraw(amount: <strong>var<\/strong> float) -&gt; newB: <strong>var<\/strong> float:\n   balance:= balance - amount\n   transactions.insert(\n      Transaction(\"withdraw\", clock.today, clock.now, amount))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">They both invoke <code>transactions.insert<\/code> with almost identical arguments. Both <code>deposit<\/code> and <code>withdraw<\/code> perform transactions on the <code>Account<\/code> and we would therefore like to reflect this in the model of the bank account. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To do this, we introduce a common supermethod <code>transact<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>transact(amount: <strong>var<\/strong> float):\n   theTransaction: <strong>ref<\/strong> Transaction\n   theTransaction := Transaction(\"\", clock.today, clock.now, amount))\n   inner(transact)\n   transactions.insert(theTransaction)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method <code>transact<\/code> takes care of recording the <code>transactions<\/code> in the <code>transactions<\/code> object. It generates a <code>Transaction<\/code> object and assigns it to the reference variable <code>theTransaction<\/code>. Then it executes an <code>inner(transact)<\/code>, which has the effect that possible statements in submethods of <code>transact<\/code> are executed. We explain <code>inner<\/code> in section <script>mkRef(\"The inner statement\")<\/script> below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may now rewrite <code>deposit<\/code> and <code>withdraw<\/code> to become submethods of <code>transact<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>deposit: transact\n   balance := balance + amount\n   theTransaction.what := \u201cdeposit\u201d\nwithdraw: transact\n   balance := balance \u2013 amount\n   theTransaction.what := \u201cwithdraw\u201d<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By writing <code>transact<\/code> after <code>deposit:<\/code> and <code>withdraw:<\/code> both are defined as submethods of <code>transact<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is the same mechanism as for classes and subclasses; properties of <code>transact<\/code> becomes properties of both <code>deposit<\/code> and <code>withdraw<\/code>, including both data-items and statements. <\/p>\n\n\n\n<div class=\"wp-block-media-text has-media-on-the-right is-stacked-on-mobile\" style=\"grid-template-columns:auto 29%\"><div class=\"wp-block-media-text__content\">\n<p class=\"wp-block-paragraph\">It is therefore illustrated similarly, methods, however, with another color than classes.<\/p>\n<\/div><figure class=\"wp-block-media-text__media\"><img fetchpriority=\"high\" decoding=\"async\" width=\"328\" height=\"161\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/submethods.jpg\" alt=\"\" class=\"wp-image-9456 size-full\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/submethods.jpg 328w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/submethods-300x147.jpg 300w\" sizes=\"(max-width: 328px) 100vw, 328px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The effect of this is that the code in <code>transact<\/code> works as a wrapper around the code in <code>deposit<\/code> and <code>withdraw<\/code>. Invocation of <code>deposit<\/code> and <code>withdraw<\/code> starts by execution of the statements in <code>transact<\/code>; here execution of <code>inner(transact)<\/code> implies that the statements in <code>deposit<\/code> and <code>withdraw<\/code> are executed; finally the statements after <code>inner(transact)<\/code> are executed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the next section, we give a more detailed description of <code>inner<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method descriptor with a supermethod<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The following figure shows that the method descriptor for <code>deposit<\/code> includes its supermethod <code>transact<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"531\" height=\"191\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/descriptorOfDeposit-2.jpg\" alt=\"\" class=\"wp-image-8045\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/descriptorOfDeposit-2.jpg 531w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/descriptorOfDeposit-2-300x108.jpg 300w\" sizes=\"(max-width: 531px) 100vw, 531px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=4900\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>In this section, we introduce\u00a0submethods, which makes it possible to organise methods in a hierarchy similar to a class hierarchy using subclasses. As for subclass and superclass, submethod and supermethod are dual terms. A submethod is a method that has another method as a supermetod, and thus a supermethod is a method being used to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1008,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4900","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/4900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4900"}],"version-history":[{"count":33,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/4900\/revisions"}],"predecessor-version":[{"id":10014,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/4900\/revisions\/10014"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1008"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}