Bart Kleijngeld
02/15/2022, 12:00 PMBart Kleijngeld
02/15/2022, 12:00 PMimport java.io.File
data class Profile()
Here's one way to do it (1):
fun readProfile(file: File) : Profile = TODO()
Here file
nor File
says nothing about what kind of file. So, you could improve this as follows (2a):
typealias ProfileFile = File
fun readProfile(file: ProfileFile) : Profile = TODO()
or
fun readProfile(profileFile: File) : Profile = TODO()
This example may not be great, but I hope the question is clear. It's hard to encode the contextual meaning in your data without duplication.
Could someone share his experience with how to deal with this?raulraja
02/15/2022, 12:02 PMfun ProfileFile.read(): Profile
Bart Kleijngeld
02/15/2022, 12:05 PMProfileFile
is a type alias of File
, you will extend the File
with this function, right? (Which would be undesirable)raulraja
02/15/2022, 12:06 PM@JvmInline
value class FileProfile(val file: File) {
fun read(): Profile = TODO()
}
raulraja
02/15/2022, 12:07 PMBart Kleijngeld
02/15/2022, 12:08 PMraulraja
02/15/2022, 12:16 PMTies
02/15/2022, 12:16 PMBart Kleijngeld
02/15/2022, 12:19 PMFileProfile
as a typeTies
02/15/2022, 12:23 PMBart Kleijngeld
02/15/2022, 12:24 PMTies
02/15/2022, 12:25 PMBart Kleijngeld
02/15/2022, 12:25 PMTies
02/15/2022, 12:25 PMraulraja
02/16/2022, 1:26 PMString
to model an ID that has certain constrains but does not accept all strings then it’s open for trouble. In some cases though you don’t bother because performance may matter or unpacking repacking the types becomes annoying. I wish Kotlin had the notion of opaque types because value classes still have considerable overhead and ergonomic issues. Opaque types are like type aliases but hide the API of what they are aliasing so you can selectively expose what you want. https://docs.scala-lang.org/scala3/book/types-opaque-types.html