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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:38:09+00:00 2026-05-28T03:38:09+00:00

I thinks it’s easier to explain it with a simple example. (help rephrasing the

  • 0

I thinks it’s easier to explain it with a simple example. (help rephrasing the title is welcome 😉

I’d like to implement a squared method and, using implicit def, automatically add it to any class that supports the *-operator.

With an Int it’s very easy:

class EnhancedInt(x: Int) { def squared = x * x }

implicit def IntToEnchancedInt(x: Int) = new EnhancedInt(x)

But with Any or AnyVal I get the following error:

scala> class EnhanceAny(x: AnyVal) { def squared = x * x }
<console>:7: error: value * is not a member of AnyVal
       class EnhanceAny(x: AnyVal) { def squared = x * x }

I’d like to know how I could apply it to any numeric class, or, even better, to any class supporting the *-operator.

  • 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-28T03:38:10+00:00Added an answer on May 28, 2026 at 3:38 am

    It’s not possible to have solution that works on any type with a * method without writing a boilerplate conversion for each type you want to deal with. Essentially to do that you would need a recursive structural type, and Scala does not support those because of JVM type erasure. See this post for more details.

    You can get fairly close to what you want using a type class along with the Numeric type class, (inspired by the answers to this and this question). This will work with most primitives:

    //define the type class
    trait Multipliable[X] { def *(x: X): X}
    
    //define an implicit from A <% Numeric[A] -> Multipliable[Numeric[A]]
    implicit def Numeric2Mult[A](a: A)(implicit num: Numeric[A]): Multipliable[A] = new Multipliable[A]{def *(b: A) = num.times(a, b)}
    
    //now define your Enhanced class using the type class
    class EnhancedMultipliable[T <% Multipliable[T]](x: T){ def squared = x * x}
    
    //lastly define the conversion to the enhanced class
    implicit def Mult2EnhancedMult[T <% Multipliable[T]](x: T) = new EnhancedMultipliable[T](x)
    
    3.squared
    //Int = 9
    
    3.1415F.squared
    //Float = 9.869022
    
    123456789L.squared
    //Long = 15241578750190521
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering what everyone thinks the best method of handling results from your
I would like to know how a kernel programmer thinks about memory vs user
I agree google helps but i would like to few additional thinks Its not
I'm talking about things like page/stylesheet caching, minifying javascript, etc. Half of me thinks
matlab7.10.0(R2010a) I=imread('input-small.jpg'); ???Error using ==> jpeg_depth JPEG parameter struct mismatch: library thinks size is
Emacs is confusing syntax after a line like fileparse($file, qr\.[^.]+$); And thinks the rest
I just started using ReSharper and I'm trying to identify why it thinks this
Resharper certainly thinks so, and out of the box it will nag you to
I was wondering what everyone thinks of this. Is the code easy to follow?
Can somebody tell me why compiler thinks that break is necessary after yield return

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.