Assignment in Namespace

General FreeBASIC programming questions.
Post Reply
BasicScience
Posts: 489
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Assignment in Namespace

Post by BasicScience »

Why does this generate an error:
"error 111: Illegal inside a NAMESPACE block, found '=' in 'MyArray = CALLocate (MaxArray*len(MyArray))"

Code: Select all

Namespace junk
    Const maxpt    = 100
    const maxarray = 10
          
    Type _Array
        x(maxpt) as single
        y(maxpt) as single
    end type

    Dim Shared MyArray as _Array Ptr
    MyArray = CALLocate (MaxArray*len(MyArray))

END Namespace

But this is OK?

Code: Select all

Namespace junk
    Const maxpt    = 100
    const maxarray = 10
          
    Type _Array
        x(maxpt) as single
        y(maxpt) as single
    end type

END Namespace

    Dim Shared MyArray as _Array Ptr
    MyArray = CALLocate (MaxArray*len(MyArray))
    
  
If it's illegal to assign a variable within a Namespace block definition, then why?

Thanks
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Assignment in Namespace

Post by dkl »

Namespaces aren't allowed to contain code directly, only inside procedures declared in that namespace. That's because a namespace isn't a scope, it's not something that is executed, it's just something that can be used to hold declarations.

Sure enough an assignment is code that needs to be executed. If written at the toplevel, outside the namespace, it becomes part of the implicit main() procedure (which is one of FB's quirky specialties). Note that global constructors are executed before the main() procedure, so code in such constructors may find that global variables aren't set up yet if that setup is done through code in main().
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Assignment in Namespace

Post by fxm »

Any variable declared in namespace is implicitely static and visible throughout the entire program ('Shared' is useless). Therefore only an initializer with a constant is authorized:

Code: Select all

Namespace N
  Dim As Integer I = 123 ''authorized
  'I = 456                ''forbidden
End Namespace
You forgot the namespace's name prefix in your second example (or the 'Using' keyword):

Code: Select all

Namespace junk
    Const maxpt    = 100
    const maxarray = 10
         
    Type _Array
        x(maxpt) as single
        y(maxpt) as single
    end type

END Namespace

    Dim Shared MyArray as junk._Array Ptr
    MyArray = CALLocate (junk.MaxArray*len(MyArray))
BasicScience
Posts: 489
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Re: Assignment in Namespace

Post by BasicScience »

Thanks to you both (@fxm and @dkl)

So following the (corrected) example by fxm, what's the best method to have MyArray be a part of the JUNK namespace but not Global?
BasicScience
Posts: 489
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Re: Assignment in Namespace

Post by BasicScience »

looks like this works. Is it Kosher?

Code: Select all

Namespace junk
    Const maxpt    = 100
    const maxarray = 10
          
    Type _Array
        x(maxpt) as single
        y(maxpt) as single
    end type
    
    Dim MyArray as _Array ptr
    
END Namespace

Junk.MyArray = CALLocate (junk.MaxArray*len(Junk._Array))

With Junk.MyArray[0]
    .x(0) = 1
    .y(0) = 10
    print  .x(0) & "," & .y(0)
end with

sleep

fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Assignment in Namespace

Post by fxm »

If MyArray is declared in the JUNK namespace, it is mandatory global.
BasicScience
Posts: 489
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Re: Assignment in Namespace

Post by BasicScience »

@FXM, I don't understand. Here's an example that shows MyArray is NOT global (which is what I want).

Code: Select all

Namespace junk
    Const maxpt    = 100
    const maxarray = 10
          
    Type _Array
        x(maxpt) as single
        y(maxpt) as single
    end type
    
    Dim MyArray as _Array ptr
    
END Namespace

Junk.MyArray = CALLocate (junk.MaxArray*len(Junk._Array))

With Junk.MyArray[0]
    .x(0) = 1
    .y(0) = 10
    print  .x(0) & "," & .y(0)
end with

print MyArray[0].x(0)    'THIS LINE CAUSES an ERROR, Variable MyArray not declared

sleep
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Assignment in Namespace

Post by fxm »

Junk.MyArray is global (static and shared).
BasicScience
Posts: 489
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Re: Assignment in Namespace

Post by BasicScience »

Yes, I agree with you. junk.MyArray is global (static and shared) in this example

The point I was trying to verify is that the variable name MyArray is local to the JUNK namespace. MyArray is not declared outside JUNK and therefore is not encroaching on a possible conflict with another variable in MAIN() that might be declared as MyArray.

I'm making a library, and I don't want to begin using up variable names that might cause a conflict when linked to some other program.

Thanks again.
Post Reply