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

Equivalent of C++ (nameless) union with nameless structs #1849

Open
WarEagle451 opened this issue Jul 31, 2022 · 3 comments
Open

Equivalent of C++ (nameless) union with nameless structs #1849

WarEagle451 opened this issue Jul 31, 2022 · 3 comments
Labels
leads question A question for the leads team long term Issues expected to take over 90 days to resolve.

Comments

@WarEagle451
Copy link

WarEagle451 commented Jul 31, 2022

(C++)

template<typename T> struct vec4 { T x, y, z, w; };

struct color {
	union {
		vec4<float> data{ 0, 0, 0, 0};
		struct { float r, b, g, a };
	};
};

I understand that choice types are tagged unions, but there is no example or mention about how someone would implement the above code in Carbon. Also there is no mention of nameless unions. I would imagine it could be somewhat like below.

(Carbon)

class Vec4(template T:! Type) { var x: T; var y: T; var z: T; var w: T; };

class Color {
	choice {
		Data: Vec4(f32) = (.x = 0, .y = 0, .z = 0, .w = 0), // I hope this is correct
		struct { var R: f32; var G: f32; var B: f32; var A: f32; };
	};
};

While something like this isn't essential, the following points are why this should be added if it is not already;

  • Cleaner code
  • More readable code
  • Interoperability with C++
  • Straight forward to implement (As to write and understand)

Side note: Are you able to declare vars like var x, y, z, w: f32?

@geoffromer
Copy link
Contributor

Carbon will very likely eventually have support for un-tagged unions, but there isn't a design yet. I proposed one in #139, but dropped it because there were some tricky design questions to resolve, and I figured out a way to get it off the critical path for tagged unions, which was my main priority at the time. If you or someone else wants to propose a design for un-tagged unions, that might be a good starting point, although some aspects of it are pretty outdated by now.

Side note: Are you able to declare vars like var x, y, z, w: f32?

No, although you can do var (x: f32, y: f32, z: f32, w: f32) = .... A name binding always consists of a single name, then a :, then a type expression.

@WarEagle451
Copy link
Author

WarEagle451 commented Aug 2, 2022

if you or someone else wants to propose a design for un-tagged unions

Thanks, I'll probably do that when I have time

@github-actions
Copy link

github-actions bot commented Nov 1, 2022

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please comment or remove the inactive label. The long term label can also be added for issues which are expected to take time.
This issue is labeled inactive because the last activity was over 90 days ago.

@github-actions github-actions bot added the inactive Issues and PRs which have been inactive for at least 90 days. label Nov 1, 2022
@jonmeow jonmeow added long term Issues expected to take over 90 days to resolve. leads question A question for the leads team and removed inactive Issues and PRs which have been inactive for at least 90 days. labels Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
leads question A question for the leads team long term Issues expected to take over 90 days to resolve.
Projects
None yet
Development

No branches or pull requests

3 participants