{"id":160,"date":"2019-10-15T03:20:17","date_gmt":"2019-10-15T03:20:17","guid":{"rendered":"https:\/\/binaryopus.com\/?p=160"},"modified":"2019-10-15T03:20:17","modified_gmt":"2019-10-15T03:20:17","slug":"polymorphism-in-genero-4gl-now-in-version-3-20","status":"publish","type":"post","link":"https:\/\/binaryopus.com\/?p=160","title":{"rendered":"Polymorphism in Genero 4GL &#8211; now in version 3.20"},"content":{"rendered":"\n<p>Genero in its latest version has added an interface feature.  This feature finally provides the ability to implement polymorphic like features in the language.<\/p>\n\n\n\n<h4>What is Polymorphism?<\/h4>\n\n\n\n<p>Polymorphism is a feature common in object oriented languages in which different types can have different implementations of the same method.  Take this simple example:<\/p>\n\n\n\n<p>We have an interface for a class Shape that implements a method getArea() So any instance of the shape class will be required to implement this method.  Then we have two subclasses of Shape class called Circle and Square.  When you are calling a method on an instance of a Shape class you will not need to know which type of shape you have.  For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/initializeing two instances of a shape class.\nShape s1 = new Circle(14);\nShape s2 = new Square(14);\n\/\/....\n....\n..\/\/\n\/\/Now at some later point we get the area.  We do not need to know what type of shape we have.\n\/\/Depending is the instance is a circle or square shape, the area function \na1 = s1.getArea()\na2 = s2.getArea()\n\n<\/code><\/pre>\n\n\n\n<p>In the example above, once we initialize the shape, we do not need to know what type of shape it is.  When we call the getArea method, the function for that particular shape will automatically be called.  That is the basis of polymorphism, two different implementations for the same interface or parent class.<\/p>\n\n\n\n<h4>Genero&#8217;s version of Polymorphism<\/h4>\n\n\n\n<p>Starting with version 3.20, Genero has added the ability to assign an interface to a record type.  The interface associates a the different implementations of the same function with the record type.  We&#8217;ll  use the shape example like the one that was described above. First we start with three record types to represent a square, circle and rectangle.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PUBLIC TYPE Circle RECORD\n radius FLOAT\nEND RECORD\n\nPUBLIC TYPE Square RECORD\n side FLOAT\nEND RECORD\n\nPUBLIC TYPE Rectangle RECORD\n base FLOAT\n,height FLOAT\nEND RECORD<\/code><\/pre>\n\n\n\n<p>Next we define an interface type in either the same module or in a new module.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PUBLIC TYPE Shape INTERFACE\n area() returns float\n,circumference returns float<\/code><\/pre>\n\n\n\n<p>In this case, the shape type implements two methods, area and circumference. <\/p>\n\n\n\n<p>Now I need to implement these methods for each of the record types.  Usually this will be done in the module that the original records were defined in.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import util\nFUNCTION area(c Circle) \n return util.math.PI*c.radius**2\nEND FUNCTION\n\nFUNCTION circumference(c Circle) \n return c.radius*2*util.math.PI\nEND FUNCTION\n\nFUNCTION area(s Square)\n return s.side**2\nEND FUNCTION\n\nFUNCTION circumference(s Square)\n return s.side*4\nEND FUNCTION\n\nFUNCTION area(r Rectangle)\n return r.base*r.height\nEND FUNCTION\n\nFUNCTION circumference(r Rectangle)\n return r.base*2+r.height*2\nEND FUNCTION<\/code><\/pre>\n\n\n\n<p>So now all is left is to connect the interface to the record and implementations.  By assigning the shape to the interface variable will also autmatically import the methods that accept the corresponding types.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MAIN\n define\n  c Circle\n,s Square\n,r Rectangle\n,shapes DYNAMIC ARRAY OF Shape\n,i INT\nLET c.diameter = 2.0\nLET s.side = 2.0\nLET r.base = 2.0\nLET r.height = 4.0\nshapes[1] = c\nshapes[2] = s\nshapes[3] = r\n\nFOR i = 1 to shapes.getLength()\n#In each case, the appropriate area and circumference implementations will be called.\n DISPLAY \"Area: \", i, \":\" shapes[i].area()\n DISPLAY \"Circumference: \", i, \":\", shapes[i].circumference()\nEND FOR\n\nEND MAIN<\/code><\/pre>\n\n\n\n<p>The difference with other languages is that the relationship between the interface and the type is not defined when the record is defined. The relations ip between the implementations is implicitly created when the record instance is assigned to the interface variable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Genero in its latest version has added an interface feature. This feature finally provides the ability to implement polymorphic like features in the language. What is Polymorphism? Polymorphism is a feature common in object oriented languages in which different types can have different implementations of the same method. Take this simple example: We have an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,8],"tags":[18,2,19],"_links":{"self":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts\/160"}],"collection":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=160"}],"version-history":[{"count":1,"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":161,"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions\/161"}],"wp:attachment":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}