tags:

views:

133

answers:

3

I have the following C# code.

namespace MyMath {
    public class Arith {
        public Arith() {}
        public int Add(int x, int y) {
            return x + y;
        }
    }
}

And I came up with the F# code named testcs.fs to use this object.

open MyMath.Arith
let x = Add(10,20)

When I run the following command

fsc -r:MyMath.dll testcs.fs

I got this error message.

/Users/smcho/Desktop/cs/namespace/testcs.fs(1,13): error FS0039: The namespace 'Arith' is 
not defined

/Users/smcho/Desktop/cs/namespace/testcs.fs(3,9): error FS0039: The value or constructor 
'Add' is not defined

What might be wrong? I used mono for .NET environment.

+4  A: 

Since Arith is a class and not a namespace, you can't open it. You can do this instead:

open MyMath
let x = Arith().Add(10,20)
kvb
@kvb: It works fine. Thanks.
prosseek
+7  A: 

try

open MyMath
let arith = Arith() // create instance of Arith
let x = arith.Add(10, 20) // call method Add

Arith in your code is class name, you cannot open it like namespace. Possibly you are confused with ability to open F# modules so its functions can be used without qualification

desco
@desco : It works fine. Thanks.
prosseek
+3  A: 

With open, you can only open namespaces are modules (similarly to the C# using keyword). Namespaces are defined with the namespace keyword, and act the same in both C# and F#. However, modules are in fact just static classes, having only static members - F# just hides that from you.

If you look at an F# code with the reflector, you'll see that your module has been compiled as a static class. For this reason you can only use static classes as modules in F#, and in your example, the class is not static, so in order to use it, you have to create an object instance - just like you'd do in C#.

ShdNx