[Sorry, I accidentally set the Reply-To incorrectly in my previous msg;
corrected here.  I don't need a CC in responses.]

* Jan Mercl <0xj...@gmail.com> [220318 12:04]:
> On Fri, Mar 18, 2022 at 4:53 PM 'Axel Wagner' via golang-nuts
> <golang-nuts@googlegroups.com> wrote:
> > I don't really know the answer to your question. I can't think of
> > anything which would break. That being said, I also don't think it's
> > something that really needs "fixing", as it seems a pretty uncommon
> > concern.
> 
> I don't think there's anything ambiguous here. The very first sentence
> here: https://go.dev/ref/spec#Constant_expressions says
> 
> """"
> Constant expressions may contain only constant operands and are
> evaluated at compile time.
> """"
> 
> f() does not fill that requirement for sure, it's not a constant
> operand and it cannot be evaluated at compile time (in the first
> approximation).
> 
> TBH, I thought that it was all clear from my first answer in this
> thread. The compiler error confirms the specs.

If you had included the above quote in your original response, I would
have agreed with you.  However, your other msg only contained a
playground link that demonstrated that the compiler believed the
expression to be non-constant without giving any explanation of why that
was correct according to the spec.

Axel's original response clearly explained why the spec mandated the
behavior that the OP was questioning.

On the other hand, Axel's response left me wondering why the part of the
spec he quoted was written the way it was, while your quote above at
least superficially justifies the parts of the spec Axel quoted which I
was questioning; thanks.

My question stems from the fact that

  var a = [3]{1, 2, f()}
  const x = len(a)

compiles with the obvious results, but

  const x = len([3]{1, 2, f()})

does not compile.  I.e. the value obtained from evaluating a composite
literal expression of array type has a constant length, so why is taking
the len() of that value different from assigning that value to a
variable and then taking the len() of the variable?

I was really looking for the language designers to chime in with a
justification, but I guess I can accept "we don't want constant
expressions to be allowed to have side effects" as the answer.

...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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/YjS9iaoKXSy4pQ0X%40basil.wdw.

Reply via email to