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

ComputerCraft did a miss

Started by TechnicalCoding, 03 February 2017 - 09:38 PM
TechnicalCoding #1
Posted 03 February 2017 - 10:38 PM
I have this function for my operative system which will create a button, which is something I asked for some details about in my last post. But in my function I have kind of a "self" object, which the function will return all the values to.


oos.graphic.btn = function ( obj, string, settings )
  if not type ( obj ) == 'table' then -- This is where it misses, because at the bottom you'll see that I put in a string, but for me it fails to see that obj is not a table.
	oos.functions.throw ( ERROR, 'SELF expects a table, not' .. type ( obj ) );
	return false;
  end
end

-- ( OpenOS: Debug ) --
oos.graphic.btn ( 'This is not a table', 'Button Content', { ["width"] = 16, ["height"] = 3, ["offsetY"] = 1, ["offsetX"] = 1, ["foregroundColor"] = colors.white, ["backgroundColor"] = colors.red } );

Does anyone else have this problem?

It is not anything wrong with the "oos.functions.throw" function, but anyway, this is how it works:


oos.functions.throw = function ( Type, message )
  local ocolor = oos.term.getTextColor (  );
  local obgcolor = oos.term.getTextColor (  );

  if not type ( Type ) == "string" then
	oos.term.setTextColor ( oos.settings.types [ ERROR ].bg );
	oos.term.setBackgroundColor ( oos.settings.types [ ERROR ].color );
	write ( oos.settings.types [ ERROR ].title );

	oos.term.setTextColor ( oos.settings.types [ ERROR ].color );
	oos.term.setBackgroundColor ( oos.settings.types [ ERROR ].bg );
	write ( ' ' .. 'Expected TYPE at oos.functions.throw ( TYPE, message )' );

	oos.term.setTextColor ( ocolor );
	oos.term.setBackgroundColor ( obgcolor );
	print ( '' );

	return false;
  end

  if oos.settings.types [ Type ] == nil then
	Type = 'UNKNOWNException';
  end

  oos.term.setTextColor ( oos.settings.types [ Type ].bg );
  oos.term.setBackgroundColor ( oos.settings.types [ Type ].color );
  write ( oos.settings.types [ Type ].title );

  oos.term.setTextColor ( oos.settings.types [ Type ].color );
  oos.term.setBackgroundColor ( oos.settings.types [ Type ].bg );
  write ( ' ' .. message );

  oos.term.setTextColor ( ocolor );
  oos.term.setBackgroundColor ( obgcolor );
  print ( '' ); -- New Line

  return true;
end

These variables below is being implemented at the the very top of my os:

ERROR = 'ERRORException';
WARNING = 'WARNINGException';
SUCCESS = 'SUCCESSException';
INFO = 'INFOException';
USAGE = 'USAGEException';

set = 'SET';
get = 'GET';
current = 'CURRENT';

So what makes my oos.graphic.btn function fail?

The table which contains the different exceptions in the throw function

-- ( OpenOS: Exception delarations ) --
oos.settings.types = {
  ['ERRORException']   = {
    color = c.red,
    bg = c.black,
    title = 'Error'
  },

  ['WARNINGException'] = {
    color = c.yellow,
    bg = c.black,
    title = 'Warning'
  },

  ['SUCCESSException'] = {
    color = c.green,
    bg = c.black,
    title = 'Success'
  },

  ['INFOException']    = {
    color = c.blue,
    bg = c.black,
    title = 'Info'
  },

  ['USAGEException']   = {
    color = c.cyan,
    bg = c.black,
    title = 'Usage'
  },

  ['UNKNOWNException'] = {
    color = c.gray,
    bg = c.black,
    title = 'Unknown'
  }
};
Edited on 03 February 2017 - 09:41 PM
Dog #2
Posted 03 February 2017 - 10:56 PM
Instead of using "not"

if not type(something) == "something" then

use "doesn't equal" instead

if type(something) ~= "something" then

I'm terrible at explaining why, but I'll give it a go. The reason it doesn't work is that Lua evaluates your not and your comparitor as separate, like so…

if type("something") is not true and if the value of that evaluation (true/false) equals your comparison variable ("table") then proceed...

Since true or false will never equal "table" you won't get the results you expect.
Edited on 03 February 2017 - 10:01 PM
TechnicalCoding #3
Posted 03 February 2017 - 11:06 PM
Instead of using "not"

if not type(something) == "something" then
But why does the above code not work? What is wrong with it? And what makes the below code work? What's right with it?
use "doesn't equal" instead

if type(something) ~= "something" then
Edited on 03 February 2017 - 10:07 PM
H4X0RZ #4
Posted 03 February 2017 - 11:55 PM
That's because of the way operators work in Lua.

Your code tries to check if "not type(…)" is equal to " 'something' ". But you want the inverse of "type (…) == 'something' ".

You can achieve that either by using ~= which just means "is unequal", or you wrap the original expression in brackets and put a "not" infront of it, like this "not (type (…) == 'something') "
Dog #5
Posted 03 February 2017 - 11:57 PM
Looks like you replied while I was editing my reply. Re-read my last reply - I do my best to explain it there. I'm terrible with explanations, so hopefully someone with a better and more clear understanding will drop in and offer their wisdom.

edit: :ph34r:/> 'd - thanks, H4X0RZ :)/>
Edited on 03 February 2017 - 10:59 PM
TechnicalCoding #6
Posted 04 February 2017 - 12:43 AM
Since true or false will never equal "table" you won't get the results you expect.

Why does it work using


function example ( var )
  if not type ( var ) == 'string' then
    print ( 'Error: Expected string!' )
  end
end
Edited on 03 February 2017 - 11:44 PM
Bomb Bloke #7
Posted 04 February 2017 - 01:02 AM
Presumably when you're calling your example function, "var" is a string. That's the only situation in which the code snippet you've got there would act as you're expecting.

But "not type(whatever)" will always be a boolean, which will never be equal to "string".

That is to say,

not type(var) == "string"

… is the same as:

(not type(var)) == "string"

… which is different to:

not (type(var) == "string")

… which is the same as:

type(var) ~= "string"
TechnicalCoding #8
Posted 04 February 2017 - 11:01 AM
Presumably when you're calling your example function, "var" is a string. That's the only situation in which the code snippet you've got there would act as you're expecting.

But "not type(whatever)" will always be a boolean, which will never be equal to "string".

That is to say,

not type(var) == "string"

… is the same as:

(not type(var)) == "string"

… which is different to:

not (type(var) == "string")

… which is the same as:

type(var) ~= "string"

Thanks for the explanation, helps a lot!
H4X0RZ #9
Posted 04 February 2017 - 01:28 PM
Since true or false will never equal "table" you won't get the results you expect.

Why does it work using


function example ( var )
  if not type ( var ) == 'string' then
	print ( 'Error: Expected string!' )
  end
end

I tried this code in a normal instance of Lua 5.1 and this is the result:


Looks like it actually doesn't work.