user-8564775
user-8564775

Reputation: 503

F# Public Override of Protected Methods

I've just started learning F# and I'm using it with Monogame to create a simple game to help myself learn the various features of the language. I've hit a roadblock trying to access my game class from outside because the Monogame Game class defines the methods as protected. Trying to do a public override of the method throws an error, telling me setting an accessibility modifiers are not permitted.


Base Class Defined in C#

public class Base
{
    protected virtual void Method()
    {
        //...
    }
}

Public Override in F#

type Game as game = 
    inherit Base()

    //Error: Accessibility modifiers are not permitted on overrides or interface implementations
    override public game.Method = 
        //...
        ()

Q: What is the correct way to do a public F# override of an inherited protected C# method?


Upvotes: 4

Views: 1240

Answers (1)

user-8564775
user-8564775

Reputation: 503

Changing accessibility on overrides is not allowed, in either F# or C#. Based on @Endjru's, the best approach is to use a wrapper method that is public to call the protected method;

type Game as game = 
    inherit Base()

    override game.Method = 
        //...
        ()

    member game.PublicMethod = game.Method

Upvotes: 4

Related Questions