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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:19:37+00:00 2026-06-15T15:19:37+00:00

As far as I understand value classes in Scala are just there to wrap

  • 0

As far as I understand value classes in Scala are just there to wrap primitive types like Int or Boolean into another type without introducing additional memory usage. So they are basically used as a lightweight alternative to ordinary classes.

That reminds me of Haskell’s newtype notation which is also used to wrap existing types in new ones, thus introducing a new interface to some data without consuming additional space (to see the similarity of both languages consider for instance the restriction to one “constructor” with one field both in Haskell and in Scala).

What I am wondering is why the concept of introducing new types that get inlined by the compiler is not generalized to Haskell’s approach of having zero-overhead type wrappers for any kind of type. Why did the Scala guys stick to primitive types (aka AnyVal) here?

Or is there already a way in Scala to also define such wrappers for Scala.AnyRef types?

  • 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-15T15:19:39+00:00Added an answer on June 15, 2026 at 3:19 pm

    They’re not limited to AnyVal.

    implicit class RichOptionPair[A,B](val o: Option[(A,B)]) extends AnyVal {
      def ofold[C](f: (A,B) => C) = o map { case (a,b) => f(a,b) }
    }
    
    scala> Some("fish",5).ofold(_ * _)
    res0: Option[String] = Some(fishfishfishfishfish)
    

    There are various limitations on value classes that make them act like lightweight wrappers, but only being able to wrap primitives is not one of them.

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

Sidebar

Related Questions

I'm trying to understand what Scala does with Case Classes that makes them somehow
Here is what I understand so far: PASS BY VALUE Passing by value means
As far as I understand, using works like a try/catch/finally, so I would expect
I Just can't seem to wrap my head around them. As I understand it's
As far as I understand from the documentation the QUdpSocket are async but, still,
As far as I understand Facebook allowes you (through the API) to post on
As far as I understand, https is http plus SSL/TLS. What do I need
As far as I understand, the onchange event of a text input element is
As far as I understand the serial port so far, transferring data is done
As far as I understand WebRequest.PreAuthenticate is almost always good. If I enable it

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.