Object literals and class objects
This commit is contained in:
@@ -1,17 +1,16 @@
|
|||||||
class Foo(bar : Bar) {
|
class Foo(bar : Bar) {
|
||||||
|
class object {
|
||||||
|
private val cache = HashMap<Bar, Foo>()
|
||||||
|
|
||||||
} static {
|
// Factory method:
|
||||||
private val cache = HashMap<Bar, Foo>()
|
Foo() = Foo(null);
|
||||||
|
|
||||||
Foo() {
|
// Factory method:
|
||||||
return Foo(null);
|
Foo(bar : Bar) = cache.cachedLookup(bar, this(bar))
|
||||||
}
|
}
|
||||||
|
|
||||||
Foo(bar : Bar) {
|
fun doFoo() {
|
||||||
if (bar in cache.keys) return cache[bar]
|
// ...
|
||||||
val newFoo = this(bar) // calls the primary constructor
|
|
||||||
cahce[bar] = newFoo
|
|
||||||
return newFoo
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,3 +23,12 @@ val foo = Foo(Bar())
|
|||||||
class FooSub(S : String) : Foo() { // Delegates to Foo created as a result of the factory method Foo()
|
class FooSub(S : String) : Foo() { // Delegates to Foo created as a result of the factory method Foo()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Map<K, V>.cachedLookup(key : K, lazy value : V) : V {
|
||||||
|
var v = this[key]
|
||||||
|
if (v == null) {
|
||||||
|
v = value
|
||||||
|
this[key] = value
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
@@ -30,11 +30,15 @@ class Example(a : Foo, i : Int) : Bar(i), Some {
|
|||||||
// h5. Grammar
|
// h5. Grammar
|
||||||
|
|
||||||
memberDeclaration
|
memberDeclaration
|
||||||
|
: classObject
|
||||||
: constructor
|
: constructor
|
||||||
: method
|
: method
|
||||||
: property
|
: property
|
||||||
: class //TODO : static
|
: class
|
||||||
// : constant ???
|
;
|
||||||
|
|
||||||
|
classObject
|
||||||
|
: "class" objectLiteral
|
||||||
;
|
;
|
||||||
|
|
||||||
constructor
|
constructor
|
||||||
@@ -51,7 +55,7 @@ super
|
|||||||
;
|
;
|
||||||
|
|
||||||
method
|
method
|
||||||
: methodModifier* "fun" SimpleName functionParameters (":" type)? functionBody?
|
: methodModifier* "fun" SimpleName typeParameters? functionParameters (":" type)? functionBody?
|
||||||
;
|
;
|
||||||
|
|
||||||
functionParameters
|
functionParameters
|
||||||
|
|||||||
@@ -11,9 +11,8 @@ expression
|
|||||||
: expressionWithPrecedences
|
: expressionWithPrecedences
|
||||||
: match
|
: match
|
||||||
: if
|
: if
|
||||||
: "new" constructorInvocation
|
: "new" constructorInvocation // TODO: Do we need "new"?, see factory methods
|
||||||
: anonymousInnerClassInstantiation // TODO
|
: objectLiteral
|
||||||
// TODO: list comprehension
|
|
||||||
: jump
|
: jump
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -159,6 +158,23 @@ arrayAccess
|
|||||||
: "[" expression "]"
|
: "[" expression "]"
|
||||||
;
|
;
|
||||||
|
|
||||||
anonymousInnerClassInstantiation
|
objectLiteral
|
||||||
: "object" delegationSpecifier{","} classBody
|
: "object" delegationSpecifier{","} classBody?
|
||||||
;
|
;
|
||||||
|
|
||||||
|
/* Factory methods:
|
||||||
|
|
||||||
|
objectLiteral
|
||||||
|
: "object" delegationSpecifier{","} ("{" objectLiteralMember{","} "}")?
|
||||||
|
;
|
||||||
|
|
||||||
|
objectLiteralMember
|
||||||
|
: memberDeclaration
|
||||||
|
: factoryMethod
|
||||||
|
;
|
||||||
|
|
||||||
|
factoryMethod
|
||||||
|
: accessModifier? SimpleName typeParameters? functionParameters functionBody
|
||||||
|
;
|
||||||
|
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user