Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Problem with classes
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
LegendsEnd




PostPosted: 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
Sponsor
sponsor
Cervantes




PostPosted: 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 Wink).

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




PostPosted: Tue Jun 13, 2006 4:52 pm   Post subject: (No subject)

I'll go ya one better. Wink

code:
(defclass test ()
  ((foo :initarg :foo
        :initform "yarr"
        :accessor foo)))
 
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: