Utility Classes :: Class
Overview
Let's say you have a situation like this:
class ClassA: @accepts(Self(), ClassB) def foo(self, b): ... class ClassB: @accepts(Self(), ClassA) def bar(self, a): ...
See the problem? You can't use ClassB in the signature for ClassA.foo because it hasn't been declared yet. Sure, you could reverse the order of the class declarations, but then you'd have the same problem with ClassB.bar.
The solution: the Class() utility class.
ClassB = Class("ClassB")
class ClassA:
@accepts(Self(), ClassB)
def foo(self, b):
...
class ClassB:
@accepts(Self(), ClassA)
def bar(self, a):
...
The Class() utility class allows deferring the lookup of the named class until it's actually needed for typechecking.
Note the idiom used above: assign the Class() instance to a variable with the same name as the desired class. This is much cleaner than inlining the call to Class().
Details
-
The
Class()constructor takes a single argument. If more or fewer arguments are provided, aTypeCheckErrorwill be raised.The value to this argument must be a string.
-
If the actual class named in the parameter (
"ClassB", above) has not been created by the time theClassis needed for typechecking, aNameErrorwill be raised. -
Classinstances that were created with the same class name parameter will compare (==) as equals.All instances that compare equal will also hash to the same value.