Objects
Advanced Data Structures

 Objects are structures that can hold data. They are used to create a special 'type' for a variable when the primitive types such as numbers and strings do not suffice. In many programming languages, objects are defined by classes. The way you get an object is by instantiating a class. Not so in Mint. In Mint, there are no distinctions between functions and classes. Classes are just functions that return objects. Both functions and classes are called subprograms. ```sub pair(a, b) return this end p = pair(200, 370) print p.a //Prints 200. print p.b //Prints 370. print p //Prints object{b: 370, a: 200}.``` Using the command 'return this' returns an object containing all variables in the current scope. In the scope of the subprogram pair, the only variables were a and b, so it returns an object containing the variable a bound to some value, and the variable b bound to some value. As said before, objects create new types of variables for you to use. For instance, Mint does not have a built-in complex number type. However, we can create a complex number type by using objects: ```sub Complex(r, i) real = r imag = i sub getReal() return real end sub getImag() return imag end sub getMagnitude() import math return sqrt(real ^ 2 + imag ^ 2) end sub getAngle() tangent = imag / real import math when real < 0 return atan(tangent) + pi return atan(tangent) end sub add(c) imag = imag + c.getImag() real = real + c.getReal() end sub subtract(c) imag = imag - c.getImag() real = real - c.getReal() end sub multiply(c) newReal = real * c.getReal() - imag * c.getImag() imag = imag * c.getReal() + real * c.getImag() real = newReal end sub divide(c) d = c.getReal() ^ 2 + c.getImag() ^ 2 newReal = (real * c.getReal() + imag * c.getImag()) / d imag = (imag * c.getReal() - real * c.getImag()) / d real = newReal end sub display() show real show " + " show imag print "i" end return this end c1 = Complex(1, 3) c2 = Complex(2, -5) c1.display() //Prints 1 + 3i c1.add(c2) c1.divide(c2) c1.display() //Prints 0.5517241379310345 + 0.3793103448275862i``` Strings and lists are also objects to some degree. Therefore you can call their internal subprograms using the dot operator (internal subprograms are called 'methods'): ```print "Raccoon".length() //Prints 7 x = [1, 2, "dye", [17, 18]] print x.pop() //Prints [17, 18] x.reverse() print x //Prints ["dye", 2, 1] x.append(48) print x //Prints ["dye", 2, 1, 48]``` Since Mint is an object oriented language, it supports many object oriented features, including inheritance. Inheritance is when you reuse code of existing objects in new objects without needing to copy-paste code. To perform inheritance in Mint, use the keyword inherit followed by a call to another subprogram that produces an object. ```import type sub Reader(text) words = text.split(" ") sub readChar(position) if position >= text.length() print "Error: Position " + string(position) + " is beyond text length." return end return text[position] end sub readWord(position) if position >= words.length() print "Error: Position " + string(position) + " exceeds the number of words." return end return words[position] end return this end // The following text is taken from Shakespeare's Sonnet 1 txt = "From fairest creatures we desire increase, \n" txt += "That thereby beauty's rose might never die, \n" txt += "But as the riper should by time decease, \n" txt += "His tender heir might bear his memory: \n" txt += "But thou contracted to thine own bright eyes, \n" txt += "Feed'st thy light's flame with self-substantial fuel, \n" txt += "Making a famine where abundance lies, \n" txt += "Thy self thy foe, to thy sweet self too cruel: \n" txt += "Thou that art now the world's fresh ornament, \n" txt += "And only herald to the gaudy spring, \n" txt += "Within thine own bud buriest thy content, \n" txt += "And, tender churl, mak'st waste in niggarding: \n" txt += "Pity the world, or else this glutton be, \n" txt += "To eat the world's due, by the grave and thee. " reader = Reader(txt) print reader.readChar(50) //Prints e print reader.readWord(15) //Prints the sub LineReader(text) inherit Reader(text) lines = text.split("\n") sub readLine(position) if position > lines.length() print "Error: Position " + string(position) + "exceeds the number of lines." return end return lines[position] end return this end reader2 = LineReader(txt) print reader2.readWord(34) //Prints bright print reader2.readLine(3) //Prints His tender heir might bear his memory:``` By inheriting from Reader, LineReader becomes a subtype of Reader. It is able to do all the things Reader is able to do, and more. Navigation Previous Lesson: Subprograms Next Lesson: Memory Management Table of Contents