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

Compile error on code snipet from https://zig.guide/language-basics/enums #238

Open
LesserSpottedAustralianSquirel opened this issue May 13, 2024 · 0 comments

Comments

@LesserSpottedAustralianSquirel

Hi

Your code snippet runs fine in global but not in main for enums. I believe it should run in both. Sorry if I have the wrong end of the stick. Bit of a zig noob. Running zig stable 12.

If I rem out the function in the enum it works fine. Perhaps enums with functions can only be in namespaced global if referencing themselves?

Thanks

Code is as follows

cat main.zig
const std = @import("std"); // get the standard lib

//zig version
//0.12.0
// nb latest stable release zig-linux-x86_64-0.12.0

//// this works fine in global
//const Suit = enum {
// clubs,
// spades,
// diamonds,
// hearts,
// pub fn isClubs(self: Suit) bool {
// return self == Suit.clubs;
// }
//};
//
//
//

pub fn main() void

{

std.debug.print("Do I compile? (sanity check)\n",.{});

//https://zig.guide/language-basics/enums

/////////////
//code
/////////////

//const Suit = enum {
// clubs,
// spades,
// diamonds,
// hearts,
// pub fn isClubs(self: Suit) bool {
// return self == Suit.clubs;
// }
//};
//

//
/////////////
// Result
/////////////

//zig run main.zig
//main.zig:21:26: error: use of undeclared identifier 'Suit'
// pub fn isClubs(self: Suit) bool {
// ^~~~
//main.zig:16:7: error: unused local constant
//const Suit = enum {
// ^~~~
//

//
// Zig in Depth: enum and union Dude the Builder
// copied directly from https://www.youtube.com/watch?v=Q_ltZnZlLZA&list=PLtB7CL7EG7pCw7Xy1SQC53Gl8pI7aDg9t&index=8

/////////////
//code
/////////////
//
//const Color = enum {
// red,
// green,
// blue,
// // Can have methods
// fn isRewd(self: Color) bool {
// return self == .red;
// }
//};
//

/////////////
// Result
/////////////
//
//zig run main.zig
//main.zig:37:18: error: use of undeclared identifier 'Color'
// fn isRewd(self: Color) bool {
// ^~~~~
//main.zig:32:7: error: unused local constant
//const Color = enum {
// ^~~~~

} // end main

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

1 participant