Questions tagged [value-class]

62 questions
5
votes
1 answer

Scala Prohibit allocation of value classes

According to the documentation on value classes, they may be allocated under a number of circumstances: Allocation Summary a value class is treated as another type. a value class is assigned to an array. doing runtime type tests, such as pattern…
J Pullar
  • 1,915
  • 2
  • 18
  • 30
5
votes
1 answer

Scala value class compilation fails for base type with partial-function-parameter method

Say, I defined a value class as follows package object p { class ValueClass[T](val o: Option[T]) extends AnyVal { def foo: Option[T] = o collect { case t => t } } } The compilation failed with the message: overriding…
5
votes
1 answer

Scala: recognising objects of value classes

I'm reflectively invoking a method whose argument might or might not be an instance of a value class. As the purpose of value classes is to avoid boxing of underlying value, if the parameter type is value class then the method in question will in…
narthi
  • 2,188
  • 1
  • 16
  • 27
4
votes
1 answer

Matching against Value Classes in Akka

I've created the Value Class final class Feature(val value: Vector[Double]) extends AnyVal To match against that class in scala: val d = new Feature(Vector(1.1)) s match { case a:Feature => println(s"a:feature, $a") case _ =>…
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
4
votes
0 answers

Why doesn't scala inline factory methods for value classes?

Consider: class Wrap(val amount :Int) extends AnyVal object Wrap { @inline final def apply(int :Int) = new Wrap(int) } object Playground { val w = Wrap(1) } compiling with (2.11.7) scalac -feature -Xprint:all Playground.scala I…
Turin
  • 2,208
  • 15
  • 23
4
votes
0 answers

Is it possible to obtain the effect of a specialized value class in scala?

I know how @specialized classes and value classes work and I know that I can't have a specialized value class as in: class MilliVolt[@specialized T](val content :T) extends AnyVal //illegal I can't find a way to obtain a similiar effect: with a…
Turin
  • 2,208
  • 15
  • 23
4
votes
1 answer

Using Scala value classes to define new numeric type

When I first heard of value classes, I thought -- finally! Now I can define my own numeric types sans object allocations! But it turned out harder than I thought. I want to define my own Decimal type, either Dec64 (http://dec64.com/) or long-backed…
Alexander Temerev
  • 2,654
  • 3
  • 27
  • 34
4
votes
1 answer

Mockito stubbing method with value class argument fails with NullPointerException

Using typed value classes as IDs is a common pattern in Scala. However, it seems Mockito has an issue when stubbing methods that take value classes as arguments. In the example below, the first stub, with an actual value works just fine, but the…
Cristian Vrabie
  • 3,972
  • 5
  • 30
  • 49
3
votes
2 answers

Trouble serializing optional value class instances with json4s

I'm trying to serialize a case class with optional value-class field to JSON using json4s. So far, I'm not able to get the optional value-class field rendered correctly (see the snippet below with examples). I tried json-native and json-jackson…
Roman
  • 64,384
  • 92
  • 238
  • 332
3
votes
1 answer

How to mock a function that returns a value class instance with Mockito?

I'm trying to mock a method that returns an instance of a value class (extends AnyVal) I'm getting some weird error message, which I understand (because of value class erasure) but I'm surprised Mockito doesn't cope with that. My class: case class…
Guillaume
  • 22,694
  • 14
  • 56
  • 70
3
votes
1 answer

Scala implicit conversions and mkNumericOps with value classes

I am trying to add numeric operations to a value class that I have defined called Quantity. Code that I am using this is as follows... import scala.language.implicitConversions case class Quantity(value: Double) extends AnyVal object Quantity { …
davidrpugh
  • 4,363
  • 5
  • 32
  • 46
3
votes
1 answer

Why do implicit value classes have an extra method invocation?

I was inspecting the byte-code generated by implicit classes and wanted to compare to what is generated when they extend AnyVal. Without implicit: object Example1 { class Wrapper(val self: Int) extends AnyVal { def add(n: Int): Int = self + n …
Gabor Juhasz
  • 317
  • 2
  • 9
3
votes
4 answers

validations in value classes

SIP-15 implies one can use value classes to define for example new numeric classes, such as positive numbers. Is it possible to code such a constraint that the underlying > 0 in absence of constructor without having to call a separate method for…
Sachin K
  • 195
  • 1
  • 8
3
votes
3 answers

Does having a private constructor on a value class negate the benefits of a value class?

I'm thinking of making a value class that has some guard on how it can be instantiated. For the sake of example, say I want a non-negative integer: class NonNegInt private (val value: Int) extends AnyVal object NonNegInt { def apply(value: Int):…
2rs2ts
  • 10,662
  • 10
  • 51
  • 95
2
votes
1 answer

Using Kotlin Value classes in Java

We have two projects, and the kotlin one publishes a package that's imported by java. In kotlin, is a value class like @JvmInline value class CountryId(private val id: UUID) { override fun toString(): String = id.toString() companion object…
CaffGeek
  • 21,856
  • 17
  • 100
  • 184