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

Username:   Password: 
 RegisterRegister   
 Record types in classes
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
spongeboob




PostPosted: Sat Jun 03, 2006 5:12 pm   Post subject: Record types in classes

code:

class wtf
    export lol
    type test :
        record
            name : string
        end record

    var newTest : test

    function lol (name : string) : test
        newTest.name := name
        result newTest
    end lol

end wtf

var wtf1: ^wtf
new wtf, wtf1

type test :
    record
        name : string
    end record

var newTest : test

newTest:=wtf(wtf1).lol ("wtf")
put newTest.name


The code returns the error "Assigned value is the wrong type"
How can it be fixed so that it works?
Sponsor
Sponsor
Sponsor
sponsor
TheOneTrueGod




PostPosted: Sat Jun 03, 2006 5:19 pm   Post subject: (No subject)

Just because they are named the same, doesn't mean they are the same thing Razz The two "test" types you created are separate entities of each other, and therefore aren't associated with each other. If you make test global, I believe this will fix the problem. (Someone correct me if theres a better way)
code:
type test :
    record
        name : string
    end record

class wtf
    import test
    export lol

    var newTest : test

    function lol (name : string) : test
        newTest.name := name
        result newTest
    end lol

end wtf

var wtf1: ^wtf
new wtf, wtf1

var newTest : test

newTest:=wtf(wtf1).lol ("wtf")
put newTest.name
spongeboob




PostPosted: Sat Jun 03, 2006 5:37 pm   Post subject: (No subject)

Very Happy That seems to work
but what if the class is a different file and I want import it

How would I declare the type as a "global" then?
TheOneTrueGod




PostPosted: Sat Jun 03, 2006 5:55 pm   Post subject: (No subject)

Well, off the top of my head, you could probably put it in a module in the other file (I'm not sure if this would work or not) and use it in your main code and in the other file. You'd have to test it out though.
Cervantes




PostPosted: Sat Jun 03, 2006 6:02 pm   Post subject: (No subject)

Your type is global. It is not, however, pervasive. This means that you have to manually import it into classes and modules.

If you want to make it pervasive, define it with the keyword pervasive or with an asterisk (*). Here's an example for creating a pervasive variable.
code:

var pervasive foo : int
var *foo : int

Similarly for a type.

If the class is in a different file and you want to import it, add the keyword unit to the start of that file and make sure that file only contains the class definition. Save the file as "<class name>.tu". Then in your main program,
code:

import MyClassName

at the start of the code.
spongeboob




PostPosted: Sat Jun 03, 2006 6:27 pm   Post subject: (No subject)

So this is what I did

"testType.tu"
code:

unit
class testType
    export test,lol
    type * test :
        record
            name : string
        end record

    var test1 : test

    function lol (name : string) : test
        test1.name := name
        result test1
    end lol

end testType


"whatever.t"
code:

import testType
var wtf1 : ^testType
new testType, wtf1

var newTest : testType.test

newTest := testType(wtf1).lol ("wtf")
put newTest.name


and...it works!! Very Happy !! Thanks for all the help you guys! I really appreciate it!
Cervantes




PostPosted: Sat Jun 03, 2006 6:54 pm   Post subject: (No subject)

You probably shouldn't be declaring a pervasive variable or type from within a class. The only things that should be pervasive are things that are truly global, like the gravitational constant of the universe. Global variables are typically a bad idea.

I recommend you either declare the type outside the class and import it or declare it inside the class and export it. Probably the former.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: