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 classpath(entries : ClassPathEntry...) { ... }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun library(initializer : Library.{ => Library}) {
|
fun library(initializer : Library.{ () : Library}) {
|
||||||
val lib = new Library()
|
val lib = new Library()
|
||||||
lib.initializer()
|
lib.initializer()
|
||||||
return lib
|
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 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>>()
|
val visited = new HashSet<Vertex<V>>()
|
||||||
vertices.foreach{dfs(it, visited, handler)}
|
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))
|
if (!visited.add(current))
|
||||||
return
|
return
|
||||||
handler(current)
|
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 {
|
vertices.foreach {
|
||||||
if (!visited.add(it))
|
if (!visited.add(it))
|
||||||
continue
|
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() {
|
fun example() {
|
||||||
|
|
||||||
@@ -8,7 +8,8 @@ fun example() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
System.out.{
|
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)
|
fun naturalOrder<in T : IComparable<T>>(a : T, b : T) : Int = a.compareTo(b)
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ enum class ComparisonResult {
|
|||||||
LS,EQ, GR
|
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 =>
|
fun asMatchableComparison<T>(cmp : Comparison<T>) : MatchableComparison<T> = {a, b =>
|
||||||
val res = cmp(a, b)
|
val res = cmp(a, b)
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ functionRest
|
|||||||
;
|
;
|
||||||
|
|
||||||
functionParameters
|
functionParameters
|
||||||
: "(" (parameter{","})? ")"
|
: "(" ((parameter ("=" expression)?){","})? ")" // default values
|
||||||
;
|
;
|
||||||
|
|
||||||
block
|
block
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ expression
|
|||||||
: tupleLiteral
|
: tupleLiteral
|
||||||
: listLiteral
|
: listLiteral
|
||||||
: mapLiteral
|
: mapLiteral
|
||||||
|
: range
|
||||||
: "null"
|
: "null"
|
||||||
: "this"
|
: "this"
|
||||||
: memberAccessExpression
|
: memberAccessExpression
|
||||||
@@ -86,7 +87,7 @@ typeArguments
|
|||||||
;
|
;
|
||||||
|
|
||||||
valueArguments
|
valueArguments
|
||||||
: "(" expression{","} ")"
|
: "(" (SimpleName ":")? expression{","} ")"
|
||||||
;
|
;
|
||||||
|
|
||||||
infixFunctionCall
|
infixFunctionCall
|
||||||
@@ -107,10 +108,10 @@ tupleLiteral // Ambiguity when after a SimpleName (infix call). In this case (e)
|
|||||||
: "(" expression{","} ")"
|
: "(" expression{","} ")"
|
||||||
;
|
;
|
||||||
|
|
||||||
functionLiteral
|
functionLiteral // one can use "it" as a parameter name
|
||||||
: "{" SimpleName "=>" expression "}"
|
: "{" SimpleName "=>" expression "}"
|
||||||
: "{" functionTypeContents "=>" expression "}"
|
: "{" "(" SimpleName{","} ")" "=>" expression "}"
|
||||||
: "{" "(" parameter{","} ")" "=>" expression "}"
|
: "{" "(" parameter{","} ")" (":" type)? "=>" expression "}"
|
||||||
;
|
;
|
||||||
|
|
||||||
constructorInvocation
|
constructorInvocation
|
||||||
@@ -141,6 +142,10 @@ literalConstant
|
|||||||
: MultiLineStringTemplate
|
: MultiLineStringTemplate
|
||||||
;
|
;
|
||||||
|
|
||||||
|
range
|
||||||
|
: "[" expression ".." expression "]"
|
||||||
|
;
|
||||||
|
|
||||||
memberAccessExpression
|
memberAccessExpression
|
||||||
: (expression ".")? memberAccess
|
: (expression ".")? memberAccess
|
||||||
;
|
;
|
||||||
@@ -178,4 +183,4 @@ factoryMethod
|
|||||||
: accessModifier? SimpleName typeParameters? functionParameters functionBody
|
: accessModifier? SimpleName typeParameters? functionParameters functionBody
|
||||||
;
|
;
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
/*
|
/*
|
||||||
|
|
||||||
Foo<Bar<X>, T, Object> // user type
|
Foo<Bar<X>, T, Object> // user type
|
||||||
{A, Object => Foo} // function type
|
{(A, Object) : Foo} // function type
|
||||||
{ => Foo} // function with no arguments
|
{ () : Foo} // function with no arguments
|
||||||
(A, B, C) // tuple type
|
(A, B, C) // tuple type
|
||||||
|
(a : A, b : B) // tuple type with named entries
|
||||||
() // 0-ary tuple type (Unit)
|
() // 0-ary tuple type (Unit)
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@@ -33,7 +34,8 @@ functionTypeContents
|
|||||||
;
|
;
|
||||||
|
|
||||||
tupleType
|
tupleType
|
||||||
: "(" type{","} ")"
|
: "(" type{","}? ")"
|
||||||
|
: "(" parameter{","} ")" // tuple with named entries, the names do not affect assignment compatibility
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user