Declare a global variable in boo

467 views Asked by At

As far as I can tell from the website, the following code should compile to a DLL with a top-level property Version:

namespace MyLibrary

[Module]
class MainClass:
    public static Version as string

    static def constructor():
        Version = "0.1"

This compiles, but if I then enter these commands into booish:

import MyLibrary
print (Version)

then I get "ERROR: Unknown identifier: 'Version'".

Presumably this code worked in an earlier version of the language. I am using 0.9.4.9. What is the correct way to achieve this effect?

(I've noticed that there is an implicit static class MyVersionModule in which top-level static methods get placed, but I don't know how to add properties to this either).

1

There are 1 answers

0
justin.m.chase On

In .net there is no way to have methods or fields that are not actually members of a class. Boo hides this somewhat by having the implicit class for the main file in the module (as you noticed) but when importing you still need to access it as a member.

For statics you have to first reference the type then the member so in your example printing the version would be like this:

import MyLibrary
print (MainClass.Version)

Of course this isn't the 'correct' way to store version information in .net though, which is to use assembly level attributes instead. That would look more like this:

[assembly: System.Reflection.AssemblyVersion("1.0")]

namespace MyLibrary

[Module]
class MainClass:
  pass

Then getting the version you would do using reflection, there are a couple of ways to get the assembly but the easiest is to get the Type then it's assembly:

import System
import System.Reflection
import MyLibrary

atts = typeof(MainClass).Assembly \
         .GetCustomAttributes(typeof(AssemblyVersionAttribute), false)

version = (atts[0] as AssemblyVersionAttribute).Version
print(version)