Sobeston / zig.guide

Repo for https://zig.guide content. Get up to speed with Zig quickly.
https://zig.guide
MIT License
647 stars 170 forks source link

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

Open LesserSpottedAustralianSquirel opened 4 months ago

LesserSpottedAustralianSquirel commented 4 months ago

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