Commit b26855d
committed
HorizontalBarChartActivity with high precision
The problem is invariant generics. BarDataSet is DataSet<BarEntryFloat>, so entries expects MutableList<BarEntryFloat>. Even though BarEntryDouble : BarEntryFloat, MutableList<BarEntryDouble> is not a MutableList<BarEntryFloat> because MutableList is invariant (it's both a producer and consumer).
MutableList<T> is invariant in T because it both reads (get() → T) and writes (add(T)). Even though BarEntryDouble : BarEntryFloat, the compiler must reject the assignment to prevent this kind of unsound write:
val list: MutableList<BarEntryFloat> = values // if allowed...
list.add(BarEntryFloat(1f, 2f)) // would corrupt the original BarEntryDouble list!
The @Suppress("UNCHECKED_CAST") cast is safe here because:
* JVM erases generics at runtime — both are just MutableList on the heap
* BarDataSet only reads entries from the list as BarEntryFloat, and BarEntryDouble IS a BarEntryFloat
* Nothing writes a plain BarEntryFloat back into the list through BarDataSet1 parent 9a8c74e commit b26855d
File tree
1 file changed
+15
-7
lines changed- app/src/main/kotlin/info/appdev/chartexample
1 file changed
+15
-7
lines changedLines changed: 15 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
99 | | - | |
| 100 | + | |
100 | 101 | | |
101 | 102 | | |
102 | 103 | | |
103 | | - | |
| 104 | + | |
104 | 105 | | |
105 | | - | |
106 | | - | |
| 106 | + | |
| 107 | + | |
107 | 108 | | |
108 | 109 | | |
109 | 110 | | |
| |||
112 | 113 | | |
113 | 114 | | |
114 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
115 | 120 | | |
116 | 121 | | |
117 | 122 | | |
118 | 123 | | |
119 | | - | |
| 124 | + | |
| 125 | + | |
120 | 126 | | |
121 | 127 | | |
122 | 128 | | |
123 | | - | |
| 129 | + | |
| 130 | + | |
124 | 131 | | |
125 | 132 | | |
126 | 133 | | |
| |||
230 | 237 | | |
231 | 238 | | |
232 | 239 | | |
| 240 | + | |
233 | 241 | | |
234 | | - | |
| 242 | + | |
235 | 243 | | |
236 | 244 | | |
237 | 245 | | |
| |||
0 commit comments