Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Just do

    type Username string
And replace

      return Username{username}
with

      return Username(username)


The problem there is that you lose the guarantee that the parser validated the string value.

A caller can just say:

    // This is returning an error for some reason, so let's do it directly.
    // username, err := parsers.NewUsername(raw)
    username := parsers.Username(raw)
You also get implicit conversions in ways you probably don't want:

    var u Username
    u = "<hello>" // Implicitly converts from string to Username


That's true I did not think of that.


If you do that, people outside the package can also do Username(x) conversions instead of calling NewUsername. Making value package private means that you can only set it from outside the package using provided functionality.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: