Default parameters, function type examples fixed, tuples with named entries
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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");
|
||||
}()
|
||||
|
||||
}
|
||||
@@ -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>()) {}
|
||||
|
||||
//...
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -64,7 +64,7 @@ functionRest
|
||||
;
|
||||
|
||||
functionParameters
|
||||
: "(" (parameter{","})? ")"
|
||||
: "(" ((parameter ("=" expression)?){","})? ")" // default values
|
||||
;
|
||||
|
||||
block
|
||||
|
||||
@@ -5,6 +5,7 @@ expression
|
||||
: tupleLiteral
|
||||
: listLiteral
|
||||
: mapLiteral
|
||||
: range
|
||||
: "null"
|
||||
: "this"
|
||||
: memberAccessExpression
|
||||
@@ -86,7 +87,7 @@ typeArguments
|
||||
;
|
||||
|
||||
valueArguments
|
||||
: "(" expression{","} ")"
|
||||
: "(" (SimpleName ":")? expression{","} ")"
|
||||
;
|
||||
|
||||
infixFunctionCall
|
||||
@@ -107,10 +108,10 @@ tupleLiteral // Ambiguity when after a SimpleName (infix call). In this case (e)
|
||||
: "(" expression{","} ")"
|
||||
;
|
||||
|
||||
functionLiteral
|
||||
functionLiteral // one can use "it" as a parameter name
|
||||
: "{" SimpleName "=>" expression "}"
|
||||
: "{" functionTypeContents "=>" expression "}"
|
||||
: "{" "(" parameter{","} ")" "=>" expression "}"
|
||||
: "{" "(" SimpleName{","} ")" "=>" expression "}"
|
||||
: "{" "(" parameter{","} ")" (":" type)? "=>" expression "}"
|
||||
;
|
||||
|
||||
constructorInvocation
|
||||
@@ -141,6 +142,10 @@ literalConstant
|
||||
: MultiLineStringTemplate
|
||||
;
|
||||
|
||||
range
|
||||
: "[" expression ".." expression "]"
|
||||
;
|
||||
|
||||
memberAccessExpression
|
||||
: (expression ".")? memberAccess
|
||||
;
|
||||
@@ -178,4 +183,4 @@ factoryMethod
|
||||
: accessModifier? SimpleName typeParameters? functionParameters functionBody
|
||||
;
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
/*
|
||||
|
||||
Foo<Bar<X>, T, Object> // user type
|
||||
{A, Object => Foo} // function type
|
||||
{ => Foo} // function with no arguments
|
||||
{(A, Object) : Foo} // function type
|
||||
{ () : Foo} // function with no arguments
|
||||
(A, B, C) // tuple type
|
||||
(a : A, b : B) // tuple type with named entries
|
||||
() // 0-ary tuple type (Unit)
|
||||
|
||||
*/
|
||||
@@ -33,7 +34,8 @@ functionTypeContents
|
||||
;
|
||||
|
||||
tupleType
|
||||
: "(" type{","} ")"
|
||||
: "(" type{","}? ")"
|
||||
: "(" parameter{","} ")" // tuple with named entries, the names do not affect assignment compatibility
|
||||
;
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user