Default parameters, function type examples fixed, tuples with named entries

This commit is contained in:
Andrey Breslav
2010-11-25 16:51:30 +03:00
parent 73b12271c0
commit dd49fc6ce2
8 changed files with 84 additions and 18 deletions
+1 -1
View File
@@ -31,7 +31,7 @@ class AntBuilder {
fun classpath(entries : ClassPathEntry...) { ... }
}
fun library(initializer : Library.{ => Library}) {
fun library(initializer : Library.{ () : Library}) {
val lib = new Library()
lib.initializer()
return lib
+3 -3
View File
@@ -24,11 +24,11 @@ class Graph<V, E> {
fun neighbours(v : Vertex<V>) = edges.filter{it.from == v}.map{it.to} // type is IIterable<Vertex<V>>
fun dfs(handler : {V => ()}) {
fun dfs(handler : {(V) : Unit}) {
val visited = new HashSet<Vertex<V>>()
vertices.foreach{dfs(it, visited, handler)}
fun dfs(current : Vertex<V>, visited : ISet<Vertex<V>>, handler : {V => ()}) {
fun dfs(current : Vertex<V>, visited : ISet<Vertex<V>>, handler : {(V) : Unit}) {
if (!visited.add(current))
return
handler(current)
@@ -36,7 +36,7 @@ class Graph<V, E> {
}
}
public fun traverse(pending : IPushPop<Vertex<V>>, visited : ISet<Vertex<V>>, handler : {V => ()}) {
public fun traverse(pending : IPushPop<Vertex<V>>, visited : ISet<Vertex<V>>, handler : {(V) : Unit}) {
vertices.foreach {
if (!visited.add(it))
continue
+4 -3
View File
@@ -1,4 +1,4 @@
[inline] fun with<T>(receiver : T, body : T.{=> ()}) = receiver.body()
[inline] fun with<T>(receiver : T, body : T.{() : Unit}) = receiver.body()
fun example() {
@@ -8,7 +8,8 @@ fun example() {
}
System.out.{
}
println("foo");
print("bar");
}()
}
+58
View File
@@ -0,0 +1,58 @@
virtual class IEquality {
fun equals(other : Any) : Boolean
= (this as java.lang.Object).equals(other as java.lang.Object)
}
virtual class IHashable : IEquality {
val hashCode() : Integer {
get() = (this as java.lang.Object).hashCode()
}
}
virtual class IMap<K, V> {
fun get(key : K) : V
fun set(key : K, value : V) : V
fun remove(key : K) : V
fun containsKey(key : K) : Boolean
}
class HashableWrapper(wraps val obj : Any) : IHashable
// equals and hashCode implementations are inherited
[inline] fun Any.hashable() : HashableWrapper = new HashableWrapper(this)
virtual class IHashingStrategy<K> {
fun equals(a : K, b : K) : Boolean
fun hashCode(a : K) : Integer
}
class DefaultHashingStrategy<in K : IHashable> : IHashingStrategy<K> {
override fun equals(a : K, b : K) : Boolean = a.equals(b)
override fun hashCode(a : K) : Integer = a.hashCode
}
class JavaObjectHashingStrategy<K> : IHashingStrategy<K> {
override fun equals(a : K, b : K) : Boolean
= a.hashable().equals(b)
override fun hashCode(a : K) : Integer
= a.hashable().hashCode
}
class HashMap<K, V> : IMap<K, V> {
private [inline] fun hashCode(a : K) = a.hashable().hashCode
private [inline] fun equals(a : K, b : K) = a.hashable() == b
// everything else uses these equals() and hashCode()...
}
class StrategyHashMap<K, V>(hashingStrategy : IHashingStrategy<K>) : IMap<K, V> {
this() where (K : IHashable) : this(new DefaultHashingStrategy<K>()) {}
// where !(K : IHashable)
this() : this(new JavaObjectHashingStrategy<K>()) {}
//...
}
+2 -2
View File
@@ -1,4 +1,4 @@
type Comparison<in T> = {T, T => Int}
type Comparison<in T> = {(T, T) : Int}
fun naturalOrder<in T : IComparable<T>>(a : T, b : T) : Int = a.compareTo(b)
@@ -8,7 +8,7 @@ enum class ComparisonResult {
LS,EQ, GR
}
type MatchableComparison<in T> = {T, T, => ComparisonResult}
type MatchableComparison<in T> = {(T, T) : ComparisonResult}
fun asMatchableComparison<T>(cmp : Comparison<T>) : MatchableComparison<T> = {a, b =>
val res = cmp(a, b)