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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:02:41+00:00 2026-05-25T12:02:41+00:00

I am searching for a way to restrict my polymorphic class to types that

  • 0

I am searching for a way to restrict my polymorphic class to types that have a certain member function.

class Table[T](bla: Array[T]) {
  val symbols = bla

  symbols.foreach( x => x * probability(x))

  def probability(t: T) : Double =  ...
}   

This code does not compile because T doesnt have member *. How can I assure this. I dont want to use inheritance.

Edit: probability is actually implemented. It returns a Double.

Any ideas?

  • 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-25T12:02:42+00:00Added an answer on May 25, 2026 at 12:02 pm

    The problem can be solved in different ways. For example, if you just want type T to have some method (and you don’t care whether this method defined on the object or there is implicit conversion that coverts object to something that has this method), then you can use view bounds. Here is an example that expects type T to have method def *(times: Int): T:

    class Table[T <% {def *(times: Int): T}](bla: Array[T]) {
      bla.foreach( x => println(x * 2))
    } 
    
    new Table(Array("Hello", "World"))
    // Prints:
    //   HelloHello
    //   WorldWorld
    

    String does not have method *, but there exist an implicit conversion to StringOps that has this method.

    Here is another example. In this case I restricting type T with method def size: Int:

    class Table[T <% {def size: Int}](bla: Array[T]) {
      bla.foreach( x => println(x.size))
    } 
    
    new Table(Array(List(1, 2, 3), List("World")))
    // Prints:
    //  3
    //  1
    

    List has method size, and it also works as expected.

    But this could be more involving if you are working with numeric values like ints, floats, doubles, etc. In this case I can recommend you to use context bound. Scala has Numeric type class. You can use it to work with numbers without knowledge about their type (with Numeric you can actually work with anything that can be represented as number, so your code would be much more general and abstract). Here is an example if it:

    import math.Numeric.Implicits._
    
    class Table[T : Numeric](bla: Array[T]) {
      bla.foreach( x => println(x * x))
    } 
    
    new Table(Array(1, 2, 3))
    // Prints:
    //  1
    //  4
    //  9
    
    new Table(Array(BigInt("13473264523654723574623"), BigInt("5786785634377457457465784685683746583454545454")))
    // Prints:
    //  181528856924372945350108280958825119049592129
    //  33486887978237312740760811863500355048015109407078304275771413678604907671187978933752066116
    

    Update

    As you noted in comments, Numeric still does not solve your problem, because it can only work on the numbers of the same type. You can simply solve this problem by introducing new type class. Here is an example of it:

    import math.Numeric.Implicits._
    
    trait Convert[From, To] {
        def convert(f: From): To
    }
    
    object Convert {
        implicit object DoubleToInt extends Convert[Double, Int] {
            def convert(d: Double): Int = d.toInt
        }
    
        implicit object DoubleToBigInt extends Convert[Double, BigInt] {
            def convert(d: Double): BigInt = d.toLong
        }
    }
    
    type DoubleConvert[To] = Convert[Double, To]
    
    class Table[T : Numeric : DoubleConvert](bla: Array[T]) {
      bla.foreach( x => println(x * implicitly[DoubleConvert[T]].convert(probability(x))))
      def probability(t: T) : Double = t.toDouble + 2.5
    } 
    
    new Table(Array(1, 2, 3))
    new Table(Array(BigInt("13473264523654723574623"), BigInt("5786785634377453434")))
    

    With DoubleConvert type class and T : Numeric : DoubleConvert context bound you are not only saying, that T should be some kind of number, but also that there should exist some evidence, that it can be converted from Double. You are receiving such evidence with implicitly[DoubleConvert[T]] and then you are using it to convert Double to T. I defined Convert for Double -> Int and Double -> BigInt, but you can also define you own for the types you need.

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

Sidebar

Related Questions

I'm searching the way(s) to fill an array with numbers from 0 to a
I'm searching for a way to create a menu in an iPhone app that
I have been searching for a way to insert linebreaks in my code for
I have been searching for way to perform a linq query to get an
I have created an IMAP-Server. Now I am searching a way to force the
I am searching for a way to make an application helper that allows you
I am searching a way to combine two MHT-Files. I have one MHT-File and
I'm searching for a way of developing a MethodInterceptor which prints the caller class.
Is there a way you can restrict which grid columns are eligible for searching
I am searching a way to do this : public class SimpleText_SpinnerAdapter extends ArrayAdapter<Enum1>

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.