Data Type vs Class

What’s the difference between a Data Type and a Class from a Lua perspective? The wiki has pages for both:

image

It gets confusing to me. For example, “Random” is a data type, but Random.new creates a random object. Isn’t an object an instance of a class?

1 Like

Some Roblox classes are also used as data types. Vector3, for instance. The difference is typeof will distinguish these data types, i.e. typeof(Vector3.new()) is “Vector3” and typeof(Instance.new("Part")) is “Instance”. Regular data types that aren’t Roblox classes, like number, string, table, function are identified by both the typeof and type functions. The type function returns userdata for all classes but the typeof function works on data type classes.

2 Likes

Are you saying Vector3 is also a class…?

In Roblox terms, classes are instances and datatypes are Lua primitives or classes without physical instances tied to the DataModel. By Roblox definitions Vector3 is not a class but by standard coding convention it is. Essentially: is Vector3 a class? Well yes but actually no.

Contrary to what @1waffle1 said, Vector3 is considered a type. typeof returns both Lua and Roblox types. As well, it’s important to note base classes: Part’s base class is Instance and it inherits from it, but Vector3 does not and it is treated as its own datatype.

1 Like

If Vector3 isnt considered a class, how is it an object?

It’s not really an object, that’s how it is by convention. I don’t know what you’d call it frankly. Maybe it still is an object and the only difference is the classification of the location where you constructed the instance from.

Roblox is weird when it comes to these kinds of things because the line between what constitutes a datatype, object and class is fairly blurry. Conventionally (by programming standards), both Roblox datatypes and instances are classes, but in Roblox practice a datatype is an instanceless class or Lua primitive and a class is something that can also have a representation in the DataModel.

While it’s an interesting question, is there any reason why you need to know the difference or is it just for the sake of knowledge?

I feel like I would be a more competent programmer if I knew the details and proper terminology with what I/m working with.

I never really had a formal education when it came to learning RBX Lua, I kind of just jumped into it and learned from hands on experience. So when it comes to dealing with the specific nomenclature, I’m kind of stumped.

I’m pretty much on the same boat. I never actually delved into any material, I learned Roblox programming myself and picked up things along the way by referencing articles or talking with others on the DevForum.

Personally, I don’t feel it’s worth much investing into nomenclature. So long as you know what Lua primitive types are, you’re fine. Knowing the difference for something so obscure doesn’t have any impact on your competence. Both Roblox datatypes and Roblox classes are just pseudoclasses and userdata, nothing special in knowing what they are. Even I don’t.

It’s probably easier if you think about classes as objects made from Instance.new and datatypes as classes with constructors that you can’t actually parent to the DataModel as an instance.

1 Like

Honestly that really is a good way of thinking of it.

I really drain myself out when I go down to the molecular level of programming and it probably slows down my development progress anyways.

1 Like

The .new part of Vector3.new is a class constructor. It constructs a new instance of the Vector3 class, and the new instance is an object. Conventionally, within Roblox, classes that don’t inherit from the Instance superclass are just considered individual data types. They are all classes, but the data type ones don’t inherit from Instance so they aren’t physically represented inside of the game hierarchy, they just contain data and have operators like a data type. When people refer to classes in the context of Roblox, they typically mean objects that inherit from Instance. They could also be referring to custom classes written in a script using some idiomatic Lua analog of OOP using a table with a constructor in it and a metatable with a __index metamethod that points to the set of class methods. Different things are meant in different contexts.

Random is a pretty bad example of a “data type” because really it isn’t. It only has methods. It doesn’t actually contain data like a Color3 or a CFrame, but since you can’t put a “Random” inside of a game, it’s grouped into the list of data types.

5 Likes

The blurring of the lines here remind me of classes VS structs in other languages. There’s a lot of interchangeable use cases here.

Generally though structs tend to be a group of data bunched together to represent a more complicated form of data. Classes tend to orient more towards behavior.

Lua being the language it is, there’s a lot of line blurring. Statically typed languages tend to have much more rigid definitions.

In Roblox Class refers to sub classes of the Instance class. It’s pretty much just a shortening of Instance Class. Data types refer to userdatas which contain new data and they are used within Instances. Vector3 is a userdata containing three components (x, y, and z). Under the hood this is probably used in a data struct (hence the name data type) rather than a full class although I can’t be sure of that.

Lua doesn’t exactly have a concept for classes. Userdatas are basically interfaces so that lua can interact with C/C++ code and vice versa. Same idea as a c function but for more complex data types. In Roblox your lua code can make userdatas with newproxy which is useful for modules that want to prevent rawset/rawget.