Re: Pass by Reference

new topic     » goto parent     » topic index » view thread      » older message » newer message

I'm slowly grinding my way through my Java application, porting it to Euphoria. The mapping of between the two is pretty straightforward, but a bit tedious.

The "attributes" of a class can be declared via enum:

public enum attrib_alpha, attrib_beta, attrib_gamma, element_count 

and then a constructor declared along the lines of:

public function new( ... ) 
    -- create an instance 
    sequence this = malloc(0, element_count-1) 
 
    -- initialization code goes here 
    this[attrib_alpha] = rnd() 
    this[attrib_beta] = rnd() 
    this[attrib_gamma] = rnd() 
 
 
   return malloc(this) 
end function 
 
public function my_method(integer this_handle) 
    -- get the instance 
    sequence this = ram_space[this_handle] 
 
    -- change an attribute 
    this[attribute_alpha] = rnd() 
  
    -- update the object 
    ram_space[this_handle] = this 
 
    -- return some result 
    return ram_space[this_handle] 
end function 

If I didn't mind being clunky, I could even code this as:

public function my_method(integer this_handle) 
 
    -- change an attribute 
    ram_space[this_handle][attribute_alpha] = rnd() 
 
    -- return some result 
    return ram_space[this_handle][attribute_alpha] 
  
end function 

Calling the routine is along the lines of:

include "my_class.e" 
 
-- create an instance of my_class 
integer instance = my_class:new() 
 
-- call a method of my_class 
integer result = my_class:my_method(instance, integer new_alpha) 

I can access class attributes like so:

-- this is equivalent to instance.attrib_alpha 
? ram_space[instance][my_class:attrib_alpha] 

This (in theory) would work fairly well for classes without inheritance. It would obviously work much nicer if the interpreter handled this stuff automatically. I need play with this some more to see how well it would work for wxWidgets. In the mean time, I'll continue converting my code from Java and see what sort of snags I run into.

- David

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu