y
01/19/2023, 10:59 AMProcessor
that processes some data and I have a function process()
that instantiates the class and returns the result. I don’t want to have intermediate state, so I want to disallow creation of this class outside of the file it is declared in.
however, I do want to call the function as Processor.process()
.
currently I have
class Processor private constructor(...) {
//...
private fun finalResults(): Int
companion object {
fun process(...): Int {
return Processor(...).finalResults()
}
}
}
is there another way to do this?
EDIT: basically, I'm asking for a way to emulate namespaces here.CLOVIS
01/19/2023, 1:04 PMfun process(…): Int { … }
private class Processor { … }
Sounds to me like the class shouldn't exist at all, it should probably just be private top level functionspsh
01/19/2023, 1:50 PMobject Processor {
fun processOne (): Int {
return 123
}
fun processTwo (): Int {
return 456
}
}
y
01/19/2023, 1:58 PMCLOVIS
01/19/2023, 1:59 PMprocess
function call.y
01/19/2023, 2:01 PMProcessor
has properties, does some (recursive) calculations, and ends up with a solutionJoffrey
01/19/2023, 2:01 PMProcessor
has state, the object
option is not viable indeedy
01/19/2023, 2:02 PMBufferedReader
that is initialized with a file path, and the result is a String of the fileJoffrey
01/19/2023, 2:02 PMProcessor.process()
call site nice with the current approachy
01/19/2023, 2:03 PMCLOVIS
01/19/2023, 2:03 PMclass Processor {
…
fun process(): Int
}
y
01/19/2023, 2:03 PMCLOVIS
01/19/2023, 2:04 PMy
01/19/2023, 2:04 PMCLOVIS
01/19/2023, 2:05 PMJoffrey
01/19/2023, 2:05 PMCLOVIS
01/19/2023, 2:06 PMprocess
top-level function, but it seems to me like you could just make a regular class that accepts process
' arguments as constructor parameters.y
01/19/2023, 2:07 PMProcessor
class.CLOVIS
01/19/2023, 2:08 PMJoffrey
01/19/2023, 2:08 PMCLOVIS
01/19/2023, 2:09 PMy
01/19/2023, 2:09 PMJoffrey
01/19/2023, 2:09 PMCLOVIS
01/19/2023, 2:11 PMJoffrey
01/19/2023, 2:11 PMobject
next to it, with its own name to hold the public functions you want to supportCLOVIS
01/19/2023, 2:12 PMy
01/19/2023, 2:13 PMCLOVIS
01/19/2023, 2:13 PMobject Processor {
fun process(): Int { … }
}
private class YourInternalState(…)
Joffrey
01/19/2023, 2:15 PMy
01/19/2023, 2:15 PMJoffrey
01/19/2023, 2:16 PMCLOVIS
01/19/2023, 2:16 PMprivate
on everything, since the entire class is privatey
01/19/2023, 2:16 PM