OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Mar 18, 2024 8:26 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: what is a class?
PostPosted: Thu May 05, 2016 5:03 am 
Offline
Member
Member

Joined: Mon Dec 21, 2009 6:03 pm
Posts: 164
I've been working on my own language for a while now, and recently added user-defined units (to help with some of the problems in the language).

E.g. (comments on syntax welcome)
x := 10.1(kg) + 3(g)

is basically sugar for (based on smalltalk syntax)

x := (WEIGHT kg: 10.1) + (WEIGHT g: 3)

where 'WEIGHT' is a class, and 'kg' is a class method taking one argument and returns an object.

this introduces the concept of a 'unit'. A 'kg' is a unit of WEIGHT

I've been analysing and considering what this actually does. Three things have come up that make me question what a class is. NUMBERs, COLOURs and SHAPEs.

I originally constructed a colour as..
RGB red: 255 green: 255 blue: 255
HSL hue: 255 saturation: 255 luminance: 255

But with the unit way of thinking this has combined them into one class called COLOUR. All functionality is equivalent with both ways.

You could also do the same with numbers. An integer is after all in the same 'system' as a real or complex.

So my question is, is INTEGER really a class? or is it just aperspective of a class NUMBER?

What seems to be happening compared to traditonal class usage is the new class has become the traditional abstract class and the new class method constructor has become the traditional class.

look forward to hear thoughts
Mike Brown


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 10:14 am 
Offline
Member
Member

Joined: Sun Feb 01, 2009 6:11 am
Posts: 1070
Location: Germany
Every INTEGER is a NUMBER, but not every NUMBER is an INTEGER. It's a strict subset. So if you call these things classes, I guess I would call INTEGER a subclass of NUMBER.

_________________
Developer of tyndur - community OS of Lowlevel (German)


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 11:02 am 
Offline
Member
Member

Joined: Sat Mar 01, 2014 2:59 pm
Posts: 1146
Normally though
Kevin wrote:
Every INTEGER is a NUMBER, but not every NUMBER is an INTEGER. It's a strict subset. So if you call these things classes, I guess I would call INTEGER a subclass of NUMBER.
Normally though we make "integer" a base type rather than a class.

_________________
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 1:39 pm 
Offline
Member
Member

Joined: Thu Mar 25, 2010 11:26 pm
Posts: 1801
Location: Melbourne, Australia
onlyonemac wrote:
Normally though
Kevin wrote:
Every INTEGER is a NUMBER, but not every NUMBER is an INTEGER. It's a strict subset. So if you call these things classes, I guess I would call INTEGER a subclass of NUMBER.
Normally though we make "integer" a base type rather than a class.
You mean we make NUMBER a base type? INTEGER is derived from NUMBER.

_________________
If a trainstation is where trains stop, what is a workstation ?


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 1:52 pm 
Offline
Member
Member

Joined: Sat Mar 01, 2014 2:59 pm
Posts: 1146
gerryg400 wrote:
onlyonemac wrote:
Normally though
Kevin wrote:
Every INTEGER is a NUMBER, but not every NUMBER is an INTEGER. It's a strict subset. So if you call these things classes, I guess I would call INTEGER a subclass of NUMBER.
Normally though we make "integer" a base type rather than a class.
You mean we make NUMBER a base type? INTEGER is derived from NUMBER.
No, we make integer a base type. Conceptually, an integer is a type of number just as a float is a type of number, and a long int is a type of integer just as a short int or a char is a type of integer - consequently, the compiler allows casting from one "number" type to another (sometimes with a loss of precision), but the base type is still "integer" (or more accurately, "long int", "short int", "char", etc.).

_________________
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 1:53 pm 
Offline
Member
Member

Joined: Mon Dec 21, 2009 6:03 pm
Posts: 164
Kevin wrote:
Every INTEGER is a NUMBER, but not every NUMBER is an INTEGER. It's a strict subset. So if you call these things classes, I guess I would call INTEGER a subclass of NUMBER.


Yes, subset! That makes sense.

What I've created above is a superset, its a sum of all subsets. This makes me question inheritance, perhaps typical inheritance is upside down? What i think i want is to extend the superclass with each subclass definition (like a partial class with a label), this would mean an instance of NUMBER would contain all the code from class INTEGER.

This would mean the code provided by "INTEGER" is a specialization provided by NUMBER, and can be used in situations where its constraints can be proven?

Mike


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 2:08 pm 
Offline
Member
Member

Joined: Mon Dec 21, 2009 6:03 pm
Posts: 164
A pseudo code example,

Code:
class SHAPE {
  point POINTS[];

  area() {
    // Calculate the area of any polygon.
    result = 0;
    foreach(c = 0; c < POINTS.length - 1; c++) {
      result += POINTS[c].x * POINTS[c+1].y
      result += POINTS[c].y * POINTS[c+1].x
    }
    result += POINTS[POINTS.length].x * POINTS[0].y
    result += POINTS[POINTS.length].y * POINTS[0].x
    result = result / 2
    return result;
  }
};

