Questions tagged [value-class]
62 questions
2
votes
1 answer
Mocked function doesn't return value class correctly in Scala
I need to mock a function from an arbitrary type to another type that is a value-class.
For example with the following signature String => ValueClass.
This is how my value-class is implemented:
final case class ValueClass(value: String) extends…

mkUltra
- 2,828
- 1
- 22
- 47
2
votes
1 answer
Yet another attempt of creating Anyval Option
I'm trying to implement an Option which doesn't consume extra memory for a wrapper.
I create a class. Null stands for None, non-null value stands for Some.
class Maybe[+T](private val nullable: T) extends AnyVal {
@inline final def isDefined:…

simpadjo
- 3,947
- 1
- 13
- 38
2
votes
2 answers
why scala value class#toString contains case class info?
value classes can be used to achieve type safety without the overhead of unboxing.
I had the impression that in runtime such types/classes would "not exist", being seen as simple types (for instance, a value class case class X(i: Int) extends AnyVal…

pedrorijo91
- 7,635
- 9
- 44
- 82
2
votes
2 answers
Idiomatic approach to Scala Value Classes
Does anyone know if there is an idiomatic way to name the inner value of a Scala Value Class? Say I have a value class for a product id, is it better defined as:
case class ProductId(productId:String) extends AnyVal
case class…

Diego Martinoia
- 4,592
- 1
- 17
- 36
2
votes
1 answer
Why can't I override a method that takes a value-class as parameter in Scala?
I'm playing around with value classes (class that extends AnyVal) in Scala 2.10.3 but are running into a strange compiler error when using them as parameter to abstract methods.
As the following example demonstrates:
class ValueClass(val x: Int)…

mbergenlid
- 23
- 2
1
vote
1 answer
Is there any way to rewrite the below code using Scala value class or other concept?
I need to write two functions to get the output format and the output index for file conversion. As part of this, I wrote a TransformSettings class for these methods and set the default value. And in the transformer class, I created a new object of…

vsathyak
- 73
- 8
1
vote
0 answers
Should tagged types work for value classes?
I am trying to create a tagged type (in Scala 2.x) in the same way that Shapeless (2.x) does but it fails with a ClassCastException:
object Main {
sealed trait TaggedProps[Props] extends Any
final class KeyAddingStage(private val args:…

steinybot
- 5,491
- 6
- 37
- 55
1
vote
1 answer
Can you make a list of value classes, with the list being bound to the list of values?
Since value classes (aka inline classes) are not classes at runtime but the value class, is it possible to make a list of them, and the list is bound to the list of values?
If i couldn't explain that very clearly, here is a code example:
class…

RGBCube
- 82
- 5
1
vote
0 answers
Purpose of Scala trait extending AnyVal?
Sometimes in Scala/Scala.js I see trait Foo extends AnyVal { ... } – what's the meaning and purpose of extends AnyVal here?
I'm aware of value classes, and that they can not extend traits that extend AnyRef, so using extends AnyVal would make sense…

Nikita
- 2,924
- 1
- 19
- 25
1
vote
1 answer
What data structure should I use for lookup in constant time from a changing pair of values to an object in java?
I have a valueclass (position) with two doubles (x and y) in it.
I also have a class ship that has an attribute with a position object in it.
I need to be able to do do a lookup like: get ship at (5,7) with constant time.
I also want to be able to…

Frederik Baetens
- 781
- 1
- 9
- 20
1
vote
1 answer
Hierarchy of value classes in Scala?
I have defined the following class hierarchy where I want to restrict the the type parameter to be conformable with Double...
sealed abstract class Quantity[-T](value: T)(implicit ev: T <:< Double)
case class DiscreteQuantity(value: Long) extends…

davidrpugh
- 4,363
- 5
- 32
- 46
1
vote
2 answers
scala value class multiple inheritance
I have in my project objects that represent IDs.
Let's say it is ChairId, TableId, LampId. I want them all to inherit from GenericId. And I want to be able to call def f(x: GenericId) = x.id
I want them to hold only single id: String so I would like…

kpbochenek
- 469
- 2
- 21
1
vote
1 answer
Java: What is the difference (also in use) between an enumeration and a value class?
I fail to see the difference between a value class and an enumeration. It would be great if someone could tell me the difference between them and what the difference in their use is.

Sander
- 142
- 2
- 15
1
vote
1 answer
Scala: Value Class vs Case Class
I'm trying to discover the differences between using a value class or a case class in a given scenario. Suppose I want to model the integers mod 5 as a unique datatype. The question is which one I should begin with...
class IntegerMod5(val value:…

Kelvin Chung
- 1,327
- 1
- 11
- 23
1
vote
2 answers
Chaining value class calls
Is it possible to chain values classes without them actually being instantiated? For example:
import scala.swing._
object SwingPlus {
implicit class RichComponent(val self: Component) extends AnyVal {
def clientProps: ClientProperties = new…

0__
- 66,707
- 21
- 171
- 266