Feature Request: Methods in Types

General FreeBASIC programming questions.
Posts: 228
Joined: Mar 12, 2006 16:25

Feature Request: Methods in Types

Postby RockTheSchock » Mar 12, 2006 16:53

Wouldn't it be nice to have more object oriented TYPE's?

if you could write:

Code: Select all

TYPE wndObject
     PRIVATE title AS STRING 'private: only accessible from Method
     SUB paintALL()        'new Type Methods
         print this->title    'this as new Keyword in Methods
     END SUB

DIM wnd AS wndObject
wnd.paintAll ()                'this = @wnd

I dont know if it's a good Idea to Implement SUbs and Functions in Type Blocks, but in Java methods are implemeted in Classes, too.

A nice to have feature would be an EXTENDS Keyword for TYPEs

Code: Select all

TYPE Person
   name AS String
   SUB PrintAll
      print this->name

TYPE Employee EXTENDS Person
   sallary AS DOUBLE
   SUB PrintAll                 'overwrites Method from person
      print this->sallary
Last edited by RockTheSchock on Mar 12, 2006 17:07, edited 2 times in total.
Posts: 1525
Joined: May 27, 2005 17:18

Postby jofers » Mar 12, 2006 16:55

Object oriented programming is on the to-do list, but it will wait while the compiler is adapted to gcc, and after that it will still take awhile to implement, so you can use useful C++ classes like vector and directx.

So: It's on the horizon, but don't hold your breath waiting :)
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Postby 1000101 » Mar 12, 2006 18:36

Also, Fb allows function pointers in structures (types) but you can't put the function directly in as you did.

The best you can do is what I showed shadowolf, seen here: http://www.freebasic.net/forum/viewtopic.php?t=3298
Posts: 228
Joined: Mar 12, 2006 16:25

Think about the code above as suggestion

Postby RockTheSchock » Mar 12, 2006 21:15

What you have shown Shadowolf I've already used on my own in one of my projects. It disturbs me that in every Procedure I must add an extra parameter "this as ... ptr" and I have to set the Pointer to a procedure.

It's just an idea, how the compiler could be extended with OOP.

I wanted to hear what syntax you would like to have in a future version of fbc and I wanted some statements from an fbc developer if it's possible to implement these suggestions.

For example if the compiler reaches a statement like

Code: Select all


it moves the adress of wnd to a register and inside a method "this" is automatically created as "DIM this AS TypeName PTR" and the adress in the register is moved to "this".

thats how I would implement this future. Alternatively it could be pushed like any other paramter to the stack, but hidden without the need to be declared. It would be even better if you dont need to write this-> in front of members of the Type.

If a Procedure would be defined globally and not inside the type
"DIM this AS TypeName PTR" could not be created automatically but it has to be declared explicit. So a normal global procedure could be used as Method of a Type and there would be no need for a Method in a typeblock.
In this case if this line of code
"DIM this AS TypeName PTR"
is defined in a global procedure whenever it's called from an var of custom type it's filled with its adress.

I hope you understand what I mean, beacause my English isnt that good
Posts: 775
Joined: Jul 01, 2005 18:45

Postby VirusScanner » Mar 12, 2006 22:16

You'll have to wait around a while, but soon FB will have the CLASS statement, and all that you're asking for will be possible:

Code: Select all

class person
    name as string
    sub set_name( nm as string )
        this->name = nm
    end sub
end class

class employee extends person
    id as integer
    sub set_info( newid as integer, nm as string )
        this->id = newid
        this->name = nm
    end sub
end class

at least is how I think it will work.
Posts: 655
Joined: Jan 10, 2006 0:44

Postby tunginobi » Mar 13, 2006 0:34

I like how VS has it laid out. Nice and clean.

And don't you worry, Rock, OO is on the list for stuff to be added to FB.
Site Admin
Posts: 3799
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il

Postby v1ctor » Mar 13, 2006 1:07

From the TODO list:

[ ] classes
- *MUST* follow the GCC 3.x ABI to make it easier to reuse C++ libs compiled by GCC
- single inheritance, plus interfaces
- exceptions - with stack unwind support
- operator overloading
- properties
- pure virtual methods
- down casting
- name space
- static constructors and destructors
- some support for RTTI

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests