User-Created Types Across Multiple Files
Author |
Message |
Dusk Eagle
|
Posted: Fri Jun 05, 2009 8:15 am Post subject: User-Created Types Across Multiple Files |
|
|
Hey guys, I am having a problem with using user-created types across multiple files and it would be great if you could help me out. I realize the easy and obvious solution is to not use user-created types and to just use a bunch of variables, but I would really prefer it if I could still use my user type.
What is it you are trying to achieve?
I am trying to make a user-created type to use across multiple files.
What is the problem you are having?
Unfortunately, Turing does not recognize that "Type" in File 1 is the same "Type" in File 2.
Describe what you have tried to solve this problem
I have tried creating a middleman file that only "includes "Coord.t"", but that did not work any better. I also tried asking my teacher to help me but he couldn't solve my problem either.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Note: the same three files can be found in a .zip archive below if you prefer.
File 1 (the type file):
Turing: |
type Coord :
record
x, y : int
end record
|
File 2 (a module that requires a variable of type Coord to be passed into it):
Turing: |
unit
module L
export setPosition
include "Coord.t"
proc setPosition (position_ : Coord)
put position_.x
end setPosition
end L
|
File 3 (the main program i.e. the one to execute):
Turing: |
import "L.t"
include "Coord.t"
var coordinate : Coord
coordinate.x := 5
coordinate.y := 6
L.setPosition (coordinate)
|
Please specify what version of Turing you are using
4.1
Description: |
Same three files as above. |
|
Download |
Filename: |
user_created_types.zip |
Filesize: |
571 Bytes |
Downloaded: |
43 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Fri Jun 05, 2009 10:11 am Post subject: RE:User-Created Types Across Multiple Files |
|
|
I think what's happening here is that you've got two different includes that define the Coord type, each in a different scope (one is in the main scope, in your Main.t file, where it is included at the top of the file; the other is inside the module L, and is therefore not accessible to the outside).
I don't know offhand how to solve the problem if you require that L remain a module. If you're willing to change L to a class, the problem becomes trivial - just include "Coord.t" at the top of L.t and then include "L.t" at the top of Main.t - this gives you the same Coord type in L.t as you have in Main.t.
|
|
|
|
|
|
Geniis
|
Posted: Fri Jun 05, 2009 5:57 pm Post subject: Re: User-Created Types Across Multiple Files |
|
|
Something you could do would be to include it only in your module and since you can export types from a module you could export it with ~. and use it like any other type.
example :
Turing: |
unit
module L
export setPosition, ~.Coord % Adding the ~. in front of something you want to export from your module makes it so you do not need to type the module name first
include "Coord.t"
proc setPosition (position_ : Coord)
put position_.x
end setPosition
end L
|
then you could use it like this :
Turing: |
import L
var myVar : Coord %instead of L.Coord because of the ~. in front of Coord in the export list
myVar.x := 55
L.setPosition (myVar)
|
|
|
|
|
|
|
Dusk Eagle
|
Posted: Mon Jun 08, 2009 7:56 am Post subject: Re: User-Created Types Across Multiple Files |
|
|
Well sorry about the delay in replying but I have had a busy weekend.
DemonWasp, I like your idea, but I simplified my problem before posting it to this board. I need other classes to use Coord as well, so your idea probably will not work.
Geniis, your idea looks pretty good, but it may suffer from the same problems that DemonWasp's does. I also can't seem to get it to work, though I haven't spent a long time trying to implement it. What I'm going to do instead is break up Coord into two separate variables - x and y - and simply pass them as two parameters instead of one.
|
|
|
|
|
|
|
|