
-----------------------------------
spongeboob
Sat Jun 03, 2006 5:12 pm

Record types in classes
-----------------------------------

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?

-----------------------------------
TheOneTrueGod
Sat Jun 03, 2006 5:19 pm


-----------------------------------
Just because they are named the same, doesn't mean they are the same thing :P  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)
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
Sat Jun 03, 2006 5:37 pm


-----------------------------------
:D 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
Sat Jun 03, 2006 5:55 pm


-----------------------------------
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
Sat Jun 03, 2006 6:02 pm


-----------------------------------
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.

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 ".tu". Then in your main program,

import MyClassName

at the start of the code.

-----------------------------------
spongeboob
Sat Jun 03, 2006 6:27 pm


-----------------------------------
So this is what I did

"testType.tu"

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"

import testType
var wtf1 : ^testType
new testType, wtf1

var newTest : testType.test

newTest := testType(wtf1).lol ("wtf")
put newTest.name


and...it works!!  :D  !! Thanks for all the help you guys! I really appreciate it!

-----------------------------------
Cervantes
Sat Jun 03, 2006 6:54 pm


-----------------------------------
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.
