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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:12:21+00:00 2026-05-22T21:12:21+00:00

Let’s say we have the following types: sealed trait T case object Goat extends

  • 0

Let’s say we have the following types:

sealed trait T
case object Goat extends T
case object Monk extends T
case object Tiger extends T

Now, how do you construct a collection of T such that at least one of each sub-T appears in the collection, this constraint being enforced at compile time? A collection where, contrary to this:

val s:Seq[T] = Seq(Goat)

which compiles, this:

val ts:TotalSeq[T] = TotalSeq(Goat, Goat, Tiger)

does not. I’ve used Scala above, but I would be happy to see solutions in other languages as well.

(Forgive me if my words are a bit off; I have a cold and am amusing myself today with puzzles.)

  • 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-22T21:12:22+00:00Added an answer on May 22, 2026 at 9:12 pm

    It looks like the builder pattern with generalized type constraints: http://www.tikalk.com/java/blog/type-safe-builder-scala-using-type-constraints

    Something like:

    sealed trait TBoolean
    sealed trait TTrue extends TBoolean
    sealed trait TFalse extends TBoolean
    
    class SeqBuilder[HasGoat <: TBoolean, HasMonk <: TBoolean, HasTiger <: TBoolean] private (seq: Seq[T]) {
    
      def +=(g: Goat.type) = new SeqBuilder[TTrue, HasMonk, HasTiger](g +: seq)
    
      def +=(m: Monk.type) = new SeqBuilder[HasGoat, TTrue, HasTiger](m +: seq)
    
      def +=(t: Tiger.type) = new SeqBuilder[HasGoat, HasMonk, TTrue](t +: seq)
    
      def build()(implicit evg: HasGoat =:= TTrue, evm: HasMonk =:= TTrue, evt: HasTiger =:= TTrue) = seq
    }
    
    
    object SeqBuilder {
      def apply = new SeqBuilder(Seq())
    }
    

    Usage:

    scala> SeqBuilder() + Goat + Tiger + Monk build()
    res21: Seq[T] = List(Monk, Tiger, Goat)
    
    scala> SeqBuilder() + Goat + Goat + Monk build()
    <console>:34: error: Cannot prove that Nothing =:= TTrue.
           SeqBuilder() + Goat + Goat + Monk build()
                                             ^
    

    If the number of instances you want grows you can try using an HMap

    Other similar approaches: phantom types: http://james-iry.blogspot.com/2010/10/phantom-types-in-haskell-and-scala.html

    Another approach is to ask why you need all 3 objects. Say you want to carry an operation when the seq has all three, then you can do something like:

    object SeqBuilder {
      val all = Seq(Goat, Monk, Tiger)
    
      def apply(t: T*)(onAll: Seq[T] => Unit)(onViolation: => Unit) = {
         val seq = Seq(t:_*)
         if(all forall seq.contains) onAll(seq)
         else onViolation
      }
    }
    

    That way, the function onAll is not called if not all required Ts have been supplied and otherwise the violation function is called

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

Sidebar

Related Questions

Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say for example, I have the following javascript function that returns a boolean:
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say for example i have URL containing the following percent encoded character :
Let's say I have the following classes: class Votable(models.Model): name = ... class Vote(models.Model):
Let's say I have the following tuples: scala> val t1 = Tuple2(abcd, efg) t1:

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.