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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:04:03+00:00 2026-06-15T17:04:03+00:00

I want to return a type from a function. For example: class Super case

  • 0

I want to return a type from a function. For example:

class Super
case class One(a: Int) extends Super
case class Two(b: Float) extends Super
case class Unknown extends Super

def decide(criterion: String): ??? = {
  criterion match {
    case "one" => One
    case "two" => Two
    case _ => Unknown
  }
}

So I want to return the type itself, to store it in a Map so that I could apply it somewhere later:

val test = Buffer(
  ("ahaha" -> "one")
  ("ohoho" -> "two")
  ("lalala" -> "one")
)

var map = scala.collection.mutable.Map[String, Super]()

test.map {pair =>
  map(pair._1) = decide(pair._2)
}

So that later I could like:

def act(code: String) {
  map(code) match {
    case One => doSmth[One]()
    case Two => doSmth[Two]()
    case _ => doNothing()
  }
}

I know that some parts, like the unused parameters of the case classes may seem strange here, but this is how it is in the environment I am working in, and this example is that full because I am not sure if it will differ if I take something away…

So how can I make the decide function return a type and then use it in a manner similar to what I have shown?

  • 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-15T17:04:04+00:00Added an answer on June 15, 2026 at 5:04 pm

    I think you may want case object One, etc, rather than using Class or ClassTag. Then you get useful match support. For the act method, your case objects could return a ClassTag or similar, or just let act associate One with doSmth[OneClass] etc.

    It seems you can make your case companions into case objects. Isn’t that special.

    package typeswitch
    import reflect.runtime.universe._
    
    sealed trait Selection
    
    class Super
    case class One(a: Int) extends Super
    case object One extends Selection
    case class Two(b: Float) extends Super
    case object Two extends Selection
    case class Unknown() extends Super
    case object Unknown extends Selection
    
    object Test extends App {
      type What = Selection
    
      def decide(criterion: String): What = criterion match {
        case "one" => One
        case "two" => Two
        case _ => Unknown
      }
    
      val test = List(
        "ahaha" -> "one",
        "ohoho" -> "two",
        "lalala" -> "one"
      )
    
      val m = scala.collection.mutable.Map[String, What]()
    
      test map (pair => m(pair._1) = decide(pair._2))
    
      def act(code: String) = m(code) match {
        case One => doSmth[One]()
        // non-exhaustive
        //case Two => doSmth[Two]()
        case Unknown => doNothing()
        // handle exhaustively
        case s: Selection => doSmthNew(s)
      }
      def doSmthElse[A <: Super]()(implicit t: TypeTag[A]): A = {
        Console println s"Do st with $t"
        val claas: Class[_] = t.mirror.runtimeClass(t.tpe)
        null.asInstanceOf[A]
      }
      def doSmth[A <: Super]()(implicit t: ClassTag[A]): A = {
        Console println s"Do st with $t"
        val claas: Class[_] = t.runtimeClass
        null.asInstanceOf[A]
      }
      def doSmthNew[A >: What : ClassTag, B <: Super](what: A): B = {
        Console println s"Do st new with $what"
        null.asInstanceOf[B]
      }
      def doNothing() { }
    
      val res = act("lalala")
      Console println s"Got $res?"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to return a set of values from function till the point they
I want to pass a delegate with the return type as ArrayList as parameter
I want to return an instance of an object of same type of the
Type.GetFields() does not return fields in a particular order, but I want to sort
TNumberbox and TSpinEdit return values defined as type single . I want to use
I want to return a private field from a remoted object but i get
Given is the following example: class Foo(object): def __init__(self, value=0): self.value=value def __int__(self): return
I want to use the following function (in PostgreSQL): create type holder as (userName
Here is my model_users class (the model it extends from doesn't have anything yet
I have 2 class Item & Type. An Item belongs to one type. It

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.