Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

Types in Lua

This topic is 3738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quote:
Programming in Lua There are eight basic types in Lua: nil, boolean, number, string, userdata, function, thread, and table. The type function gives the type name of a given value: print(type("Hello world")) --> string print(type(10.4*3)) --> number print(type(print)) --> function print(type(type)) --> function //here print(type(true)) --> boolean print(type(nil)) --> nil print(type(type(X))) --> string //and here The last example will result in "string" no matter the value of X, because the result of type is always a string.
I don't understand how. type(type) results in a function, but type(type(x)) result in a string. But aren't they the same thing? So why does the last chunk (as statements are called in Lua) always result in a string?

Share this post


Link to post
Share on other sites
Advertisement
I have never programmed in lua but I will take a stab.

I assume the signature of 'type' is Expression -> String. So type takes an expression and returns a string that is the type name. type (type) is: (Expression -> String) type. Type is a function.

However by applying some x to type we get the result in the codomain of the function. type(x) always returns a string. Thus type(type(x)) is a string regardless of x. i.e. f x = 2x. f is a function but f 2 is 4, an integer.

Share this post


Link to post
Share on other sites
Looks like I was slow, but here is my answer anyway:

type is a function that returns the type name of its parameter. This type name is a string, since Lua has no (built-in) concept of classes. So type(type) will return the string "function", and then type(type(type)) is the same as type("function") which is a "string".

Also, it's not quite right to call a statement a 'chunk'. A Lua chunk is actually a sequence of statements, as you would get when you loaded an entire .lua file. Check section 2.4.1 in the docs :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!