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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:13:26+00:00 2026-05-23T14:13:26+00:00

i have a class with a method that returns a function object. the requirements

  • 0

i have a class with a method that returns a function object. the requirements are that the method is arity 0 and its return type is an Option of a Function0 – whose return type is the original return type. for example:

class MyClass {
  def f(): Option[Function[A]] = Some(g _)
  def g(): Option[Function[A]] = Some(h _)
  ... goes on and on, eventually returns Some(z _) ...
  def z(): Option[Function[A]] = None
}

Due to the recursion, the issue is that the type A is defined as:

type A = Option[Function0[A]]

but since cyclic references are not allowed, it produces the error:

illegal cyclic reference involving type A

i obviously want to avoid defining a different return type for each of the methods, but having a unified return type doesn’t seem possible due to the cycle. is there any way to do this? thanks!

  • 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-05-23T14:13:27+00:00Added an answer on May 23, 2026 at 2:13 pm

    This is not supported by Scala type definitions. See How to define a cyclic type definition?

    You can accomplish this with a class instead of a type, although you would have to define your own Option-like class. An example is below (with monadic methods omitted). Also, see this mailing list discussion: http://www.scala-lang.org/node/2541

    sealed trait OptionalFunction extends (() => OptionalFunction) {
      def get: (() => OptionalFunction)
    }
    case class SomeFunction(fn: () => OptionalFunction) extends OptionalFunction {
      def apply() = fn()
      def get = fn
    }
    case object NoFunction extends OptionalFunction {
      def apply() = this
      def get = throw new NoSuchElementException
    }
    
    class MyClass {
      type A = OptionalFunction
      def f(): A = SomeFunction(g _)
      def g(): A = SomeFunction(h _)
      def h(): A = SomeFunction(() => { println("At the end!"); i })
      def i(): A = NoFunction
    }
    
    scala> new MyClass().f()()()()
    At the end!
    res0: OptionalFunction[Unit] = <function0>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class method that returns a list of employees that I can iterate
I have a static method that takes a parameter and returns a class. the
I have a class that has an output() method which returns a matplotlib Figure
I have a C# class method that return a xml document not file. How
I have an Action Method that returns a JSON-serialized object. The problem I'm having
So in writing a C++ template class, I have defined a method that returns
I have a class with some method that depend by one parameter. What is
I have a class with a ToString method that produces XML. I want to
I have a .NET class library containing a class with a method that performs
I have a method that takes a list of entities ( Class es) and

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.