Questions tagged [value-class]

62 questions
16
votes
1 answer

Why does the transpose function change numeric to character in R?

I've constructed a simple matrix in Excel with some character values and some numeric values (Screenshot of data as set up in Excel). I read it into R using the openxlsx package like so: library(openxlsx) data <-…
Morten Nielsen
  • 325
  • 2
  • 4
  • 19
15
votes
5 answers

Value classes introduce unwanted public methods

Looking at some scala-docs of my libraries, it appeared to me that there is some unwanted noise from value classes. For example: implicit class RichInt(val i: Int) extends AnyVal { def squared = i * i } This introduces an unwanted symbol i: 4.i …
0__
  • 66,707
  • 21
  • 171
  • 266
14
votes
5 answers

Value-based Classes confusion

I'm seeking some clarification to the definition of Value-based Classes. I can't imagine, how is the last bullet point (6) supposed to work together with the first one (1) they are final and immutable (though may contain references to mutable…
maaartinus
  • 44,714
  • 32
  • 161
  • 320
13
votes
1 answer

How do you enrich value classes without overhead?

Scala 2.10 introduces value classes, which you specify by making your class extend AnyVal. There are many restrictions on value classes, but one of their huge advantages is that they allow extension methods without the penalty of creating a new…
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
11
votes
4 answers

android Room with kotlin value class?

I'm trying to use a room entity with a value class: @JvmInline value class UserToken(val token: String) and the entity: @Entity(tableName = TABLE_AUTH_TOKEN) data class TokenEntity( @PrimaryKey val id: Int = 0, val token: UserToken ) I get the…
TootsieRockNRoll
  • 3,218
  • 2
  • 25
  • 50
11
votes
1 answer

Is there a way to get a warning when a Scala Value Class needs to become instantiated?

In the documentation about Scala value classes, it is mentioned that there are three cases when a value class needs to actually be allocated an instance: Allocation Summary A value class is actually instantiated when: a value class is treated as…
Prikso NAI
  • 2,592
  • 4
  • 16
  • 29
10
votes
1 answer

Elegant grouping of implicit value classes

I'm writing a set of implicit Scala wrapper classes for an existing Java library (so that I can decorate that library to make it more convenient for Scala developers). As a trivial example, let's say that the Java library (which I can't modify) has…
Mike Allen
  • 8,139
  • 2
  • 24
  • 46
9
votes
1 answer

Does Inheritance in implicit value classes introduce an overhead?

I want to apply scala's value classes to one of my projects because they enable me to enrich certain primitive types without great overhead (I hope) and stay type-safe. object Position { implicit class Pos( val i: Int ) extends AnyVal with…
peri4n
  • 1,389
  • 13
  • 24
8
votes
1 answer

Result type in structural refinement may not refer to a user-defined value class

When I define Wrapper as value class(extending AnyVal): class Wrapper(val string: String) extends AnyVal def wrapperHolder(w: Wrapper): {def wrapper: Wrapper} = new { def wrapper: Wrapper = w } I have following compile error for…
mkUltra
  • 2,828
  • 1
  • 22
  • 47
8
votes
3 answers

Scala value class, use cases

I know value class in scala inline the operation at compiler time. maybe like this case class A(i: Int) extends AnyVal { def +(that: A) = A(this.i + that.i) } A(1) + A(2) // After compile it equals to 1 + 2 But It seems not a big deal to…
Wonpyo Park
  • 301
  • 3
  • 11
7
votes
1 answer

Implicit conversion between Scala.Long and Java.lang.Long in collections

I'm using JavaConverters to go from a Java SortedSet to a Vector. val lines = function.getInstructions.asScala.toVector My getInstructions function returns an ArrayList of java.lang.Long, yet the consuming code requires Scala.Long. Is there a…
Steve H.
  • 253
  • 1
  • 3
  • 12
6
votes
1 answer

Convert sequence of AnyVal case class (Seq[T <: AnyVal]) to its runtime representation efficiently

Assuming I have a value case class case class Id(i:Int) extends AnyVal and a sequence containing this value case class Seq(Id(1), Id(2), Id(3)) is there a way of converting those values into Int without that there is a need of iteration over the…
Big_Foot1989
  • 311
  • 1
  • 3
  • 11
6
votes
1 answer

Value classes, universal traits and the necessity of instantiation

In the specification of value classes, it says: A value class can only extend universal traits and cannot be extended itself. A universal trait is a trait that extends Any, only has defs as members, and does no initialization. Universal traits…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
5
votes
1 answer

Kotlin inline value class - cannot override hashCode() function

I just cannot override hashCode() function on value class. minimal example (I know that in this example there is no need to override it...) @JvmInline value class Identifier(val value: String){ override fun hashCode(): Int =…
5
votes
0 answers

Value classes as structural type members

Here is the simplified code: class Value(val value: Int) extends AnyVal val v = new Value(1) val x = new { val f: Int = v.value } println(x.f) val y = new { val f: Value = v } println(y.f) Error: Result type in structural refinement may not…
xiagao1982
  • 1,077
  • 1
  • 13
  • 25
1
2 3 4 5