-1
fun compareWithIntermediate(output: SHR, input: Intpos): CompareResult{
 val matched = mutableListOf<String>()
 val mismatched = mutableListOf<String>()
 val modifiableAttr = mutableListOf<String>()

 compareValue(output.a.b.c, input.a.b.c,matched,mismatched)
 ......
 compareValue(output.x.y.z, input.x.y.z,matched,mismatched)

 return CompareResult(matched,mismatched)       

}


private fun compareValue(outputVal: String, inVal: String, matched:MutableList<String>, 
   mismatched:MutableList<Diff>){

   when(outputVal.compareTo(inVal)==0){
      true -> matched.add("abc")
       false -> mismatched.add(Diff("abc",outputVal,inVal))
   }
 }

So every time compareValue() is invoked i want to add values to matched or mismatched list based on condition.

How can i achieve this as multiple true/false could be possible in when. How can i add values to respective list based on condition. Does enum help solve this rather writing multiple same conditions in "when"??

Sergio
  • 27,326
  • 8
  • 128
  • 149
I_am_Vits
  • 47
  • 1
  • 10
  • Why use `when` when you can do `if (outputVal.compareTo(inVal) == 0) matched.add("abc") else mismatched.add(...)`? – user Jun 03 '20 at 20:21
  • 1
    I am not sure I understand your problem. What exactly is happening here that you don't like? Your compareValue function takes 2 strings and compare them. There can only be two outcomes : True or False? – jlengrand Jun 03 '20 at 20:21
  • just to use idiomatic way i thought to use when – I_am_Vits Jun 03 '20 at 20:22
  • @jlengrand i need update matched list/mismatched list based on true or false. – I_am_Vits Jun 03 '20 at 20:23
  • @I_am_Vits Isn't that exactly what you are doing right now though? – user Jun 03 '20 at 20:27
  • https://pl.kotl.in/8ZREsT3Rt -> @I_am_Vits I simplfied (removed the Diff structure) but your code does exactly what you describe. And because there are only 2 possible outcomes, I would agree with the first comment that using an if is more idiomatic – jlengrand Jun 03 '20 at 20:29

1 Answers1

1

Please use simpler if statement for your case:

if (outputVal.compareTo(inVal) == 0) {
   matched.add("abc")
   // update mismatched list here if need
} else {
   mismatched.add(Diff("abc",outputVal,inVal))
   // update matched list here if need
}
Sergio
  • 27,326
  • 8
  • 128
  • 149
  • Your code is more idiomatic, but does not answer the question in the title though. – jlengrand Jun 03 '20 at 20:30
  • Yes. How can i achieve in when expression for multiple same conditions?? – I_am_Vits Jun 03 '20 at 20:33
  • @I_am_Vits there are 3 people now telling you that your code works according to your description. There is no bug, as you describe it. I there is one, your example does not seem to be showing it. Can you maybe rework your description or provide an example that shows your issue – jlengrand Jun 03 '20 at 20:34
  • 1
    @I_am_Vits what do you mean by `multiple same conditions`? Could you please update your answer to show it – Sergio Jun 03 '20 at 20:38
  • @Sergey. Let's say i called compareValue 5 times and it evaluates to true then i need to add strings to matched list. How can i update matched list in "when" to add new entries? – I_am_Vits Jun 03 '20 at 20:41
  • Ok, lets begin from function `compareWithIntermediate ()`, here you create two mutable lists `matched` and `mismatched`, then you call `compareValue()` 5 times. If all 5 times `when` expression in `compareValue()` function evaluates to true, then `matched` list will contain 5 strings "abc". And when `CompareResult(...)` constructor is called `matched` list containing those 5 strings will be passed there. Is it correct? – Sergio Jun 03 '20 at 20:54