Click here to Skip to main content
15,881,681 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a small piece of code in F#. The idea is that I would have an interface which defines a few methods, properties, and events. Using this interface I want to define an abstract class (having a generic type) which would implement some properties/methods but not all of the members defined in the interface. The responsibility to define all of the members would be on the derived classes.

So far I'm stuck with the interface and the abstract class. The problems I'm having:
- The abstract class definition does not recognize the interface definition
- I'm not sure how to define the abstract members for the interface in the abstract class

The interface compiles fine but the abstract class has several errors. The messages are included in code

All comments would be appreciated. Since this is my first trial on F# there are probably a lot of mistakes to point out :)

What I have tried:

Interface definition
F#
namespace A
    type IValueItem =
        interface    
            [<CLIEvent>]
            abstract member PropertyChanged : 
                Control.IEvent<System.ComponentModel.PropertyChangedEventHandler, 
                            System.ComponentModel.PropertyChangedEventArgs>
      
            abstract ConvertedX : double with get, set

            abstract Y : double with get, set

            abstract member CreateCopy : obj

            abstract member NewTrendItem : obj
        end

And the abstract class
F#
namespace A
    [<AbstractClass>]
    type ValueItem<'TX>() =
    
        [<DefaultValue>]
        val mutable _y : double

        let _propertyChanged = new Event<_>()
        
        // ERROR: This type is not an interface type
        // ERROR: The type 'obj' is not an interface type
        // ERROR: The type 'IValueItem' is not defined
        interface IValueItem with

            // ERRROR No abstract or interface member was found that corresponds to this override	
            [<CLIEvent>]
            member this.PropertyChanged : 
                Control.IEvent<System.ComponentModel.PropertyChangedEventHandler, 
                                System.ComponentModel.PropertyChangedEventArgs> 
                = _propertyChanged.Publish

            // This definition is incomplete, should be abstract
            member ConvertedX : double with get, set
    
            // ERROR: Incomplete structured construct at or before this point in pattern
            member CreateCopy() : obj

            member NewTrendItem() : obj
      
        abstract X : 'TX with get, set

        member this.Y
            with get() = this._y
            and set(value) =
                if this._y <> value then
                    this._y <- value
                    this.NotifyPropertyChanged("Y")

        member this.NotifyPropertyChanged(propertyName) =
                this.PropertyChanged.Trigger(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName))
Posted
Updated 3-Jun-16 22:41pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900