Mike's Program

A place for notes

Micheal Earl

Writing has always been a difficult beast for me. Every time I sit down to write something, I just fail to bring anything substantial to mind. I don’t live a fanciful life of tantalizing adventure but, rather, a fanciful life of mediocre preservation.

Every time I come back to trying to write on my blog, my eyes are drawn to my first post: “I’m still alive”. Ironic, because that is not actually my first post. My first two posts were deleted by me when I wrote “I’m still alive.” Perhaps I thought that I could just retcon those posts out of my life.

You see, I constantly suffer from a serious case of Imposter Syndrome. Everything I do feels like walking through a deep pool of molasses. There is this extra resistance that seems to be tacked on to every task. Why should I try to write about my programming discoveries when dozens, or even hundreds of other people have already outdone my solutions. How do I know if something is good enough?

This brings me back to “I’m still alive.” It is a small post that I wrote as an attempt to convince myself to try writing again, but it clearly did not work (as evinced by the 2+ year gap in posts). Even though I feel that this post is superfluos, small, and insignigant, I do think that I will keep it here. I am done retconning my past. Or, maybe, it’s just nostalgia.

Micheal Earl

I made a post a while back about how I need to pick something and really go at it for a while. Finally, I have come to a decision regarding that post. My focus from this point will be on systems level programming (with some web).

C has always been really fun for me. I like having powerful low level control, but I’m less thrilled about all the pitfalls that I encounter. Using C for large projects is definitely not beginner friendly. This brings me to Rust, which isn’t really easier, but inherently avoids many of the pitfalls prevelant in C. I feel that using both languages will give me strong fundamentals in systems level programming.

Micheal Earl

Well this blog didn’t really take off. I just can’t spin up the effort to write something every day. I’ve recently gotten into Lua and the LOVE 2d game library. Playing around with that has been pretty fun.

I’m using a library in lua to help me implement OOP functionality. The library is called classical.lua and you can find it here.

You can see some notes I made on the usage of the classical library in my repo here

And, just so I can test some stuff on my blog I will post the notes here too.

 

Main.lua:

-- We must require our "classic" library to implement this
-- version of OOP. We then require our two object Classes
-- so that we can make Point and Vehicle objects + their children
Object = require 'lib/classic/classic'
require 'objects/Point'
require 'objects/Vehicle'

-- Just a main function for flavor
function main()
  -- Make 3 point objects and assign them values for x and y
  local point = Point(5, 4)
  local point2 = Point(10, 3)
  local point3 = Point(15, 4)

  -- Print the point values of our 3 point objects
  point:printPoint()
  point2:printPoint()
  point3:printPoint()

  -- Make a Rect object which is a child of point
  local rect = Rect(10, 10, 500, 500)
  rect:printPoint() -- Print our Rect objects coordinates using our
                    -- parents function Point:printPoint()

  -- Make a Car object which is a child of Vehicle
  local toyota = Car("Toyota Car", 75, "White", 4, 4)
  toyota:printName()  -- Print our Car objects name using our parent
                      -- function Vehicle:printName()

  -- Begin checking what our toyota object belongs to
  -- it is an Object, Vehicle, and finally a Car
  print(toyota:is(Object))
  print(toyota:is(Vehicle))
  print(toyota:is(Car))
  print(toyota:is(Rect))

  -- Perform similar function on our Rect object and
  -- Print the results
  print(rect:is(Point))
  print(rect:is(Rect))

end

-- Run our main function
main()

 
 

And the two classes, the first being Vehicle.lua:

Object = require '../lib/classic/classic'

-- Start of the Vehicle class. Creates Vehicle objects.
Vehicle = Object:extend()

function Vehicle:new(name, speed, color)
  self.name = name or "Vehicle"
  self.speed = speed or 60
  self.color = color or "Green"
end

function Vehicle:printSpeed()
  io.write(self.speed.."\n")
end

function Vehicle:printName()
  io.write(self.name.."\n")
end

function Vehicle:printColor()
  io.write(self.color.."\n")
end

-- Start of the Car class. Creates Car objects. Extends the Vehicle class.
Car = Vehicle:extend()

function Car:new(name, speed, color, numOfWheels, numOfDoors)
  Car.super.new(self, name, speed, color)
  self.numOfWheels = numOfWheels or 4
  self.numOfDoors = numOfDoors or 0
end

-- Overload the printName function from the parent object
function Car:printName()
  io.write(self.name .. " overloaded \n")
end


function Car:printNumOfWheels()
  io.write(self.numOfWheels.."\n")
end

function Car:printNumOfDoors()
  io.write(self.numOfDoors.."\n")
end

 
 

And the second being Point.lua

Object = require '../lib/classic/classic'

-- Make a Point object
Point = Object:extend()

function Point:new(x, y)
  -- Creating properties for our future object
  self.x = x or 0
  self.y = y or 0
end

function Point:printPoint()
  io.write(self.x .. ", " .. self.y .. "\n")
end

-- Make a Rect object inheriting from point
Rect = Point:extend()

function Rect:new(x, y, width, height)
  -- Access our parent object and run its constructer
  -- store those parent properties in our new Rect object
  Rect.super.new(self, x, y)
  self.width = width or 0
  self.height = height or 0
end

 
That should give you a pretty good idea on how the classical library works.