FAQ
I've had to rewrite an API that mostly dealt with maps to provide
interfaces instead; for boring reasons, we have a few different in-house
implementations of this interface.

The interface is more or less the following:

type AlmostMap interface {
Get(string) (string, ok)
Set(string) ()
}

Get has a second return value - whether or not the value was found. This is
comparable to the optional second value on getting a key from a map (or
when reading from a channel).

But when I use the interface, the compile requires multi-value assignment.
I know I can do this with _, but the annoying thing is requiring other
developers working on the project to do this boilerplate every time they
get a key where previously their code had assumed they were working with
maps - not to mention refactoring all of the existing code. Is it possible
to get the same "optional" second-value assignment in our own library code,
or is it magic that's restricted to builtins like receiving from a channel,
looking up keys in a map or using the range keyword?

--

Search Discussions

  • Chris dollin at Nov 5, 2012 at 11:11 am

    On 5 November 2012 09:16, Daniel Bryan wrote:
    I've had to rewrite an API that mostly dealt with maps to provide interfaces
    instead; for boring reasons, we have a few different in-house
    implementations of this interface.

    The interface is more or less the following:

    type AlmostMap interface {
    Get(string) (string, ok)
    Set(string) ()
    }

    Get has a second return value - whether or not the value was found. This is
    comparable to the optional second value on getting a key from a map (or when
    reading from a channel).

    But when I use the interface, the compile requires multi-value assignment. I
    know I can do this with _, but the annoying thing is requiring other
    developers working on the project to do this boilerplate every time they get
    a key where previously their code had assumed they were working with maps -
    not to mention refactoring all of the existing code. Is it possible to get
    the same "optional" second-value assignment in our own library code, or is
    it magic that's restricted to builtins like receiving from a channel,
    looking up keys in a map or using the range keyword?
    It's magic.

    I'd give your interface two Get methods, one of which
    returns just the value (zero if there isn't one) and the
    other of which returns valueOrZero, wasPresent.

    Naming them is going to be the tricky thing ...

    Chris

    --
    Chris "allusive" Dollin

    --
  • Minux at Nov 5, 2012 at 11:56 am

    On Mon, Nov 5, 2012 at 5:16 PM, Daniel Bryan wrote:

    I've had to rewrite an API that mostly dealt with maps to provide
    interfaces instead; for boring reasons, we have a few different in-house
    implementations of this interface.

    The interface is more or less the following:

    type AlmostMap interface {
    Get(string) (string, ok)
    Set(string) ()
    }

    Get has a second return value - whether or not the value was found. This
    is comparable to the optional second value on getting a key from a map (or
    when reading from a channel).

    But when I use the interface, the compile requires multi-value assignment.
    I know I can do this with _, but the annoying thing is requiring other
    developers working on the project to do this boilerplate every time they
    get a key where previously their code had assumed they were working with
    maps - not to mention refactoring all of the existing code. Is it possible
    to get the same "optional" second-value assignment in our own library code,
    or is it magic that's restricted to builtins like receiving from a channel,
    looking up keys in a map or using the range keyword?
    define a function like this:
    func MapGet(m AlmostMap, in string) string {
    if s, e := m.Get(in); e == nil { return s }
    else { panic(e) }
    }

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 5, '12 at 10:40a
activeNov 5, '12 at 11:56a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase