added example of compiler issue; unable to create 'size' and 'empty' properties on Collection<T> and java.util.Map<T> it seems? wonder if its a qualified name issue - qualified names don't seem to be allowed on extension properties - will raise an issue...

This commit is contained in:
James Strachan
2011-12-23 17:20:25 +00:00
parent 28d9138798
commit 8396017b5b
3 changed files with 47 additions and 34 deletions
-34
View File
@@ -82,37 +82,3 @@ val <T> List<T>.last : T?
// Map APIs
/** Returns the size of the map */
/* TODO get redeclaration errors
val Map<*,*>.size : Int
get() = size()
*/
/** Returns true if this map is empty */
/* TODO get redeclaration errors
val Map<*,*>.empty : Boolean
get() = isEmpty()
*/
/** Returns the value for the given key or returns the result of the defaultValue function if there was no entry for the given key */
inline fun <K,V> java.util.Map<K,V>.getOrElse(key: K, defaultValue: ()-> V) : V {
val current = this.get(key)
if (current != null) {
return current
} else {
return defaultValue()
}
}
/** Returns the value for the given key or the result of the defaultValue function is put into the map for the given value and returned */
inline fun <K,V> java.util.Map<K,V>.getOrElseUpdate(key: K, defaultValue: ()-> V) : V {
val current = this.get(key)
if (current != null) {
return current
} else {
val answer = defaultValue()
this.put(key, answer)
return answer
}
}
+39
View File
@@ -0,0 +1,39 @@
package std.util
import java.util.*
// Map APIs
/** Returns the size of the map */
/* TODO get redeclaration errors
val Map<*,*>.size : Int
get() = size()
*/
/** Returns true if this map is empty */
/* TODO get redeclaration errors
val Map<*,*>.empty : Boolean
get() = isEmpty()
*/
/** Returns the value for the given key or returns the result of the defaultValue function if there was no entry for the given key */
inline fun <K,V> java.util.Map<K,V>.getOrElse(key: K, defaultValue: ()-> V) : V {
val current = this.get(key)
if (current != null) {
return current
} else {
return defaultValue()
}
}
/** Returns the value for the given key or the result of the defaultValue function is put into the map for the given value and returned */
inline fun <K,V> java.util.Map<K,V>.getOrElseUpdate(key: K, defaultValue: ()-> V) : V {
val current = this.get(key)
if (current != null) {
return current
} else {
val answer = defaultValue()
this.put(key, answer)
return answer
}
}
+8
View File
@@ -32,4 +32,12 @@ class MapTest() : TestSupport() {
assertEquals(1, data.size())
}
/** TODO can't seem to define size/empty properties for Map
fun testSizeAndEmpty() {
assert{ data.empty }
assertEquals(data.size, 0)
}
*/
}