In the examples in your program, the first two do not
use the name "private" so they are allowed.
Russ
We're not.
There are other contexts in which they are different too,
such as the behavior when there are multiple variables
being declared.
> unfortunately, the subexpression "p.GetPrivate()" cannot be
> encapsulated into a new function because it is impossible to define
> its return type.
Yes. You should talk to the person who wrote that API.
Russ
On Mar 11, 8:22 pm, Russ Cox <r...@google.com> wrote:Well, but what does "shorthand" stand for? Implication (A=>B), or
> > That suggests that we are talking about 1:1 equivalence.
>
> We're not.
equivalence (A=B), or something else? I think "shorthand" isn't a
mathematical term. Shouldn't the language specification better choose
between the following two:
- Implication: If the compiler compiles "var v t = expression", then
it compiles "v := expression".
- Equivalence: The compiler compiles "var v t = expression" if and
only if it compiles "v := expression".
> There are other contexts in which they are different too,Well, I found this "API" in the code of a project that I am
> such as the behavior when there are multiple variables
> being declared.
>
> > unfortunately, the subexpression "p.GetPrivate()" cannot be
> > encapsulated into a new function because it is impossible to define
> > its return type.
>
> Yes. You should talk to the person who wrote that API.
>
> Russ
participating in. So, that would mean that I should talk to myself ...
and myself thinks that it would be good to put the blame on the
compiler for allowing me to write such nonsense ...
Russ
On Mar 11, 8:48 pm, Steven <steven...@gmail.com> wrote:Are you sure it is not you who should reread it?
> The spec says `x := expression` is shorthand for `var x = expression` (not
> `var x T = expression`), with the added ability that it can redeclare
> variables in the same scope, as long as it is still declaring at least one
> variable.
>
> Both work in this case. Please reread the spec for shorthand declaration.
The spec also says: "If the type is present, each variable is given
that type. Otherwise, the types are *deduced* from the assignment of
the expression list."
So, if the types are deduced, I am *not* accessing them and therefore
I am implicitly allowed to use unexported types there?
What does "deduced" stand for? I think it stands for "the types are
filled in automatically". This means that "var v = expression" should
be the exact same thing as "var v t = expression", assuming the
expression evaluates to type "t".
> The compiler shouldn't do anything to disallow something that is allowed inWhy don't you explain to me what "deduced" and "shorthand" stand for?
> the spec. And there's no reason for the spec to specifically disallow a
> behaviour that follows directly from both the rules and the implementation
> without extra work. If the behaviour isn't useful to you, don't use it.
It is not impossible, what is impossible is to refer to p.private
because is not exported, but this will work:
func get_it() interface{Func()} { return p.GetPrivate() }
It can look a bit strange, but it would look much better if somewhere
(main, or p, or somewhere else) you had defined an interface:
type Funcer interface{Func()}
and GetPrivate was called GetFuncer.
This can be useful in some cases. For example, see the encoding binary
package, where the types littleEndian and bigEndian implement the
ByteOrder interface but the package documentation is much clearer not
including all their methods.
--
- yiyus || JGL .
Deduce:
To reach a conclusion by applying rules of logic to given premises.
I thought that I would write a lengthier text, but I have decided not
to, since the probability of it landing in the language would be
really low since I have no actual power over the language
specification. But, at least, I would like to note that it is *not*
true that "Go's exported is a purely lexical concept". If it was a
purely lexical concept, you would be able to compile the following
code:
==========
package p
type T struct { a int }
func MakeT() T { return T{} }
==========a := p.MakeT() // Error
package main
import "p"
func main() {
println(&a)
}
==========
There is no lexical occurrence of "p.T" in the source code below
"package main". The fact that line "a := p.MakeT()" fails to compile
indicates that Go's notion of "exported" is not a purely lexical
concept.
I don't see the problem. Yes, neither of the two rules stem from the
/definition/ of exported vs non-exported. They're extra rules which
are not logical consequences of that definition.
> Was that supposed to be some joke?
It doesn't look like a joke to me.
Chris
--
Chris "allusive" Dollin
Yes, you're right. The copying restriction is not based in the
lexical rule. It is a separate add-on. Maybe we should drop it.
Russ