1. Properties in Methodica

After carefully reviewing Derek's second post, I have tried my hand at creating
a good readable syntax for defining properties. I have also adopted his reasobale
suggestion that methods and properties both have private access by default.
No access specification is used for instance variables.

Derek's example class reworked again:

class Quad extends Entity

integer h,w

property integer height
public get	
	return this.h
public set
	if value<0 then
		throw Type_Error("Height must not be negative.")
	else
		this.h=value		
	end if
end property

property integer width 
public get
	return this.w		
public set
	if value<0 then
	        throw Type_Error.new("Width must not be negative.")
	else
		this.w=value
	end if
end property

property integer area
public get
	return this.h*this.w
private set	
	integer x
        x=Math.sqrt(area).to_integer()
	this.h=x
	this.w=x
end property
	
method shared public Quad new(integer height=0,integer width=-1)	
	Quad me
	if height<0 then height=0 end if
	if width<0 then
		height=Math.sqrt(height).to_integer()
		width=height
	end if
	me=super()
	me.height=height
	me.width=width
	return me
end method

end class

--used thus ...

class Application extends Entity

method shared public main()
	Quad q
	q=Quad.new(7,8)
	Console.puts(q.area) -- displays 56
  	q.width=5
    Console.puts(q.area) -- displays 35
   	q.width+=1 --shorthand for q.width=q.width+1 
   	Console.puts(q.area) -- displays 42
   	Console.puts(q.height) -- displays 7
   	Console.puts(q.width) -- displays 6
	q=Quad.new(81)
	Console.puts(q.area) -- displays 81
	Console.puts(q.height) -- displays 9
   	Console.puts(q.width) -- displays 9
    q.area=49  -- will throw Access_Denied, this property has a private setter.
end method
	
end class


new topic     » topic index » view message » categorize

2. Re: Properties in Methodica

Mike Nelson wrote:

> After carefully reviewing Derek's second post, I have tried my hand at
> creating
> a good readable syntax for defining properties.

Thanks Mike. It is looking good.

I take it that the 'value' parameter for the setter method is a reserved word in
that context and is a Euphoria object datatype. That would mean that the setter
could receive a sequence and deal with as required.

property integer height
public get        
  return this.h
public set
  if sequence(value) then
    value = toInteger(value)
  end if
  if value<0 then
     throw Type_Error("Height must not be negative.")
  else
     this.h=value            
  end if
end property

foo.height = "23"


-- 
Derek Parnell
Melbourne, Australia
Skype name: derek.j.parnell

new topic     » goto parent     » topic index » view message » categorize

3. Re: Properties in Methodica

Derek Parnell wrote:

> I take it that the 'value' parameter for the setter method is a reserved word
> in that context and is a Euphoria object datatype. That would mean that the
> setter could receive a sequence and deal with as required.

Not quite what I had in mind, but a good idea: value is indeed as reseveved word
in context, but I had assumed that it would have the type of the property.
How about this variant:

property height
public integer get        
   return this.h
public any set
   if not value.is(integer) then
value=value.to_integer() -- Will throw an exception if value can't be
       converted.
   end if
   if value<0 then
       throw Type_Error.new("Height must not be negative.")
   else
       this.h=value            
   end if
end property
 
foo.height = "23"


My original syntax would still be available for the ususal case where both the
getter and the setter are using the same type. Logically it should aso be legal
to put the access specifier after "property" when the same access is desired for
both get and set.
lines 1, 2, and 4 could be written as

property public height

The type "any" is Methodica's top level generic type, for compatibility with Eu, object is the supertype of atom, sequence, and thier subtypes.

Methodica types may be value types: object, atom, sequence, string, integer, etc. or reference types (classes): Entity, Exception, etc. By convention value types start with a lowercase letter while reference types start with an uppercased letter. (The compiler will not enforce this.)

"any" is the lone type which may hold a reference or a value. }}}

new topic     » goto parent     » topic index » view message » categorize

4. Re: Properties in Methodica

Reposting after a premature send.

Derek Parnell wrote:

> I take it that the 'value' parameter for the setter method is a reserved word
> in that context and is a Euphoria object datatype. That would mean that the
> setter could receive a sequence and deal with as required.

Not quite what I had in mind, but a good idea: value is indeed as reseveved
word in context, but I had assumed that it would have the type of the property.
How about this variant:


property height
public integer get        
   return this.h
public any set
   if not value.is(integer) then
value=value.to_integer() -- Will throw an exception if value can't be
       converted.
   end if
   if value<0 then
       throw Type_Error.new("Height must not be negative.")
   else
       this.h=value            
   end if
end property
 
foo.height = "23"


My original syntax would still be available for the ususal case where both the
getter and the setter are using the same type. Logically it should aso be legal
to put the access specifier after "property" when the same access is desired
for both get and set.
lines 1, 2, and 4 could be written as

property public height
integer get
any set


The type "any" is Methodica's top level generic type, for compatibility with Eu,
object is the supertype of atom, sequence, and thier subtypes.

Methodica types may be value types: object, atom, sequence, string, integer,
etc.
or reference types (classes): Entity, Exception, etc. By convention value types
start with a lowercase letter while reference types start with an uppercased
letter. (The compiler will not enforce this.)

"any" is the lone type which may hold a reference or a value.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu