Skip to content

Improve error reporting: Accessing protected members within async blocks. #2307

Open
@isaacabraham

Description

@isaacabraham

What

In a class implementation, trying to access a protected base class member within an async block won't compile: -

    member __.Foo() = async { return base.Ok()  } // Pop

The error is: -
The member or object constructor 'Ok' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda expressions.

Why

My first question - although slightly beside the point - would be, why does this error occur (from a language point of view)?
My concern with this error is that: -

  • It's somewhat unclear what it means. Yes, I get it - but beginners will not understand that an async block might cause continuations etc. etc. which in turn are therefore forbidden.
  • It provides no suggestion to work around this, when there's a perfectly legitimate one: -
    member __.Ok() = base.Ok()
    member this.Foo() = async { return this.Ok()  } // now compiles

How

Quick win

Improve the error so when when within a CE, the error says: -
The member or object constructor 'Ok' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from within a computation expression such as Async.

Slightly more betterer win

Improve the error so when when within a CE, the error says: -
The member or object constructor 'Ok' is not accessible. Private members may only be accessed from within the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from within a computation expression such as Async. Consider creating a member in this type which explicitly calls to the protected member, and call that member from this code point instead.

Most bestest win

The code fixes are so menial and obvious, it leads to the question: why not have the compiler do it for us? The best solution to this would simply be to eliminate this (if possible, of course) by generating a member that does the delegation, thus eliminating this error completely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature ImprovementTheme-Simple-F#A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.

    Type

    No type

    Projects

    Status

    In Progress

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions