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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:05:12+00:00 2026-06-09T02:05:12+00:00

I have a pair of classes that look something like this. There’s a Generator

  • 0

I have a pair of classes that look something like this. There’s a Generator that generates a value based on some class-level values, and a GeneratorFactory that constructs a Generator.

case class Generator[T, S](a: T, b: T, c: T) {
  def generate(implicit bf: CanBuildFrom[S, T, S]): S =
    bf() += (a, b, c) result
}

case class GeneratorFactory[T]() {
  def build[S <% Seq[T]](seq: S) = Generator[T, S](seq(0), seq(1), seq(2))
}

You’ll notice that GeneratorFactory.build accepts an argument of type S and Generator.generate produces a value of type S, but there is nothing of type S stored by the Generator.

We can use the classes like this. The factory works on a sequence of Char, and generate produces a String because build is given a String.

val gb = GeneratorFactory[Char]()
val g = gb.build("this string")
val o = g.generate

This is fine and handles the String type implicitly because we are using the GeneratorFactory.


The Problem

Now the problem arises when I want to construct a Generator without going through the factory. I would like to be able to do this:

val g2 = Generator('a', 'b', 'c')
g2.generate // error

But I get an error because g2 has type Generator[Char,Nothing] and Scala “Cannot construct a collection of type Nothing with elements of type Char based on a collection of type Nothing.”

What I want is a way to tell Scala that the “default value” of S is something like Seq[T] instead of Nothing. Borrowing from the syntax for default parameters, we could think of this as being something like:

case class Generator[T, S=Seq[T]]

Insufficient Solutions

Of course it works if we explicitly tell the generator what its generated type should be, but I think a default option would be nicer (my actual scenario is more complex):

val g3 = Generator[Char, String]('a', 'b', 'c')
val o3 = g3.generate  // works fine, o3 has type String

I thought about overloading Generator.apply to have a one-generic-type version, but this causes an error since apparently Scala can’t distinguish between the two apply definitions:

object Generator {
  def apply[T](a: T, b: T, c: T) = new Generator[T, Seq[T]](a, b, c)
}

val g2 = Generator('a', 'b', 'c')  // error: ambiguous reference to overloaded definition

Desired Output

What I would like is a way to simply construct a Generator without specifying the type S and have it default to Seq[T] so that I can do:

val g2 = Generator('a', 'b', 'c')
val o2 = g2.generate
// o2 is of type Seq[Char]

I think that this would be the cleanest interface for the user.

Any ideas how I can make this happen?

  • 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-09T02:05:14+00:00Added an answer on June 9, 2026 at 2:05 am

    Is there a reason you don’t want to use a base trait and then narrow S as needed in its subclasses? The following for example fits your requirements:

    import scala.collection.generic.CanBuildFrom
    
    trait Generator[T] {
      type S
      def a: T; def b: T; def c: T
      def generate(implicit bf: CanBuildFrom[S, T, S]): S = bf() += (a, b, c) result
    }
    
    object Generator {
      def apply[T](x: T, y: T, z: T) = new Generator[T] {
        type S = Seq[T]
        val (a, b, c) = (x, y, z)
      }
    }
    
    case class GeneratorFactory[T]() {
      def build[U <% Seq[T]](seq: U) = new Generator[T] {
        type S = U
        val Seq(a, b, c, _*) = seq: Seq[T]
      }
    }
    

    I’ve made S an abstract type to keep it a little more out of the way of the user, but you could just as well make it a type parameter.

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

Sidebar

Related Questions

I have a simple pair of classes that looks like this: class X include
I've create some classes, so that I can have a dictionary with a pair
I have some JSON data that looks like this: { data: [{ name:John Smith,
Is this even possible? I have a key-pair that I already made with GPG
Working with the MVVM pattern, I have a pair of view model classes that
I have some code that looks like the following. First I have some domain
I'm building an application whose usage is going to look something like this: application
There are N different classes that can be observed in my problem and my
I have three classes: SomeThing, SomeOtherThing, and YetAntherThing. All three have an identical member
I have a pair of classes: class Collection < ActiveRecord::Base has_many :items, autosave: true

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.