Remove enum type prefix when printing enum

This commit is contained in:
Bruce Hill 2025-03-16 17:07:22 -04:00
parent f4f5fd4fdd
commit e80571e36f
4 changed files with 10 additions and 11 deletions

View File

@ -63,9 +63,9 @@ func increment(arg:ArgumentType -> ReturnType):
...
>> increment(AnInt(5))
= ReturnType.AnInt(6)
= AnInt(6)
>> increment(SomeText("HI"))
= ReturnType.Nothiing
= Nothiing
```
This lets us have overlapping tag names for different types, but smartly infer

View File

@ -75,13 +75,12 @@ public Text_t Enum$as_text(const void *obj, bool colorize, const TypeInfo_t *typ
int32_t tag = *(int32_t*)obj;
NamedType_t value = type->EnumInfo.tags[tag-1];
if (!value.type || value.type->size == 0)
return Text$format(colorize ? "\x1b[36;1m%s\x1b[m.\x1b[1m%s\x1b[m" : "%s.%s", type->EnumInfo.name, value.name);
return Text$format(colorize ? "\x1b[1m%s\x1b[m" : "%s", value.name);
ptrdiff_t byte_offset = sizeof(int32_t);
if (value.type->align && byte_offset % value.type->align > 0)
byte_offset += value.type->align - (byte_offset % value.type->align);
return Text$concat(Text$format(colorize ? "\x1b[36;1m%s\x1b[m." : "%s.", type->EnumInfo.name),
generic_as_text(obj + byte_offset, colorize, value.type));
return generic_as_text(obj + byte_offset, colorize, value.type);
}
PUREFUNC public bool Enum$is_none(const void *x, const TypeInfo_t*)

View File

@ -17,11 +17,11 @@ func choose_text(f:Foo->Text):
func main():
>> Foo.Zero
= Foo.Zero
= Zero
>> Foo.One(123)
= Foo.One(123)
= One(123)
>> Foo.Two(123, 456)
= Foo.Two(x=123, y=456)
= Two(x=123, y=456)
>> one := Foo.One(123)
>> one.One
@ -59,7 +59,7 @@ func main():
>> choose_text(Foo.Four(1,2,3,4))
= "Four"
>> choose_text(Foo.Last("XX"))
= 'else: Foo.Last("XX")'
= 'else: Last("XX")'
i := 1
cases := [Foo.One(1), Foo.One(2), Foo.Zero]

View File

@ -218,12 +218,12 @@ func main():
!! ...
!! Enums:
>> yep := Enum.maybe(yes)
= Enum.Y(123) : Enum?
= Y(123) : Enum?
>> nope := Enum.maybe(no)
= none : Enum?
>> if yep:
>> yep
= Enum.Y(123)
= Y(123)
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")