Skip to content

Commit

Permalink
Merge pull request #85 from leonardt/name-cb
Browse files Browse the repository at this point in the history
Add call back to set class name
  • Loading branch information
cdonovick authored Aug 1, 2019
2 parents c063ba8 + 37ee396 commit ad044f5
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion hwtypes/adt_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ def _fields_cb(cls, idx):
'''
return tuple(idx)

def _name_cb(cls, idx):
'''
Gives subclasses a chance define their name based on their idx.
'''
return '{}[{}]'.format(cls.__name__, ', '.join(map(lambda t : t.__name__, idx)))

def __getitem__(cls, idx) -> 'BoundMeta':
mcs = type(cls)
if not isinstance(idx, tp.Iterable):
Expand All @@ -142,7 +148,7 @@ def __getitem__(cls, idx) -> 'BoundMeta':
bases = [cls]
bases.extend(b[idx] for b in cls.__bases__ if isinstance(b, BoundMeta))
bases = tuple(bases)
class_name = '{}[{}]'.format(cls.__name__, ', '.join(map(lambda t : t.__name__, idx)))
class_name = cls._name_cb(idx)

t = mcs(class_name, bases, {'__module__' : cls.__module__}, fields=idx)
mcs._class_cache[cls, idx] = t
Expand Down

0 comments on commit ad044f5

Please sign in to comment.