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

  • Home
  • SEARCH
  • 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 6642091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:53:20+00:00 2026-05-25T23:53:20+00:00

Essentially I want to do something like this: class Shape class CoordSystem class C3D(val

  • 0

Essentially I want to do something like this:

class Shape

class CoordSystem
class C3D(val x: Double, y: Double, z: Double) extends CoordSystem
class C2D(val x: Double, y: Double) extends CoordSystem

abstract class Shape {
  def getCoords[C <: CoordSystem]: List[C]
} 

class Pyramid extends Shape {
  def getCoords: List[C3D] = 
    List(new C3D(1,2,1), new C3D(1,1,1), new C3D(2,2,1), new C3D(2,1,1), new C3D(1.5,1.5,3))
}   
>> error: class Pyramid needs to be abstract, since method getCoords in class Shape of type [C <: CoordSystem]List[C] is not defined

I’ve seen a handful of different ideas on this answer, but none of them seem quite right for this case – because they don’t seem to let me write code elsewhere which refers to myShape.getCoords as if it’s been correctly defined in a Shape subclass, returning a List of objects from a subclass of CoordSystem.

I also found an interesting discussion about generics on the Scala Lang email list, but couldn’t quite tie it back to my situation.

Any help gratefully appreciated!

  • 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-25T23:53:21+00:00Added an answer on May 25, 2026 at 11:53 pm

    How about something like this:

    class CoordSystem
    class C3D(val x: Double, y: Double, z: Double) extends CoordSystem
    class C2D(val x: Double, y: Double) extends CoordSystem
    
    trait ShapeLike[+C <: CoordSystem] {
      def getCoords: List[C]
    }
    
    abstract class Shape extends ShapeLike[CoordSystem]
    
    class Pyramid extends Shape with ShapeLike[C3D] {
      def getCoords: List[C3D] =
        List(new C3D(1, 2, 1), new C3D(1, 1, 1), new C3D(2, 2, 1), new C3D(2, 1, 1), new C3D(1.5, 1.5, 3))
    }
    

    Of course, nothing forces you to declare the extra type ShapeLike to do this; its purpose is to allow you to use the type Shape without annoying extra type parameters.

    So, actually the answer to your question as stated in the title is: you may “tighten up” the type bound of a type parameter in a subclass if it is defined as a covariant type parameter in the superclass; conversely, you may “loosen” the type bound of a contravariant type parameter.

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

Sidebar

Related Questions

Essentially I want to do something like this: class foo: x = 4 @property
I have a generic class that essentially extends a linq class. This means that
Essentially I want to be able to do something like: a = Integer(1) a
I have a class that creates and uses a System.Threading.Timer, something like this: using
I want to implement a view in an iPhone application that is essentially like
I essentially want to give each instance of a class a unique id. So,
So I have a class that looks something like the following: public class MyClass
I remember doing this in DB class in what feels like ages ago, but
Hey guys, I have a UL like so: <ul id=list> <li class=something> <div class=btns
I'm trying to make a generic vector class. While I can do something like

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.