value-class

Value-based Classes confusion

南楼画角 提交于 2020-01-22 09:42:04
问题 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 objects ) (6) they are freely substitutable when equal, meaning that interchanging any two instances x and y that are equal according to equals() in any computation or method invocation should produce no visible change in behavior. Optional is such a

why scala value class#toString contains case class info?

跟風遠走 提交于 2019-12-24 00:36:59
问题 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 would be a simple Int on runtime). But if you do call a .toString method on a value class instance it would print something like: scala> val myValueClass = X(3) myValueClass: X = 3 scala> myValueClass.toString res5: String = X(3) so I guess the

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

徘徊边缘 提交于 2019-12-22 05:54:22
问题 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 wrapperHolder: Error:(5, 22) Result type in structural refinement may not refer to a user-defined value class def wrapper: Wrapper = w Why it doesn't work for value class? 来源: https://stackoverflow.com/questions/52678323/result-type-in-structural-refinement-may-not

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

我与影子孤独终老i 提交于 2019-12-10 12:48:24
问题 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 <- read.xlsx('~desktop/data.xlsx) After that I check the class: sapply(data, class) x1 a b c "character" "numeric" "numeric" "numeric" Which is exactly what I want. My problem occurs when I try to transpose the matrix, and then check for class again: data <- t(data) When i check with

Mockito stubbing method with value class argument fails with NullPointerException

谁说我不能喝 提交于 2019-12-07 09:54:59
问题 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 second one, that uses an argument matcher throws NullPointerException. The only reference to this I've found is this question but the solution shown there does not work. Anyone knows a solution to this, or a work-around? Versions are: org.mockito

Mockito stubbing method with value class argument fails with NullPointerException

浪尽此生 提交于 2019-12-05 13:49:16
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 second one, that uses an argument matcher throws NullPointerException. The only reference to this I've found is this question but the solution shown there does not work. Anyone knows a solution to this, or a work-around? Versions are: org.mockito:mockito-all:1.10.19 and org.specs2:specs2_2.11:2.4.15 import org.specs2.mutable.Specification import org

Value classes introduce unwanted public methods

大城市里の小女人 提交于 2019-12-05 11:35:03
问题 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 // arghh.... That stuff appears both in the scala docs and in the IDE auto completion which is really not good. So... any ideas of how to mitigate this problem? I mean you can use RichInt(val self: Int) but that doesn't make it any better ( 4.self ,

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

半城伤御伤魂 提交于 2019-12-05 05:59:50
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 wrapperHolder: Error:(5, 22) Result type in structural refinement may not refer to a user-defined value class def wrapper: Wrapper = w Why it doesn't work for value class? 来源: https://stackoverflow.com/questions/52678323/result-type-in-structural-refinement-may-not-refer-to-a-user-defined-value-class

Value classes introduce unwanted public methods

坚强是说给别人听的谎言 提交于 2019-12-03 23:21:47
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 // arghh.... That stuff appears both in the scala docs and in the IDE auto completion which is really not good. So... any ideas of how to mitigate this problem? I mean you can use RichInt(val self: Int) but that doesn't make it any better ( 4.self , wth?) EDIT : In the following example, does the compiler erase the intermediate object, or not? import

Value-based Classes confusion

拜拜、爱过 提交于 2019-12-03 02:35:55
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 objects ) (6) they are freely substitutable when equal, meaning that interchanging any two instances x and y that are equal according to equals() in any computation or method invocation should produce no visible change in behavior. Optional is such a class. Optional a = Optional.of(new ArrayList<String>()); Optional b = Optional.of(new ArrayList<String>