Hi,
the problem is in the "Method" body where you (as I expect) want to assign the value of the argument "arg" to the field "exampleInterfaceA" of the class. There are actually two issues, but the issue reported by the compiler is that the type of argument is "InterfaceA" while the type of field is "InterfaceA option". The types have to match, so you'll need to create a value of type "InterfaceA option", whch you can do by using "Some" constructor (see below).

The second issue with your code (if you want to assign a value to a field) is that assignment/mutation in F# is done using "<-" operator. The operator "=" just compares the values, which means that your code would just return false (and this is piped to "ignore" function, so it would be ignored). Finally, you also have to use downcast (:?>), which performs dynamic type conversion, instead of upcast (:>) which can be used statically correct conversions (e.g. from any class to "obj").

I think you wanted to write the following:

1
2
3
4
5
6
 
type test1() = 
  let mutable exampleInterfaceA:InterfaceA option = None
  interface InterfaceB with
    member x.Method(arg) = 
      exampleInterfaceA <- Some(arg :?> InterfaceA)

If you want to avoid using "option" type (in a situation where the field should always have a valid value), I would recommend creating an implicit constructor which would take the initial value as an argument (but that really depends on what the code really does, because if the value is optional you'll have to use "option" type):

1
2
3
4
5
6
 
type test1(def) = 
  let mutable exampleInterfaceA:InterfaceA = def
  interface InterfaceB with
    member x.Method(arg) = 
      exampleInterfaceA <- (arg :?> InterfaceA)

BTW: you can declare interfaces using more compact syntax - the compiler automatically infers that you're declaring interface, because it doens't have a constructor and has only abstract members:

1
2
3
4
5
 
type InterfaceA = 
  abstract getAddress : string with get,set
type InterfaceB = 
  abstract Method: obj -> unit
By on 12/9/2007 5:42 PM ()

I see!

thank you a lot!

By on 12/10/2007 12:24 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper