My function has to return that struct but if it doesn't find the data
then it has to return it empty:
--------
type bar struct { ... }
func (db *foo) Get(num uint32) *bar {
....
// Return an empty struct
var d bar
return d{}
}
--------
The error message is:
d is not a type
You don't have a type named 'd', you want to write:
> return d
You *really* should read the specs more carefully.
Your questions are becoming more and more trivial each day
Surma
My function has to return that struct but if it doesn't find the data
then it has to return it empty:
--------
type bar struct { ... }
func (db *foo) Get(num uint32) *bar {
....
// Return an empty struct
var d bar
return d{}
}
--------
The error message is:
d is not a type
On 10 abr, 12:23, Alexander Surma <alexander.su...@googlemail.com>
wrote:
Indeed, in the Go spec. says:
// An empty struct.
struct {}
On 10 abr, 12:25, chris dollin <ehog.he...@googlemail.com> wrote:
> On 10 April 2010 13:16, Joan Miller <pelok...@gmail.com> wrote:
>
> > How to return an empty struct?
>
> Do you mean a struct with no fields, or a struct with its
> zero value?
The best one to know easily if there is not data return by that
function.
> > My function has to return that struct but if it doesn't find the data
> > then it has to return it empty:
> > --------
> > type bar struct { ... }
>
> > func (db *foo) Get(num uint32) *bar {
>
> You're returning a pointer, not a struct at all. Is
> that what you meant?
Yes right, it's a pointer. In my code it returns a pointer.
> > ....
>
> > // Return an empty struct
> > var d bar
> > return d{}
> > }
> > --------
>
> > The error message is:
> > d is not a type
>
> And quite right too: d is a variable of type foo,
> not a type. (The go compiler's messages are
> usually pretty good -- thanks, writers!) The
> construct T{V1 ... Vn} requires T to be a type.
>
> You can either
>
> return d
> or
> return bar{}
>
> If you really meant the pointer type after all
>
> return &d
> return &bar{}
Thanks, it works using
return &bar{}
On 10 abr, 12:40, chris dollin <ehog.he...@googlemail.com> wrote:
If the function finds the data then it returns bar with its fields.
If that data is not found, then it has to return an empty struct (bar)
to know that it was not found.
On 10 abr, 12:25, chris dollin <ehog.he...@googlemail.com> wrote:
> On 10 April 2010 13:16, Joan Miller <pelok...@gmail.com> wrote:The best one to know easily if there is not data return by that
>
> > How to return an empty struct?
>
> Do you mean a struct with no fields, or a struct with its
> zero value?
function.
On 10 abr, 13:02, chris dollin <ehog.he...@googlemail.com> wrote:
> On 10 April 2010 13:52, Joan Miller <pelok...@gmail.com> wrote:
>
>
>
> > On 10 abr, 12:25, chris dollin <ehog.he...@googlemail.com> wrote:
> > > On 10 April 2010 13:16, Joan Miller <pelok...@gmail.com> wrote:
>
> > > > How to return an empty struct?
>
> > > Do you mean a struct with no fields, or a struct with its
> > > zero value?
> > The best one to know easily if there is not data return by that
> > function.
>
> Return two results, one your struct (or a pointer to it), the
> other a boolean saying whether the data is really there
> or not.
For this case, it's more simple returns one only value for then to
check if that struct is empty or not.
If the function finds the data then it returns bar with its fields.
If that data is not found, then it has to return an empty struct (bar)
to know that it was not found.
In the failure case, you're creating an instance of a struct with
default values, and expecting the client to somehow check every field
for validity? That's insane. Have you not worked in a language with
pointers before? Since you're returning a pointer, why don't you just
return nil? Or, better yet, why don't you do what practically every
example in the documentation does and use the comma-ok or comma-err
idiom?
On 10 abr, 13:49, Peter Bourgon <peterbour...@gmail.com> wrote:
I've not worked into a language with pointers and it's being something
frustrating for me (until I get used).
What is that language?
Lots of languages either don't have pointers, or discourage their use.
Personally, while I agree that it's often quite a good idea to have
pointers (depending heavily on language design), the better languages
strongly discourage using them. The profligate use of pointers is one
of the real weaknesses of C & C++.
OTOH, I don't understand go pointers yet. So far it looks as if they
are tightly constrained in how they can be used. If such is the case,
then their use is more a syntactic wart than a real problem. (E.g.,
given that go is a garbage collected language, I'm presuming that one
can't use a pointer to index beyond the bounds of an array that it is
defined on. If you can, this is a major weakness.)
Esko Luontola wrote:I think that Ruby, Python, and Smalltalk are such languages. In Java, also, most programmers don't deal with pointers. It's unusual in D. I'm not sure it's possible in Snobal. Etc.
On Apr 10, 5:51 pm, Joan Miller <pelok...@gmail.com> wrote:
I've not worked into a language with pointersWhat is that language?
OTOH, I don't understand go pointers yet.
So far it looks as if they are tightly constrained in how they can be used.
If such is the case, then their use is more a syntactic wart than a real problem.
(E.g., given that go is a garbage collected language, I'm presuming that one can't use a pointer to index beyond the bounds of an array that it is defined on. If you can, this is a major weakness.)
chris dollin wrote:
On 11 April 2010 07:57, Charles Hixson <charle...@earthlink.net <mailto:charle...@earthlink.net>> wrote:If you come from a C or C++ background, then I can understand this.
...
If such is the case, then their use is more a syntactic wart than
a real problem.
Why a "syntactic wart"? The semantics of such pointers is
pretty much essential in an imperative language, and
having syntax to reflect it doesn't strike me as particularly
warty.
You've gotten used to them. But a wart it is, and it causes lots of problems to people who aren't accustomed to them.
All the other languages that I mentioned (except, perhaps, Snobal) actually use pointers, but the programmers don't need to deal with them beyond remembering that they are handled differently from, say, integers (in Smalltalk even this wart is handled, but it's computationally expensive).
So it's no less powerful, and it's easier and more intuitive. (One
still needs to be wary about multiple references to the same mutable
object, of course.)
D definitely has value types.
And those languages don't have value types. You don't
get to pass around a struct {int, int} value, only a pointer
to it. You don't get to pass around an array [3] float64.
only a pointer to one, and the 3 gets lost as part of the type
and turns into a run-time-only value. Go allows you to
do this and that choice is -- I believe, has to be -- reflected
in the syntax.
And you *can* pass structs (and possibly static arrays [i.e., arrays that are of a size defined at compile time]). Java also has value types, though they tend to be less used, because they are more awkward.
Maybe the compiler could do more in the way of inference,
but I think people would still need to be able to express
their choice:
func (this TTT) brew( y int ) { ... }
TTT might be some type X or instead *X. Which do you
want? It makes a difference. If you want the compiler to
choose for you, what are the rules for making that choice?
Are you sure they're not as confusing to newcomers as
the existence and use of *T?
The basic idea here is that a reference type is a different type from the type it references...and the compiler knows which type each variable is,
and whether it can do an automatic conversion. So, being Cish:
int *xp, x;
then the compiler knows that if you write:
xp = x
that can only mean store the address of x into xp, and conversely. This means that the only "syntax" one need for referencing or dereferencing is the declaration and something equivalent to the dot-notation (e.g. a.b.catapult(c)) and c will be automatically referenced or dereferenced to fit the function declaration.
So it's no less powerful, and it's easier and more intuitive.
The basic idea here is that a reference type is a different type from the type it references...and the compiler knows which type each variable is, and whether it can do an automatic conversion. So, being Cish:
int *xp, x;
then the compiler knows that if you write:
xp = x
that can only mean store the address of x into xp, and conversely.