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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:29:27+00:00 2026-06-05T03:29:27+00:00

Suppose I have a stupid little case class like so: case class Foo(name: String,

  • 0

Suppose I have a stupid little case class like so:

case class Foo(name: String, other: Foo)

How can I define a and b immutably such that a.other is b, and b.other is a? Does scala provide some way to “tie the knot”? I’d like to do something like this:

val (a, b): (Foo, Foo) = (Foo("a", b), Foo("b", a)) // Doesn't work.

Possibilities

In Haskell I would do this:

data Foo = Foo { name :: String, other :: Foo }

a = Foo "a" b
b = Foo "b" a

Where the bindings to a and b are contained in the same let expression, or at the top level.

Or, without abusing Haskell’s automagical letrec capabilities:

(a, b) = fix (\ ~(a', b') -> Foo "a" b', Foo "b" a')

Note the lazy pattern, ~(a', b'), that’s important.

  • 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-05T03:29:29+00:00Added an answer on June 5, 2026 at 3:29 am

    You want Foo to be unchanged, but laziness in Scala is on the declaration site. It is impossible for Foo to be non-strict without changing it, and the pattern indicated in Haskell only works because Foo, there, is non-strict (that is, Foo "a" b doesn’t evaluate b immediately).

    Otherwise the solution is pretty much the same, allowing for the hoops necessary to get everything non-strict:

    class Foo(name: String, other0: => Foo) { // Cannot be case class, because that mandates strictness
      lazy val other = other0 // otherwise Scala will always reevaluate
    }
    object Foo {
      def apply(name: String, other: => Foo) = new Foo(name, other)
    }
    
    val (a: Foo, b: Foo) = (Foo("a", b), Foo("b", a))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a class, User: public class User { private String name; private
Suppose I have a class like this: class MyClass { private: vector<MyOtherClass> myMember; public:
given a string literal in c++ i have to remove toxic words like stupid
Suppose I have a table like: Tab(MyDate datetime null, MyIs bit null, ...) Then
Suppose I have the following HTML: <div id=Wrapper> <div class=MyClass></div> <div class=MyClass></div> <div class=MyClass></div>
suppose I have a html markup like this: <div> <p> this is the parent
Suppose I have 20 string of different length, each of them is supposed to
Suppose I have an integer array like this: {5,3,5,4,2} and I have a method
Suppose we have the Java code: Object arr = Array.newInstance(Array.class, 5); Would that run?
Hey folks, I have a question which feels stupid but I can't quite say

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.