Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug with using a @meta value more than once #54

Closed
zachwolfe opened this issue Oct 15, 2021 · 5 comments
Closed

Possible bug with using a @meta value more than once #54

zachwolfe opened this issue Oct 15, 2021 · 5 comments

Comments

@zachwolfe
Copy link

The code below fails to compile for me with the error memoized meta object int 'val' is not a portable type. Originally found this when trying to do something more interesting, with a @meta loop instead of a block, and a loop variable instead of @meta int val = 0. Removing one of the @meta val statements makes the error go away.

struct MyStruct {
    @meta {
        @meta int val = 0;
        void member_function() {
            @meta val;
            @meta val;
        }
    }
};

P.S. Great work on Circle! Found out about it last week. Many of the features (simple compile-time metaprogramming, single-source shaders, etc.) are things I've been wishing for in a programming language for a long time, so it's been really cool to actually be able to try some of them out.

@seanbaxter
Copy link
Owner

This is a thorny aspect of C++. Member function definitions are parsed after the enclosing class definition is completed. Like, the closing brace of MyStruct is hit, then the compiler has to go back and parse the function definitions. This makes metaprograming of member functions from meta control flow in the class definition extremely tricky. That val declaration is actually destroyed before either of its uses. So what I do is memoize the meta objects when a member function is declared, so that they'll be available when the definition is parsed.

That is probably where this issue is. What are you trying to achieve? There may be some more robust way to approach that, or maybe a different way to do metaprogramming inside class definitions.

@zachwolfe
Copy link
Author

Huh, interesting. That explains the wording of the error message. Still, shouldn’t the memoized object be able to be used as many times as you want within the member function?

As for what I was trying to achieve, nothing important. Trying to synthesize vector swizzle functions, like vector.zyx(). I know it’s already built in to the compiler; I’m just fooling around. :)

@seanbaxter
Copy link
Owner

Oh, ya well it is a bug.

BTW, I have vector swizzles as a language builtin:
https://github.com/seanbaxter/shaders#vector-swizzle

@zachwolfe
Copy link
Author

Ah, ok. We’re on the same page then.

Yep, I know they are built in. I was just fooling around with Circle’s metaprogramming features.

@seanbaxter
Copy link
Owner

Thanks for the report. Fixed for build 139. Will upload that tomorrow or Monday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants