Problem with classes
Author |
Message |
LegendsEnd
|
Posted: Tue Jun 13, 2006 3:55 pm Post subject: Problem with classes |
|
|
code: |
class towns
export name, gold, food, population, location
var name : string
var gold, food, population : int
type location :
record
x1, y1, x2, y2 : int
end record
end towns
var town : ^towns
new town
get town -> name
|
I exported the variable name, but when I try to store something in it, it says it's read only in the scope and can't be modified by get. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Tue Jun 13, 2006 4:05 pm Post subject: (No subject) |
|
|
That's right. It is read-only. You cannot write to it.
If you want to write to it, you have to export it with the keyword var before it, just like when you add the keyword var before parameters in methods (which you never do, I'm sure ).
The other way to do it is to create the reader/writer methods yourself:
code: |
class Test
var foo := "yarr"
proc set_foo (value : string)
foo := value
end set_foo
function get_foo : string
result foo
end get_foo
end Test
|
Neither of these solutions are very pretty. They don't compare to Ruby's attr_reader, attr_writer, and attr_accessor:
Ruby: |
class Test
attr_accessor :foo
def initialize
@foo = "yarr"
end
end
|
|
|
|
|
|
|
wtd
|
Posted: Tue Jun 13, 2006 4:52 pm Post subject: (No subject) |
|
|
I'll go ya one better.
code: | (defclass test ()
((foo :initarg :foo
:initform "yarr"
:accessor foo))) |
|
|
|
|
|
|
|
|