Log In  

Switching from Another programming to Lua

Objective

If you are wanting to program in Pico-8 but are familiar in another programming language except for LUA. This reference will help you get started.

Further Updates


I plan on extending this post to include multiple languages. I know over 13 programming languages. Please like and suggest what language you would like to see next. I also plan on covering more of each
language in detail later as well.

Suggestions


Please provide input if I make any mistakes such as the code does not compile or if you have a better tested solution.

Direct Comparison of Programming Languages.

Class Declaration

Notes:


To simply keep the instance variable "a" private for the table foo do not pass back the
address back from the create_foo function. Without the address an external user can not change the
value of "pa" (private "a").

Lua Code

--Lua

function create_foo(a)
 --make the "a" a private global variable 
 local self = {pa = a or 0}

 --considered a private method at the moment
 local _print = function(b)
  print(self.pa)
 end

 --changes print from a private function to a public method 
 return {print=_print}
end

foo = create_foo(5)
foo.print()

--[[
--prints nill
print(foo.pa)
--]]

output => 5

Equivalent Python 3 Code

class Foo:
  def __init__(self, a=0):
    self.__a = a

  def print(self):
    print(self.__a)

f = Foo(5)
f.print()

Equivalent C++ Code

/*
rewrite includes without spaces before you compile.
*/

#include < stdio.h >
#include < iostream >

using namespace std;

class Foo{
    private:
        int a;

    public:
        Foo(int a){ this->a = a; }
        void print();
};

void Foo::print(){
    cout << this->a << endl;
}

int main()
{
    Foo f(5);
    f.print();    
    return 0;
}

Equivalent Java Code

//Java

public class Main
{
    public static void main(String[] args) {
        Foo f = new Foo(5);
        f.print();
    }
}

class Foo{
    private int a;
    public Foo(int a){
        this.a = a;
    }
    public void print(){
        System.out.println(this.a);
    }

}

Equivalent C# Code

//C#
using System;

public class Program
{

    public static void Main()
    {
        Foo f = new Foo(5);
        f.print();      
    }
}

public class Foo
{
    int a = 0;

 public Foo(int a)
 {
     this.a = a;
 }

    public void print()
    {
     System.Console.Write(this.a);
    }
}

output => 5

Inheritance

Notes:


To take advantage of Inheritance In Lua keep in mind that Tables in Lua are Mutable by default.
Simply just add the FOO table to the Bar Table and return it to inherit all the methods or functions from
FOO.

Lua Code

--lua

function create_foo(a)
 local self = {pa = a or 0}

 local _print = function(b)
  print(self.pa)
 end

 return {print=_print}
end

function create_bar(o,b)

 local self = {pb = b or 0}

--BAR inherits values from FOO
 local temp = o

--add additional functions over inherited functions
    temp.get_b = function()
     return self.pb
    end

    temp.set_b = function(c)
     self.pb = c
    end

 return temp
end

bar = create_bar(create_foo(5),10)
bar.print()
print(bar.get_b())

--[[
--prints nil
print(bar.pb)
--]]

output => 5
10

Equivalent Python 3 Code

class Foo:
  def __init__(self, a=0):
    self.__a = a

  def print(self):
    print(self.__a)

class Bar(Foo):
  def __init__(self, a=0, b=0):
    super().__init__(a)
    self.b = b

bar = Bar(5,10)
bar.print()
print(bar.b)

Equivalent C++ Code

/*
rewrite includes without spaces before you compile.
*/

#include < stdio.h >
#include < iostream >

using namespace std;

class Foo{
    private:
        int a;

    public:
        Foo(int a){ this->a = a; }
        void print();
};

void Foo::print(){
    cout << this->a << endl;
}

class Bar : public Foo {
 private:
    int b;

 public:
    Bar(int a, int b) : Foo(a) { this->b = b; }
    int get_b() {return b;}
    void set_b(int value) {this->b = value;}
};

int main()
{
    Bar b(5,10);
    b.print();    
    cout << b.get_b() << endl;
    return 0;
}

Equivalent Java Code

//Java

public class Main
{
    public static void main(String[] args) {
        Bar b = new Bar(5,10);
        b.print();
        System.out.println(Integer.toString(b.get_b()));
    }
}

class Foo{
    private int a;
    public Foo(int a){
        this.a = a;
    }
    public void print(){
        System.out.println(this.a);
    }
}

class Bar extends Foo{
  private int b;
  public Bar(int a, int b){
      super(a);
      this.b = b;
  }
  public void set_b(int value){
      this.b = value;
  }
  public int get_b(){
      return this.b;
  }
}

Equivalent C# Code

//c#
using System;

public class Program
{

    public static void Main()
    {
        Bar b = new Bar(5,10);
        b.print();
        System.Console.Write(b.B);      
    }
}

public class Foo
{
    int a = 0;

 public Foo(int a)
 {
     this.a = a;
 }

    public void print()
    {
     System.Console.WriteLine(this.a);
    }
}

public class Bar : Foo
{
 int b;
 public Bar(int a, int b) : base(a) 
 {
  this.b = b;
 }

 public int B
 {
  get{return b;}
  set{b = value;}
 }
}

output => 5
10

P#70971 2020-03-31 06:32 ( Edited 2020-04-01 03:03)

1

if kept as a private variable, the ‘int a’ member can be elluded:

function create_foo(a)
 local temp = {
  print = function(self)
  print(a)
 end
 }
 return temp
end

and a more ‘object oriented’ calling syntax would be:

— same as: b.print(b,12)
b:print(12)

lua automatically adds a ‘self’ first parameter to the call

P#74356 2020-03-31 11:23 ( Edited 2020-03-31 11:25)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2022-01-27 18:18:57 | 0.010s | Q:11