Author Topic: Suggestion: Object instantiation  (Read 2458 times)

0 Members and 1 Guest are viewing this topic.

Petr Schreiber

  • Guest
Suggestion: Object instantiation
« on: December 18, 2010, 08:19:12 AM »
Hi Charles,

I would like to be able to define explicitly constructor(optionally with params) and destructor(always without params, executed by GC) in classes, and be able to invoke it like:
Code: [Select]
class GameObject

  constructor (Single x, Single y, Single z)

  end constructor

  destructor()

  end destructor

end class

...

function InitLevel()
  
  GameObject go = new GameObject(1,2,3)

  ' here go goes out of scope, so Oxygen would call its destructor first, and then deallocated it from memory
end function

I know you demonstrated the use of new and del macros, but this built in support would be better for us, poor mortals.


Thanks,
Petr
« Last Edit: December 18, 2010, 01:03:46 PM by Petr Schreiber »

Charles Pegge

  • Guest
Re: Suggestion: Object instantiation
« Reply #1 on: December 18, 2010, 02:02:58 PM »

Hi Petr,

Yes I will canonise new and del (and constructor and destructor) so they become part of the core language. The "hard-cores" will still be able to create objects to their own specification but these are a safe way to create dynamic objects.

Example:
Code: [Select]

'==========
class shape
'==========
  '
  double x,y,z
  '
  method constructor(double*a,double*b,double*c)
  '=============================================
    x=a : y=b : z=c
    string tab=chr 9
    print "construct" tab x tab y tab z
  end method

  method destructor()
  '==================
    print "destroy"
  end method

end class



new shape cuboid 1,2,3

'...

del cuboid

Here are the entries in keyw.bas where most O2 keywords are defined:
Code: [Select]
data "new"            ,-19,"%1 * %2 : & %2=news sizeof %1 : %2.constructor"
'=====================

'action:   create a dynamic object and call its constructor method.
'use:      to create and initialise persistent objects
'example:  new shape cuboid 1,1,1
'related:  del
'group:    system macro


data "del"            ,-19,"%1.destructor : frees & %1 : & %1=0"
'=====================

'action:   Call a dynamic object's destructor method and disallocate its memorey block.
'use:      to delete persistent objects
'example:  del cuboid
'related:  new
'group:    system macro

I'll put this into Alpha024 which I will release before Christmas.

Many thanks!

Charles

Petr Schreiber

  • Guest
Re: Suggestion: Object instantiation
« Reply #2 on: December 18, 2010, 02:13:14 PM »
Thank you Charles!

That is very smart way of design - I wanted to write my own macro first for tests, but didn't know how to make the parameter passing done (there can be none or many). Now I see it in your trick :)


Petr