1. Need Help Creating include files.

Hi all,

I need to know what I am doing wrong here:

I created an include file called var.e with the following type definitions.

-- var.e include file
-- Custom type definitions


type POSITIVE(atom x)
    return x >= 0
end type

type NEGATIVE(atom x)
    return x < 0
end type


Then I included the file var.e in my program like so:

include get.e
include var.e -- the include file that I created.

atom sum
POSITIVE pos_num
NEGATIVE neg_num
sequence press


pos_num = prompt_number("\nEnter a positve number only!!!: ", {})
neg_num = prompt_number("\nEnter a negaitve number only!!!: ", {})

sum = pos_num + neg_num

puts(1,"\n" & "Thank You. The positive number you entered was ")
? pos_num

puts(1,"\n" & "Thank You. The negative number you entered was ")
? neg_num

puts(1,"\n" & "The result of adding the numbers is ")
? sum

press = gets(0)

When I try to execute the prog, I get this error:

numinp2.ex:29
POSITIVE has not been declared 
POSITIVE pos_num
       ^

What am I missing?  I'm brand new to programming in Euphoria.  
I'm using ver 2.4 on a Win XP machine, running in a DOS window.

I put the file var.e in the euphoria\include directory.  
I set the environment variable and path according to instructions found on 
the web site.

Any help would be appreciated.

Thanks

Martin

new topic     » topic index » view message » categorize

2. Re: Need Help Creating include files.

Martin Berger wrote:
> 
> Hi all,
> 
> I need to know what I am doing wrong here:
> 
> I created an include file called var.e with the following type definitions.
> 
> -- var.e include file
> -- Custom type definitions
> 
> 
> type POSITIVE(atom x)
>     return x >= 0
> end type
> 
> type NEGATIVE(atom x)
>     return x < 0
> end type

These need to be declared as global if you want to use them outside of
the file in which they are declared.

global type POSITIVE(atom x)
    return x >= 0
end type
 
global type NEGATIVE(atom x)
    return x < 0
end type


A type has scope just like a function, and can be called just like a 
regular function.

Matt Lewis

new topic     » goto parent     » topic index » view message » categorize

3. Re: Need Help Creating include files.

Matt

Let me see if I understand,  Any function or type I put in an include file 
has to be declared as a global?

Martin

new topic     » goto parent     » topic index » view message » categorize

4. Re: Need Help Creating include files.

Martin Berger wrote:
> 
> Hi all,
> 
> I need to know what I am doing wrong here:
> 
<snip>
> What am I missing?  I'm brand new to programming in Euphoria.  
> I'm using ver 2.4 on a Win XP machine, running in a DOS window.
> 
> I put the file var.e in the euphoria\include directory.  
> I set the environment variable and path according to instructions found on 
> the web site.
> 
> Any help would be appreciated.
> 
> Thanks
> 
> Martin
> 

See section 2.4.2 (Scope) of the reference manual (Language Definition):

When you 'include' a Euphoria file in a main file (see 2.6 Special Top-Level
Statements), only the variables and routines declared using the 'global' 
keyword are accessible or even visible to the main file. The other, non-
global, declarations in the included file are forgotten at the end of the 
included file, and you will get an error message, "not declared", if you try
to use them in the main file. 

So to fix your problem, do this:
-- var.e include file
-- Custom type definitions

global type POSITIVE(atom x)
    return x >= 0
end type

global type NEGATIVE(atom x)
    return x < 0
end type


-- Brian

new topic     » goto parent     » topic index » view message » categorize

5. Re: Need Help Creating include files.

Martin Berger wrote:
> 
> Matt
> 
> Let me see if I understand,  Any function or type I put in an include file 
> has to be declared as a global?

Only if you want to allow code from other files to be able to use it.  I 
know, it sounds like you'll want to make everything global, but as you 
write more complex include files, there will inevitably be plenty of 
stuff that you don't want to be global.  In general, it's a good idea to
keep as much local as possible, especially variables.

Matt Lewis

new topic     » goto parent     » topic index » view message » categorize

6. Re: Need Help Creating include files.

To Matt & Brian:

Thanks for responding, problem solved!!!!

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu