Skip to content

[Feature Request] Reflection: inspect a type's attributes via attributesof or similar #3100

@NotsoanoNimus

Description

@NotsoanoNimus

At the moment, C3 does not have a way to inspect the attributes associated with a user-defined type. This would be a nice feature beyond the existing tagging feature because conditional behavior for the presence of builtin attributes could apply as well.

Here's a very contrived example:

// Sample changes to Standard Library code
<*
 @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'alloc_aligned' instead"
*>
macro alloc(Allocator allocator, $Type) @nodiscard
{
	$if @in("mustinit", ...$Type.attributesof):
		return ($Type*)calloc(allocator, $Type.sizeof);
	$else
		return ($Type*)malloc(allocator, $Type.sizeof);
	$endif
}
// ========================
struct MyType @mustinit   // assume `mustinit` is a built-in C3 language attribute
{
    int a;
    int b;
} 
MyType* x = alloc::alloc(mem, MyType);   // here, the buffer is now implicitly zeroized

Clarification: It's possible to define MyType here with a custom attribute that includes a tag - e.g., attrdef @ForceInit = @mustinit, @tag("force_init", 1); - but the key point of this request is to be able to inspect those C3 builtin attributes without the tags and attrdef at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions