Jump to content




ComputerCraft did a miss

help

8 replies to this topic

#1 TechnicalCoding

  • Members
  • 35 posts

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 by TechnicalCoding, 03 February 2017 - 10:41 PM.


#2 Dog

  • Members
  • 1,177 posts
  • LocationEarth orbit

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 by Dog, 03 February 2017 - 11:01 PM.


#3 TechnicalCoding

  • Members
  • 35 posts

Posted 03 February 2017 - 11:06 PM

View PostDog, on 03 February 2017 - 10:56 PM, said:

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?

View PostDog, on 03 February 2017 - 10:56 PM, said:

use "doesn't equal" instead
if type(something) ~= "something" then

Edited by TechnicalCoding, 03 February 2017 - 11:07 PM.


#4 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

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') "

#5 Dog

  • Members
  • 1,177 posts
  • LocationEarth orbit

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 by Dog, 03 February 2017 - 11:59 PM.


#6 TechnicalCoding

  • Members
  • 35 posts

Posted 04 February 2017 - 12:43 AM

View PostDog, on 03 February 2017 - 10:56 PM, said:

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 by TechnicalCoding, 04 February 2017 - 12:44 AM.


#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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"


#8 TechnicalCoding

  • Members
  • 35 posts

Posted 04 February 2017 - 11:01 AM

View PostBomb Bloke, on 04 February 2017 - 01:02 AM, said:

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!

#9 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 04 February 2017 - 01:28 PM

View PostTechnicalCoding, on 04 February 2017 - 12:43 AM, said:

View PostDog, on 03 February 2017 - 10:56 PM, said:

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:
Posted Image

Looks like it actually doesn't work.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users