...job for a getter function.

On Fri, Nov 8, 2019 at 9:31 AM Jake Montgomery <jake6...@gmail.com> wrote:

> The inability to have a type safe enum in Go has bothered me as well.
>
> While using a struct, as Peter suggests, does prevent accidental use of
> literals, it also prevents you from making your enum items constant.This
> means that the values can be changed, accidentally, or intentionally, in
> another package. For example:
>
> package enum
>
> import "fmt"
>
> type Status struct {
>     s string
> }
>
> var One = Status{"one"}
> var Two = Status{"two"}
>
> func PrintIt(stat Status) {
>     fmt.Println("Status is", stat.s)
> }
>
> But a client of your package can then do:
>
> func Foo() {
>
>     enum.One = enum.Two
>     enum.PrintIt(enum.One)
>
> }
>
> Which will print "two", and perminantly alter the meaning of enum.One.
>
>
> On Thursday, November 7, 2019 at 8:15:20 AM UTC-5, speter wrote:
>>
>> Hi bsr,
>>
>> I'd suggest to use a struct type with a single string field. It will
>> prevent conversion from untyped string constant "by mistake".
>> Moreover, if you make the string field unexported, you can limit new
>> instance creation to the declaring package, allowing to enforce predefined
>> values.
>> Unlike with some other languages, there is no memory or runtime overhead
>> due to encapsulating the string within a struct.
>>
>> HTH,
>> Peter
>>
>> On Thu, Nov 7, 2019 at 7:58 PM bsr <bsr...@gmail.com> wrote:
>>
>>> Hello,
>>>
>>> I am a long time user of go, but I always had the impression that below
>>> code would not work as string and Status are different type.
>>> I thought I need to explicitly convert as ```exec(Status("abc"))``` it
>>> to work.
>>>
>>> I think, this part of the spec may be the reason
>>> https://golang.org/ref/spec#Assignability
>>>
>>>    - x is an untyped constant <https://golang.org/ref/spec#Constants>
>>>    representable <https://golang.org/ref/spec#Representability> by a
>>>    value of type T.
>>>
>>> Is there a way I can prevent this behavior.
>>> I am using Status like an enum, and only predefined status values should
>>> be allowed.
>>>
>>>
>>>
>>>
>>> https://play.golang.org/p/4zsb7KtPBC6
>>>
>>> package main
>>>
>>> import (
>>>     "fmt"
>>> )
>>>
>>> type Status string
>>>
>>> func main() {
>>>     exec("abc")
>>> }
>>>
>>> func exec(s Status) {
>>>     fmt.Printf("Hello, %s", s)
>>> }
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "golang-nuts" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to golan...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/golang-nuts/a20a7034-19c3-410a-bc86-25deff38534f%40googlegroups.com
>>> <https://groups.google.com/d/msgid/golang-nuts/a20a7034-19c3-410a-bc86-25deff38534f%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/9df8504c-4309-43ab-86ee-518b52eb2e2f%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/9df8504c-4309-43ab-86ee-518b52eb2e2f%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 

*Michael T. jonesmichael.jo...@gmail.com <michael.jo...@gmail.com>*

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CALoEmQw2WZQjcmpRzue8xpX4JKOc3PsR_5TAcoWnaaS%3DxkR5CQ%40mail.gmail.com.

Reply via email to