Computer Science Canada

Need Help With Import/Export var

Author:  Anonymous [ Mon May 15, 2006 12:26 pm ]
Post subject:  Need Help With Import/Export var

I know how to export procedures, but when I try to export a variable I get an error "exported symbol 'drive' has not been declared".

Here is the code which I use the import:
code:

import GUI, Determine in "Drive Determine.tu", Drive in "Drive Determine.tu", var drive in "Drive Determine.tu"


Now here is the module:
code:

unit
module Determine
    export Drive, var drive

    procedure Drive
        var computerLib := "KLCVI-LIB-"
        var drive : string

        if (Sys.GetUserName = "Drew Martell") then %tells if im on my home computer
            drive := "F"

        elsif (Sys.GetUserName = "martelld") then %tells if im at school
            drive := "D"

        elsif (Sys.GetComputerName = computerLib + "01") or
                (Sys.GetComputerName = computerLib + "02") or
                (Sys.GetComputerName = computerLib + "03") or
                (Sys.GetComputerName = computerLib + "04") or
                (Sys.GetComputerName = computerLib + "05") or
                (Sys.GetComputerName = computerLib + "06") or
                (Sys.GetComputerName = computerLib + "07") or
                (Sys.GetComputerName = computerLib + "08") or
                (Sys.GetComputerName = computerLib + "09") or
                (Sys.GetComputerName = computerLib + "10") or
                (Sys.GetComputerName = computerLib + "11") or
                (Sys.GetComputerName = computerLib + "12") or
                (Sys.GetComputerName = computerLib + "13") then
            drive := 'E'       
        end if   
       
    end Drive

end Determine


I know this unit is useless since Turing does it for you, but I want to know how to import variables.

Author:  NikG [ Mon May 15, 2006 2:57 pm ]
Post subject: 

First in the module, I believe you can't export the variable when it's inside a procedure, only when it's declaredt outside.

Second, are you sure it should be "export var drive"?
shouldn't you leave the "var" out? (that's just a guess, anyone want to correct me?)

Author:  Anonymous [ Mon May 15, 2006 4:27 pm ]
Post subject:  k

Ill try declairing outside then importing...

Author:  Anonymous [ Mon May 15, 2006 4:42 pm ]
Post subject: 

Now it says "Unit symbol is determine, not drive" when I place it next to the other imports. This is hard =(

Author:  Anonymous [ Mon May 15, 2006 4:43 pm ]
Post subject: 

Meh nvm, it's not that important. I think its because when I import it, it then attatches itself to a string when its a char or int or something and gives me an error. Thanks anyways.

Author:  NikG [ Mon May 15, 2006 4:56 pm ]
Post subject: 

code:
% Determine.tu
unit
module Determine
    export Drive

    fcn Drive : string
        var computerLib := "KLCVI-LIB-"
        var drive : string

        if (Sys.GetUserName = "Drew Martell") then %tells if im on my home computer
            drive := "F"
        elsif (Sys.GetUserName = "martelld") then %tells if im at school
            drive := "D"
        elsif (Sys.GetComputerName = computerLib + "01") or
                (Sys.GetComputerName = computerLib + "02") or
                (Sys.GetComputerName = computerLib + "03") or
                (Sys.GetComputerName = computerLib + "04") or
                (Sys.GetComputerName = computerLib + "05") or
                (Sys.GetComputerName = computerLib + "06") or
                (Sys.GetComputerName = computerLib + "07") or
                (Sys.GetComputerName = computerLib + "08") or
                (Sys.GetComputerName = computerLib + "09") or
                (Sys.GetComputerName = computerLib + "10") or
                (Sys.GetComputerName = computerLib + "11") or
                (Sys.GetComputerName = computerLib + "12") or
                (Sys.GetComputerName = computerLib + "13") then
            drive := 'E'
        else
            drive := "NF"
        end if

        result drive
    end Drive
end Determine

code:
import Determine in "Determine.tu"
put Determine.Drive

That works, I just changed Drive to a function.

Yay, I learned something new.

Author:  Cervantes [ Mon May 15, 2006 5:18 pm ]
Post subject:  Re: Need Help With Import/Export var

vahnx wrote:

Here is the code which I use the import:
code:

import GUI, Determine in "Drive Determine.tu", Drive in "Drive Determine.tu", var drive in "Drive Determine.tu"


You should only be importing the module, as NikG showed. By importing the module into your program, you're also importing the public variables of that module (the variables that the module exported).

Exporting it with the var keyword previously makes it writable. That is, you can assign it a new value from outside the module. This is often a bad idea.

Author:  Delos [ Mon May 15, 2006 6:11 pm ]
Post subject:  Re: Need Help With Import/Export var

Cervantes wrote:
vahnx wrote:

Here is the code which I use the import:
code:

import GUI, Determine in "Drive Determine.tu", Drive in "Drive Determine.tu", var drive in "Drive Determine.tu"


You should only be importing the module, as NikG showed. By importing the module into your program, you're also importing the public variables of that module (the variables that the module exported).

Exporting it with the var keyword previously makes it writable. That is, you can assign it a new value from outside the module. This is often a bad idea.


Though as we all know, Turing's treatment of read-only variables is, uh, a facade. A total sham! It will give you a warning indicating that read-onlies cannot be modified externally, and then promptly proceed to modify them!
Ah well...

Author:  NikG [ Mon May 15, 2006 6:44 pm ]
Post subject: 

Ahhhh shoot!

The problem vahnx was having, and I wasn't able to figure out until just a minute ago, was simply that the drive variable he had in his original code was part of Determine!

So you can't just call it with "put drive", you have to use "put Determine.drive"

Author:  Cervantes [ Mon May 15, 2006 7:17 pm ]
Post subject:  Re: Need Help With Import/Export var

Delos wrote:
Though as we all know, Turing's treatment of read-only variables is, uh, a facade. A total sham! It will give you a warning indicating that read-onlies cannot be modified externally, and then promptly proceed to modify them!
Ah well...

That's not so bad. In Ruby, you get the same functionality with constants. You can even avoid getting the warning by doing a
code:

Object.instance_eval {remove_const :Constant_name}


Yeah, non-constant constants are kinda wierd, but you don't see me complaining. Smile

Author:  Anonymous [ Tue May 16, 2006 9:34 pm ]
Post subject:  Learned a new thing

Yay functions =)


: