E06180: not accessible from this context

Category: E06xxx Type hierarchy checks · Reported in phase: FULL_RESOLUTION · Classification: NOT_ACCESSIBLE · All error codes

What went wrong

You're trying to use a method, property or constructor that EXISTS but is NOT ACCESSIBLE from your current context due to visibility rules (private/protected).

A common case is a dynamic function or class BODY naming a property of the object around it (or a capture of an enclosing dynamic function). A dynamic construct is not inside that object, so its body cannot reach the object's properties directly - they are reached through a CAPTURE. The capture list is evaluated in the method that creates the dynamic construct, where the property is visible:

above()
  <- rtn as Predicate of Integer: (limit: pivot) is Predicate of Integer as pure function
    r: t > limit

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 2130 (Can a dynamic function created in a method capture the class's own private property, and does it share it?)
  • ek9 -q 2131 (How does a dynamic function take a class property as a starting value without changing the property?)
  • ek9 -q 95 (How do I control field visibility in EK9?)
  • ek9 -q 97 (What is the difference between classes and records in EK9?)
  • ek9 -q 93 (How do I define a class in EK9?)
  • ek9 -q 118 (How do I implement the builder pattern in EK9?)

How to fix it

In a dynamic function or class body:

  1. Capture the property - '(limit: pivot)' - and use the capture. It SHARES the object: a mutation ('++', '+=', a copy into it) of 'limit' moves 'pivot'. Choose this when the dynamic construct exists to update the object (a callback recording into its tally).
  2. Capture a copy - '(limit: Integer(pivot))' - when the dynamic construct only needs an initial value it then works on; 'pivot' does not move.

Elsewhere:

  1. Make method public if external access is needed
  2. Create public wrapper method that calls private method
  3. Extend the class to access protected methods
  4. Check if there's an alternative public API

Common causes

  • A dynamic function/class body naming the enclosing object's property instead of a capture of it
  • Accessing private method or property from outside the class
  • Accessing a super class's private property from a subclass (a capture of it is judged the same way)
  • Accessing protected method from non-subclass
  • Accessing internal method from different module

See also

  • E06180: member EXISTS but visibility blocks access (this error)
  • E50001: the name does not resolve at all
  • E50060: method DOESN'T EXIST at all
  • E06140: multiple methods match equally (ambiguity)

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E06180: not accessible from this context

#!ek9
defines module error.e06180.bad

  defines class

    Account as open
      balance as Integer: 0

      default private Account()

      Account()
        -> opening as Integer
        balance: opening

      protected adjust()
        -> amount as Integer
        balance += amount

      private audit()
        <- rtn as Boolean: balance >= 0

      default operator ?

    Savings extends Account

      Savings()
        //The no-argument constructor is private to Account
        super()

      check()
        <- rtn as Boolean?
        //A private method is not visible to a sub-type
        rtn: audit()

      default operator ?

    Auditor

      correct()
        -> account as Savings
        //A protected method is visible only within the hierarchy
        account.adjust(1)

    Splitter
      pivot as Integer: 10

      above()
        //A dynamic function's body is not inside Splitter: it cannot name pivot
        <- rtn as Predicate of Integer: () is Predicate of Integer as pure function
          r: t > pivot

      default operator ?

Fixed: Call what is visible from where you are: a public constructor, a protected method, a captured value

#!ek9
defines module error.e06180.good.fix

  defines class

    Account as open
      balance as Integer: 0

      default private Account()

      Account()
        -> opening as Integer
        balance: opening

      adjust()
        -> amount as Integer
        balance += amount

      protected audit()
        <- rtn as Boolean: balance >= 0

      default operator ?

    Savings extends Account

      Savings()
        //The no-argument constructor is private to Account
        super(0)

      check()
        <- rtn as Boolean?
        //A private method is not visible to a sub-type
        rtn: audit()

      default operator ?

    Auditor

      correct()
        -> account as Savings
        //A protected method is visible only within the hierarchy
        account.adjust(1)

    Splitter
      pivot as Integer: 10

      above()
        //A dynamic function's body is not inside Splitter: it cannot name pivot
        <- rtn as Predicate of Integer: (pivot) is Predicate of Integer as pure function
          r: t > pivot

      default operator ?

Fixed: Call what the context can see: a public constructor, a protected method from the sub-type, and capture the property

#!ek9
defines module error.e06180.good

  defines class

    Account as open
      balance as Integer: 0

      default private Account()

      Account()
        -> opening as Integer
        balance: opening

      protected audit()
        <- rtn as Boolean: balance >= 0

      adjust()
        -> amount as Integer
        balance += amount

      default operator ?

    Savings extends Account

      Savings()
        super(0)

      check()
        <- rtn as Boolean: audit()

      default operator ?

    Auditor

      correct()
        -> account as Savings
        account.adjust(1)

    Splitter
      pivot as Integer: 10

      //The capture list is evaluated in above(), where pivot is visible
      above()
        <- rtn as Predicate of Integer: (limit: pivot) is Predicate of Integer as pure function
          r: t > limit

      default operator ?

Questions and answers

These Q&A entries show E06180 and its fix: