This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sewbacca's profile picture

Add new Types or Structs

Started by Sewbacca, 07 January 2016 - 04:38 PM
Sewbacca #1
Posted 07 January 2016 - 05:38 PM
Is it possible to create new types (like rate or integer) or structs?
Dragon53535 #2
Posted 07 January 2016 - 09:12 PM
It's certainly possible, but probably inefficient and bulky.
MKlegoman357 #3
Posted 07 January 2016 - 09:19 PM
Can you create actual types in Lua? No. Can you somehow 'simulate' this? Yes, very easily. When talking about types I suspect you're also talking about OOP (Object Oriented Programming). In Lua, OOP is done similarly to JavaScript: while there is no native way of created classes or new types you have freedom to create your own class and type system however you want. You will most definitely use tables, which are the most useful and powerful type in Lua. They are basically objects, passed around by reference, which can hold any amount of any datatype in Lua and perform other fun functions.

One of the most widely used patterns for OOP in Lua can be found here.

I'm also wondering, do you need this for a specific program you want to code or just to know Lua better? Feel free to ask any other questions you might have :)/>
Sewbacca #4
Posted 07 January 2016 - 09:28 PM
You will most definitely use tables, which are the most useful and powerful type in Lua.

I know.

I'm also wondering, do you need this for a specific program you want to code or just to know Lua better?

I just want to know Lua better. :)/>

Is it possible declare static variables like in C#? :D/>
(a not edit able value)
Edited on 07 January 2016 - 08:31 PM
KingofGamesYami #5
Posted 07 January 2016 - 09:29 PM
I'm also wondering, do you need this for a specific program you want to code or just to know Lua better?

I just want to know Lua better. :)/>/>/>

Is it possible declare static variables like in C#? :D/>/>/>
(a not edit able value)

In short: no.

Longer version: yes, but that requires metatables or pre-processing.
Edited on 07 January 2016 - 08:30 PM
Creator #6
Posted 07 January 2016 - 09:30 PM
The tutorial uses metatables, which are very slow computer wise. It is also possible to use a function to initialize an object, and the private attributes will be local to that function, while the public ones are put in a table self and returned by the object initializing function.
SquidDev #7
Posted 07 January 2016 - 09:33 PM
The tutorial uses metatables, which are very slow computer wise. It is also possible to use a function to initialize an object, and the private attributes will be local to that function, while the public ones are put in a table self and returned by the object initializing function.

But the instantating closures is not very efficient either. For objects that will be around a very long time I'd suggest closures, but for objects that will be around in the short/medium term I'd still use metatables. Anyway, its not like most code needs to be efficient in CC :)/>.
Lyqyd #8
Posted 07 January 2016 - 10:10 PM
The vector API included with ComputerCraft is a good example that adds a "type". They are, of course, just tables, and will report that fact when passed to type(), but the metatable entries allow them to be added, subtracted, etc. from each other with the usual operators.
Sewbacca #9
Posted 07 January 2016 - 10:27 PM
The vector API included with ComputerCraft is a good example that adds a "type". They are, of course, just tables, and will report that fact when passed to type(), but the metatable entries allow them to be added, subtracted, etc. from each other with the usual operators.

I search a solution to lead type to say that is a integer or rate :(/>
Edited on 07 January 2016 - 09:28 PM
KingofGamesYami #10
Posted 07 January 2016 - 10:41 PM
You can override type.


local _type = type
type = function( x )
  local result = _type( x )
  if result == "table" then
    --#check if it's just a regular table or an object
  end
  return result
end
Sewbacca #11
Posted 08 January 2016 - 08:51 AM
Thanks! :D/>
I keep this idea in mind, but is there a other way to lead type to say it's a integer or something else?
Edited on 08 January 2016 - 03:50 PM
SquidDev #12
Posted 08 January 2016 - 10:46 AM
Thanks! :D/>
I keep this idea in mind, but is there a over way to lead type to say it's a integer or something else?

You can use x % 1 == 0 to check for "integerness" but this version of Lua doesn't have an integer type.


local _type = type
type = function( x )
  local result = _type( x )
  if result == "table" then
	--#check if it's just a regular table or an object
  elseif result == "number" and x % 1 == 0 then
    return "integer"
  end
  return result
end

That may muck up other functions though which won't expect "integer" as a type.
Sewbacca #13
Posted 08 January 2016 - 04:52 PM
Has nobody a idea to add new types without editing type?
Lyqyd #14
Posted 08 January 2016 - 04:55 PM
You cannot add new types. You can replace the type() function with a version that will lie to you. That's your only option, and it isn't a particularly good one. The real questions is, why do you need new types? What are you trying to do that can't be done without adding new types along the way?
Sewbacca #15
Posted 08 January 2016 - 05:20 PM
You cannot add new types. You can replace the type() function with a version that will lie to you. That's your only option, and it isn't a particularly good one. The real questions is, why do you need new types? What are you trying to do that can't be done without adding new types along the way?

There are manny ways to code it, but it is much easier to add new types.

And:

I just want to know Lua better. :)/>

Okay, so there is no way to add new types. :(/>

Thanks anyway!
Edited on 08 January 2016 - 04:26 PM