Ties
07/13/2022, 3:10 PM@JvmInline
value class Foo(val value : Int) {
}
//This doesnt work
fun bar(myFoo : Foo) : Int{
return myFoo + 1
}
//This works
fun bar(myFoo : Foo) : Int{
return myFoo.value + 1
}
Ruckus
07/13/2022, 3:16 PMChris Lee
07/13/2022, 3:16 PMFoo
, for your value class, and Int
, for Foo.value.Ties
07/13/2022, 3:19 PMChris Lee
07/13/2022, 3:22 PMFoo
(value class) is an Int
(value type), as there are two types in play. You can dereference .value
and/or provide operators or other convenience/extension methods to make usage of Foo
more idiomatic for its value type.Ties
07/13/2022, 3:25 PMChris Lee
07/13/2022, 3:26 PMsomething(a: Int, b: Int)
could be called with Foo
transparently, which puts you back at type aliases.Ties
07/13/2022, 3:26 PMChris Lee
07/13/2022, 3:27 PMTies
07/13/2022, 3:27 PMChris Lee
07/13/2022, 3:29 PMTies
07/13/2022, 3:31 PM@JvmInline
value class Foo(val value : Int) {
}
fun bar(a : Int, b : Foo) = a + b
bar(1, Foo(2)) // would work
bar(1, 2) // would not work
bar(Foo(1), Foo(2)) // would work
Ruckus
07/13/2022, 3:48 PMTies
07/13/2022, 3:54 PMRuckus
07/13/2022, 3:58 PMvalue
is private, the user cannot use it directly, which is effectively what you are proposing.Ties
07/13/2022, 4:04 PMRuckus
07/13/2022, 4:04 PMInt
for the sake of POSIX compliance, allowing fileNotFound + 1
is nonsense.Ties
07/13/2022, 4:06 PMnkiesel
07/13/2022, 6:13 PM@JvmInline
value class Foo(val value : Int) {
operator fun plus(i: Int): Int {
return value + i
}
}
Ties
07/13/2022, 6:19 PMnkiesel
07/13/2022, 6:28 PMvalue class Even(value: Int)
with appropriately overloaded operators would not accomplish that? Of course, you would still not get a compile error for Even(3)
unless you use something like
@JvmInline
value class Even private constructor(val value : Int) {
companion object {
fun ofTwice(i: Int) = Even(i * 2)
}
}
Ties
07/13/2022, 6:56 PM@JvmInline
value class Foo(val value : Int) {}
fun bar(a : Int, b : Foo) = ???
bar(1, Foo(2)) // would work
bar(1, 2) // This will not work
bar(Foo(1).value, Foo(2)) // would work
bar(Foo(1), Foo(2)) // and this doesnt (and this was what my thought experiment was about)
nkiesel
07/13/2022, 7:02 PM1 + Apple(2) + Orange(3)
should result in 6
?Ties
07/13/2022, 7:03 PMnkiesel
07/13/2022, 7:06 PMTies
07/13/2022, 7:09 PMfun doSomething(m : Meter) = ???
will still not accept doSomething(Feet(1))fun doSomething(m : Meter, f: Feet) : Int = m+ f
this will be possible thoughnkiesel
07/13/2022, 7:29 PMm + f
.Ties
07/13/2022, 7:33 PMnkiesel
07/13/2022, 7:36 PM"hello" + 3 == "hello3"
, but I still think the gain does not justify the potential pain (esp. given that I can always implement this if I really want to). Anyway, OO for me nowTies
07/13/2022, 8:00 PMfun doSomething(m : String, f: Int ) : String = m + f
`Youssef Shoaib [MOD]
07/13/2022, 8:07 PMvalue class Positive
could arguably be useful as its underlying int since it only refines the type. Maybe this should be a new language feature? In fact, I suggested a while back the idea of transparent value class
which would be considered both its own type but also its underlying type: https://kotlinlang.slack.com/archives/CQ3GFJTU1/p1629640887047500 https://kotlinlang.slack.com/archives/CQ3GFJTU1/p1629640887047600Klitos Kyriacou
07/14/2022, 8:08 AMprocedure Main is
type Distance is new Float;
type Area is new Float;
D1 : Distance := 2.0;
D2 : Distance := 3.0;
A : Area;
begin
D1 := D1 + D2; -- OK
D1 := D1 + A; -- NOT OK: incompatible types for "+" operator
A := D1 * D2; -- NOT OK: incompatible types for ":=" assignment
A := Area (D1 * D2); -- OK
end Main;
Ruckus
07/14/2022, 2:34 PMTies
07/14/2022, 2:48 PMYoussef Shoaib [MOD]
07/14/2022, 3:00 PMtransparent value class
(having a keyword like transparent makes it very explicit which is good) maybe we can consider that value class to just be a subtype of its underlying value. And so, in a way, Foo would be a subtype of Int, and so, just like a normal subclassing relationship, if Foo + Int
is not defined, Int + Int
will be used instead.Ties
07/14/2022, 3:15 PMYoussef Shoaib [MOD]
07/14/2022, 3:26 PMFoo + Int
that returns a Foo