4 ms·
In my example it would be accessible as Constants.FooBar and it would be of a type FlagID just as required. What else do you need? Mutability was dismissed in
by xaduha 4y ago
In my example it would be accessible as Constants.FooBar and it would be of a type FlagID just as required. What else do you need? Mutability was dismissed in the blog post too and if you're modifying something called Constants.FooBar it's kinda on you anyway.
- atombender 4y agoYour example does not use a type, so I fail to see how it's an equivalent example. What does your example give you aside from "namespacing" the values under a generic struct?
- xaduha 4y agoRead the whole comment, FlagID is there and function signature func IsEnabled(id FlagID) bool { would be satisfied just fine
- atombender 4y agoI was not replying to your proposal, but to the comment by "lenkite", which only defines "var Animal" and no type.
- xaduha 4y agoIt is the same thing, just needs a bit of imagination.
- goodoldneon 4y agoYour example wouldn't work for function param types, right? The following code compiles even though I'm not passing a valid flag ID: type FlagID string var Constants = struct { FooBar FlagID FizzBuzz FlagID }{ "FooBar", "FizzBuzz", } func p(flag FlagID) { fmt.Println(flag) } func main() { p("hi") }
- xaduha 4y agoYou're right and OP mentioned it at the start too. I guess your editor would show you what is expected there and that should be good enough for most cases. If it is some sort of a library though, then there should be a better way to do this that to pass some enums as parameters to functions. But yes, it is a shortcoming that Go should probably fix.
- goodoldneon 4y agoThis type of thing has been surprising to me as I've learned Go. It seems like Go makes a great effort around safety in a lot of ways, but sometimes will avoid safety and say "just be careful" instead