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 6567485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:20:32+00:00 2026-05-25T14:20:32+00:00

I have some code that has the invariant that an object must be constructed

  • 0

I have some code that has the invariant that an object must be constructed in the function where it is ultimately used (for various reasons related to global state that aren’t ideal but are part of the assumption).

e.g. Suppose there is the function boo below that is responsible for manipulating moo.

def boo(mooGen: () => Moo) {
  val m = mooGen() // a new MOO must be created HERE
  m.moo()
}

Clients of boo that want to use boo must pass in a type of () => Moo, where the function generates the desired Moo.

Ideal client behavior:

boo( () => new Moo(// specific parameters here) )

The Moo is not created until inside the boo body.

However, a client can easily misstep with the following code:

val myMoo = new Moo(// specific parameters here)
boo( () => myMoo)

This breaks the invariant where we want the moo construction to occur only in boo.

So basically, I want to determine if the return value of mooGen is created within the call stack of the function or whether it was created beforehand.

There are many ways to verify this at runtime. However, is there any way to force this pattern at compile time? Using implicits or anything else clever?

Any ideas are appreciated!

  • 1 1 Answer
  • 3 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-05-25T14:20:33+00:00Added an answer on May 25, 2026 at 2:20 pm

    Put boo and Moo into their own object, together with a Token class, that can’t be instantiated outside the object.

    scala> object Foo {
         | class Moo(token:Token) {}
         | class Token private[Foo]()
         | def boo(mooGen: (Token) => Moo) {val m = mooGen(new Token)}
         | }
    defined module Foo
    

    Now what you want can be done:

    scala> Foo.boo(new Foo.Moo(_))
    

    And what you don’t want can’t be done:

    scala> val mymoo = new Foo.Moo(new Foo.Token)
    <console>:8: error: constructor Token in class Token cannot be accessed in objec
    t $iw
           val mymoo = new Foo.Moo(new Foo.Token)
                               ^
    

    But if the client really wants to he can – unfortunately – still get his Moo:

    val ireallywantone = new Foo.Moo(null.asInstanceOf[Foo.Token])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some older (broken) code that has a join using a *= table1.ID
I have a standalone Java app that has some licensing code that I want
We have a system that has some Bash scripts running besides Java code. Since
I have some code that has generic references in it and my IBM RAD
I have some code that locates all the contact folders that a user has
I have some code that works like the advised use of TransactionScope, but has
I have some code that enumerates over an object and records any errors it
I have some code that I had to write to replace a function that
I have some code that assigns an object to a generic id variable and
I have some code that has a dynamic-class system in C++ that has a

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.