diff --git a/examples/src/Builder.jetl b/examples/src/Builder.jetl index 85532ac7e7d..51c542a67ce 100644 --- a/examples/src/Builder.jetl +++ b/examples/src/Builder.jetl @@ -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 diff --git a/examples/src/Graph.jetl b/examples/src/Graph.jetl index 4ed01564310..9a8732d053e 100644 --- a/examples/src/Graph.jetl +++ b/examples/src/Graph.jetl @@ -24,11 +24,11 @@ class Graph { fun neighbours(v : Vertex) = edges.filter{it.from == v}.map{it.to} // type is IIterable> - fun dfs(handler : {V => ()}) { + fun dfs(handler : {(V) : Unit}) { val visited = new HashSet>() vertices.foreach{dfs(it, visited, handler)} - fun dfs(current : Vertex, visited : ISet>, handler : {V => ()}) { + fun dfs(current : Vertex, visited : ISet>, handler : {(V) : Unit}) { if (!visited.add(current)) return handler(current) @@ -36,7 +36,7 @@ class Graph { } } - public fun traverse(pending : IPushPop>, visited : ISet>, handler : {V => ()}) { + public fun traverse(pending : IPushPop>, visited : ISet>, handler : {(V) : Unit}) { vertices.foreach { if (!visited.add(it)) continue diff --git a/examples/src/With.jetl b/examples/src/With.jetl index 5e3c3fe02b0..cae143e7f21 100644 --- a/examples/src/With.jetl +++ b/examples/src/With.jetl @@ -1,4 +1,4 @@ -[inline] fun with(receiver : T, body : T.{=> ()}) = receiver.body() +[inline] fun with(receiver : T, body : T.{() : Unit}) = receiver.body() fun example() { @@ -8,7 +8,8 @@ fun example() { } System.out.{ - - } + println("foo"); + print("bar"); + }() } \ No newline at end of file diff --git a/examples/src/collections/HashMap.jetl b/examples/src/collections/HashMap.jetl new file mode 100644 index 00000000000..45feb068776 --- /dev/null +++ b/examples/src/collections/HashMap.jetl @@ -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 { + 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 { + fun equals(a : K, b : K) : Boolean + fun hashCode(a : K) : Integer +} + +class DefaultHashingStrategy : IHashingStrategy { + override fun equals(a : K, b : K) : Boolean = a.equals(b) + override fun hashCode(a : K) : Integer = a.hashCode +} + +class JavaObjectHashingStrategy : IHashingStrategy { + override fun equals(a : K, b : K) : Boolean + = a.hashable().equals(b) + override fun hashCode(a : K) : Integer + = a.hashable().hashCode +} + +class HashMap : IMap { + 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(hashingStrategy : IHashingStrategy) : IMap { + + this() where (K : IHashable) : this(new DefaultHashingStrategy()) {} + + // where !(K : IHashable) + this() : this(new JavaObjectHashingStrategy()) {} + + //... + +} \ No newline at end of file diff --git a/examples/src/util/Comparison.jetl b/examples/src/util/Comparison.jetl index 1318b496d54..1af0c99ff2e 100644 --- a/examples/src/util/Comparison.jetl +++ b/examples/src/util/Comparison.jetl @@ -1,4 +1,4 @@ -type Comparison = {T, T => Int} +type Comparison = {(T, T) : Int} fun naturalOrder>(a : T, b : T) : Int = a.compareTo(b) @@ -8,7 +8,7 @@ enum class ComparisonResult { LS,EQ, GR } -type MatchableComparison = {T, T, => ComparisonResult} +type MatchableComparison = {(T, T) : ComparisonResult} fun asMatchableComparison(cmp : Comparison) : MatchableComparison = {a, b => val res = cmp(a, b) diff --git a/grammar/src/class_members.grm b/grammar/src/class_members.grm index baec24de919..7f4a476c372 100644 --- a/grammar/src/class_members.grm +++ b/grammar/src/class_members.grm @@ -64,7 +64,7 @@ functionRest ; functionParameters - : "(" (parameter{","})? ")" + : "(" ((parameter ("=" expression)?){","})? ")" // default values ; block diff --git a/grammar/src/expressions.grm b/grammar/src/expressions.grm index 5cc459a05bc..a339a37cb3b 100644 --- a/grammar/src/expressions.grm +++ b/grammar/src/expressions.grm @@ -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 ; -*/ \ No newline at end of file +*/ diff --git a/grammar/src/types.grm b/grammar/src/types.grm index 918ed6a0635..017f0d2beb9 100644 --- a/grammar/src/types.grm +++ b/grammar/src/types.grm @@ -1,9 +1,10 @@ /* Foo, 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 ;