extend SHAPE with RECTANGLE {
    int width, height;
    // We can use "specialized" variables to speed up computation (bit like an index/cache)
   // We still need to update POINTS in the superclass.

    area() {
      return width * height;
    }

   // ... A list of constraints? If we can prove its a "rectangle" we can use these methods
   // If constraints are broken, decomposes to a SHAPE.
   // If its ALWAYS a rectangle, we can code eliminate and remove unnecessary "SHAPE" code.
}


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 2:20 pm 
Offline
Member
Member

Joined: Thu Mar 25, 2010 11:26 pm
Posts: 1801
Location: Melbourne, Australia
onlyonemac wrote:
No, we make integer a base type. Conceptually, an integer is a type of number just as a float is a type of number, and a long int is a type of integer just as a short int or a char is a type of integer - consequently, the compiler allows casting from one "number" type to another (sometimes with a loss of precision), but the base type is still "integer" (or more accurately, "long int", "short int", "char", etc.).
Oh, I can now see how you got confused an made an incorrect statement.

_________________
If a trainstation is where trains stop, what is a workstation ?


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 4:13 pm 
Offline
Member
Member

Joined: Tue Feb 10, 2015 3:36 pm
Posts: 47
The better question is "what is an object?".


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 5:53 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
Classes are not types, but categories of types. What C++, Java, C# and similar languages call a "class" is actually a "type". This is very unfortunate. Functional languages like Haskell tends to get it right.

You can see this inconsistency in C++ when you use 'typename' when declaring template parameters. Also in both C/C++ you have the "typedef" keyword.

onlyonemac nailed it when he said:

onlyonemac wrote:
Conceptually, an integer is a type of number just as a float is a type of number.


So 'number' is the class. 'integer' and 'float' are types. 'number' is not a base type, it is a class.

_________________
https://github.com/kiznit/rainbow-os


Last edited by kzinti on Thu May 05, 2016 9:43 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 9:42 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
cxzuk wrote:
So my question is, is INTEGER really a class? or is it just aperspective of a class NUMBER?


"Integer" is a type of class "number". That's what the language theory nomenclature says.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 11:33 pm 
Offline
Member
Member

Joined: Sat Nov 07, 2015 3:12 pm
Posts: 145
The sole reason some languages see Integers/Reals are different classes is because they are handled not in the same way in the CPU.
Here is one big difference that makes people using integers instead of reals in financial apps:
+ Integers can overflow. This can be fixed with checks in code.
+ Reals ( float/ double) can underflow


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Thu May 05, 2016 11:53 pm 
Offline
Member
Member

Joined: Mon Dec 21, 2009 6:03 pm
Posts: 164
kzinti wrote:
cxzuk wrote:
So my question is, is INTEGER really a class? or is it just aperspective of a class NUMBER?


"Integer" is a type of class "number". That's what the language theory nomenclature says.


I think you're right, and also agree with the mismatch of classes and types. I've been avoiding types in my language but I suspect I need to understand there place soon.

After sleeping on this problem, the reason I want to extend the SHAPE class with the RECTANGLE class is because SHAPE must have all the methods RECTANGLE provides. Another way to achieve this is to have subclasses only be able to redefine methods, and not extend its inherited classes.

This feels right at first glance, and inheritance becomes 'specialisation' rather than extention. (multiple inheritance would provide overriding the intersection of methods of two or classes), Data members are encapsulated in my language so "caching" or.helper variables would be allowed.

Time to read up why inheritance allows extending of its superclass :)


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Fri May 06, 2016 12:10 am 
Offline
Member
Member

Joined: Mon Dec 21, 2009 6:03 pm
Posts: 164
zdz wrote:
The better question is "what is an object?".


my definition of object in my language is;

A fully independent computational unit that can ONLY receive and send messages with other objects. State AND behaviour are encapsulated. (This is the actor computational model).

An object is the atomic unit of construction of a program. (Objects are not Russian dolls, you can't crack an object open and more objects fall out. The better metaphor is routers connected together in a network passing messages to each other).

Objects can however be 'simulated' (virtualised) but providing a virtual environment.

An object has a network interface - A driver to access the data received (A traditional example is a calling convention and stack details) - And a "Controller" (mvc), logic to understand what the message means (A 'view' is state in a stateful protocol. The controller uses the view information to help understand messages) and invokes model methods once it understands the message intentions.

And object has a default Controller (with a stateless protocol so no view).

I'm pretty happy with the object side of things but comments always welcome


Top
 Profile  
 
 Post subject: Re: what is a class?
PostPosted: Fri May 06, 2016 12:27 am 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
cxzuk wrote:
This feels right at first glance, and inheritance becomes 'specialisation' rather than extention.


Using class (as in C++ class) inheritance for extension is bad design. It should always be about specialization. In C++, avoid subclassing as much as possible as it is the highest form of coupling.

Note that using abstract class in C++ to define interfaces is perfectly fine. But these have nothing to do with subclassing. You can see that in other languages like C# where you use the "interface" keyword instead of abusing the "class" or "type" keywords.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group