FAQ
Of course technically New is not a special identifier and there are no
special restrictions on it, so I'm looking for opinion here. As a
convention it was established early that it's good to name a function that
has a constructor-like role New or New<Something> and I'm aware that most
of the time, a New function returns a pointer. But it's often useful to
have a constructor that initializes a value and returns it directly. Is it
bad style to name such a function New?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dave Cheney at Jan 30, 2015 at 1:21 pm
    Nope, sounds reasonable to me.
    On Friday, 30 January 2015 23:58:29 UTC+11, Sonia Keys wrote:

    Of course technically New is not a special identifier and there are no
    special restrictions on it, so I'm looking for opinion here. As a
    convention it was established early that it's good to name a function that
    has a constructor-like role New or New<Something> and I'm aware that most
    of the time, a New function returns a pointer. But it's often useful to
    have a constructor that initializes a value and returns it directly. Is it
    bad style to name such a function New?
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Marvin Renich at Jan 30, 2015 at 1:29 pm

    * Sonia Keys [150130 07:58]:
    Of course technically New is not a special identifier and there are no
    special restrictions on it, so I'm looking for opinion here. As a
    convention it was established early that it's good to name a function that
    has a constructor-like role New or New<Something> and I'm aware that most
    of the time, a New function returns a pointer. But it's often useful to
    have a constructor that initializes a value and returns it directly. Is it
    bad style to name such a function New?
    Unless there is a very compelling reason to do otherwise (and I have not
    found one yet), the methods for a given type should either all be
    implemented on the value or all be implemented on a pointer to the
    value. The New... function should return the same type on which the
    methods are implemented (i.e. either pointer or value).

    Much of the time, the type will be a struct and the methods will be
    implemented on a pointer to the struct, but there are many reasons to do
    otherwise. One of my favorites is for error types, like this:

    type FrombolizeError int

    func (f FrombolizeError) Error() string {
         return fmt.Sprintf("Failed to frombolize at position %d", f)
    }

    ...Marvin

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Thwd at Jan 30, 2015 at 1:30 pm

    On Friday, January 30, 2015 at 1:58:29 PM UTC+1, Sonia Keys wrote:
    But it's often useful to have a constructor that initializes a value and
    returns it directly. Is it bad style to name such a function New?
    The criteria for choosing a pointer is purely functional, not stylistic.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jesper Louis Andersen at Jan 30, 2015 at 2:01 pm

    On Fri, Jan 30, 2015 at 1:58 PM, Sonia Keys wrote:

    Of course technically New is not a special identifier and there are no
    special restrictions on it, so I'm looking for opinion here. As a
    convention it was established early that it's good to name a function that
    has a constructor-like role New or New<Something> and I'm aware that most
    of the time, a New function returns a pointer. But it's often useful to
    have a constructor that initializes a value and returns it directly. Is it
    bad style to name such a function New?

    In addition to the other comments: many languages do not make the
    distinction between a value being boxed behind a pointer or not. Go
    somewhat edges itself into the same space, moreso than C++ or C. It would
    suggest you should return the type, pointer or not, which yields a safe
    program void of accidental mistakes. Hence, initialization as part of a
    'New'-call is nice because it makes it part of acquisition.


    --
    J.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 30, '15 at 12:58p
activeJan 30, '15 at 2:01p
posts5
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase