How could I get some compile time safety here? It'...
# announcements
p
How could I get some compile time safety here? It's an enum and an exhaustive when and I the cases on line 10 and then the when on line 11 feel like a smell
g
Why do you need internal when? Why not just have
AnalysisType.ARM -> BodyValueType.ARM_CIRCUMFERENCE
?
p
Because I return sth else later, al these nulls are supposed to return sth else later
g
Maybe you could provide full example, because this one is not really clear for me
b
Since these look like 1-1 mapping, can’t you include
BodyValueType
in
AnalysisType
itself?
Like having a
getBodyValueType
and let it return
null
for base class and have interested entities override it and return proper
BodyValueType
?
p
If I include it in AnalysisType, I don't have the sealed when safety any more, right?
This is the whole function
@gildor
g
Still looks a bit strange, so you just want to do some common logic for those cases? why not just extract it to function?
p
Extrat what in a function how?
g
Common code for those entities
Something like this:
Copy code
fun BodyValueType.toBodyResult() {
    val key = BodyValueSummaryGroupKey(range.start, range.endInclusive, this)
    val bodyValueSummary = bodyValueSummaryProvider.get(key).awaitFirst()
    val history = History(getAnalysisSummary.circumference(bodyValueSummary, mode))
    Result(ChartData2, mode, history, Summary)
}
And than just
Copy code
AnalysisType.ARM -> BodyValueType.ARM_CIRCUMFERENCE.toBodyResult()
it may be local function or local lambda if you want to use closure instead explicit types for things like range
So in this case you will have 1 level exhaustive when
It’s hard to experiment with this code because I don’t have self contained example or at least enum, maybe I don’t see some problem, but looks that this approach should work
b
I was mentioning about removing whole
when
clauses and using methods on
AnalysisType
(or more appropriate class) to build
Result
objects. So, this will essentially look like,
Copy code
suspend fun get(mode: AnalysisMode, type: AnalysisType): Result {
    return   type.buildResult()
}
Not exactly as your method is somewhat complex in nature. But I think this can be worked out
g
Yes, one more option, but it may depends on case, looks that Result also contains some additional information depending on case, but I agree, it would be probably more clear solution, separate when for AnalysisType and BodyValueType + Return
p
Hm; not sure there is a good sulution for this. The problem is also that there are also `BodyValueType`s where I need to perform a different logic. By using
type.buildResult()
I only move the problem because then I'll just have a
when(this)
switch 😕
g
What about my approach?
b
But still your
BodyValueType
are 1-1 to
AnalysisType
right? So,
buildResult
in
AnalysisType.ARM
can use
BodyValueType.ARM_CIRCUMFERENCE
to construct the result object without
when
?
p
Ah I think I got it
🙂 thanks you two this is better 🙂
g
caloriesResult and circumferenceResult also may be local, so wouldn’t require
mode
param
But not sure that it would be clear enough
p
g
yes, or with lambda
p
What do you mean by "with lambda"?
g
Use local lambda instead of local function