Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9133641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:31:23+00:00 2026-06-17T08:31:23+00:00

i needed two instances that has access to each other privates. i naturaly thought

  • 0

i needed two instances that has access to each other privates. i naturaly thought of a companion object that grants access to a one and only instance of it’s companion class. the class itself i made private, so users cannot just create instances using new.

object A {
    def apply = dual
    lazy val dual = new A
}

private class A {
    //some irrelevant logic...
}

this code does not compile. i get: class A escapes its defining scope as part of type A error, which i don’t really understand. my current workaround was to define a trait with every method declaration the class should have and make class A extend that trait, while dual is of the trait type, and not class A type.

what’s the theoretic problem i’m missing here? why is this forbiden?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:31:24+00:00Added an answer on June 17, 2026 at 8:31 am

    Paolo’s solution is good (+1), but he didn’t explain the error message, so let me try that. The problem stems from the fact that every method needs a return type. Your original definition of apply and dual returned an object of class A, thus the implicit return type of both was A. That implies that A must be visible to clients – how else could they call the function or access the val? Moreover, as both – and their parent object too – are public, they are globally visible. However, you declared A private which means it must not be visible outside its package. So there is a conflict which can’t be resolved by the compiler.

    The general rule is that all parameter and return type of functions / members must have (at least) the same scope of visibility as the referring member itself*. Thus one trivial way to solve this problem would be to reduce the visibility of apply and dual to private. This would satisfy the compiler, but not you 🙂

    Your solution gets around the problem by changing the static return type to a public trait, which thus has the same visibility as the members referring to it. The dynamic type of the returned object is still class A, however, this need not be visible to clients. This is a classic example of the principle “program to interfaces, not implementations”.

    Note that to apply this principle to the full extent, one could turn class A into a private inner class of object A, thus making it innaccessible even for other classes within the same package:

    trait A {
        //...
    }
    
    object A {
        def apply: A = dual
        lazy val dual: A = new AImpl
    
        private class AImpl extends A {
            //some irrelevant logic...
        }
    
    }
    

    * To be pedantic, the enclosing class / object may reduce the visibility of its members, like here:

    private class Holder {
      def member = new Hidden
    }
    
    private class Hidden
    

    where member is public but its enclosing class is private, effectively hiding its members from the external world. So the compiler emits no complaints here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I inherited some code that has two non-UI threads that update various WinForm controls.
I needed to build an Android app that lets you view a web page
We have a web app (ASP) that has workflow in it for management to
Let’s say that I have two C# applications - game.exe (XNA, needs to support
I'll try and make this clear; I've got two classes; GPU(Object) , for general
I’m fairly new to OO. If I have two classes A and B that
I know that I can create two different borders for my item groups and
So, originally, I was pushing intents that needed to be sent across my application
I have some C++ code that defines two classes, A and B. B takes
I have a MySQL table called items that contains thousands of records. Each record

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.