Author |
Message |
spongeboob
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
TheOneTrueGod
![](http://www.drmcninja.com/images/mcninjab3.jpg)
|
Posted: 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 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 |
|
|
|
|
|
![](images/spacer.gif) |
spongeboob
|
Posted: Sat Jun 03, 2006 5:37 pm Post subject: (No subject) |
|
|
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? |
|
|
|
|
![](images/spacer.gif) |
TheOneTrueGod
![](http://www.drmcninja.com/images/mcninjab3.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: 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,
at the start of the code. |
|
|
|
|
![](images/spacer.gif) |
spongeboob
|
Posted: 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!! !! Thanks for all the help you guys! I really appreciate it! |
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
|