Quantcast

final var with non-constant expression?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

final var with non-constant expression?

mikael.staldal
According to http://www.scala-lang.org/files/archive/spec/2.12/04-basic-declarations-and-definitions.html#value-declarations-and-definitions

a constant value definition "final val x = e" requires "e" to be a constant expression. However, the compiler happily accepts a final val declaration with a non-constant expression. Does the final modifier have any effect in that case?

If not, wouldn't it be good if the compiler warned, since the programmer might have put a non-constant expression there by mistake?

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: final var with non-constant expression?

Oliver Ruebenacker

     Hello,

  What "non-constant expression" did you try?

     Best, Oliver

On Tue, Jan 3, 2017 at 10:37 AM, Mikael Ståldal <[hidden email]> wrote:
According to http://www.scala-lang.org/files/archive/spec/2.12/04-basic-declarations-and-definitions.html#value-declarations-and-definitions

a constant value definition "final val x = e" requires "e" to be a constant expression. However, the compiler happily accepts a final val declaration with a non-constant expression. Does the final modifier have any effect in that case?

If not, wouldn't it be good if the compiler warned, since the programmer might have put a non-constant expression there by mistake?

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: final var with non-constant expression?

Jasper-M
In reply to this post by mikael.staldal
Hi,

The spec defines a "constant value definition" as a final val without type annotation and with a constant expression as its right-hand side. 
It doesn't say that every final val has to be a constant value definition. It's a special case of a final val.

Kind regards,
Jasper

Op dinsdag 3 januari 2017 16:37:27 UTC+1 schreef Mikael Ståldal:
According to <a href="http://www.scala-lang.org/files/archive/spec/2.12/04-basic-declarations-and-definitions.html#value-declarations-and-definitions" style="text-decoration:none;color:rgb(66,133,244);font-family:sans-serif;font-size:13.696px" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.scala-lang.org%2Ffiles%2Farchive%2Fspec%2F2.12%2F04-basic-declarations-and-definitions.html%23value-declarations-and-definitions\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGChmxN9xamauVjgQQ7e50RbWv1Ig&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.scala-lang.org%2Ffiles%2Farchive%2Fspec%2F2.12%2F04-basic-declarations-and-definitions.html%23value-declarations-and-definitions\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGChmxN9xamauVjgQQ7e50RbWv1Ig&#39;;return true;">http://www.scala-lang.org/files/archive/spec/2.12/04-basic-declarations-and-definitions.html#value-declarations-and-definitions

a constant value definition "final val x = e" requires "e" to be a constant expression. However, the compiler happily accepts a final val declaration with a non-constant expression. Does the final modifier have any effect in that case?

If not, wouldn't it be good if the compiler warned, since the programmer might have put a non-constant expression there by mistake?

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: final var with non-constant expression?

Jasper-M
By the way, in scala final always means "not able to extend or override". So yes, the final modifier will still have its usual effect, just not the extra effect of getting inlined automatically.

Op woensdag 4 januari 2017 10:40:02 UTC+1 schreef Jasper-M:
Hi,

The spec defines a "constant value definition" as a final val without type annotation and with a constant expression as its right-hand side. 
It doesn't say that every final val has to be a constant value definition. It's a special case of a final val.

Kind regards,
Jasper

Op dinsdag 3 januari 2017 16:37:27 UTC+1 schreef Mikael Ståldal:
According to <a href="http://www.scala-lang.org/files/archive/spec/2.12/04-basic-declarations-and-definitions.html#value-declarations-and-definitions" style="text-decoration:none;color:rgb(66,133,244);font-family:sans-serif;font-size:13.696px" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.scala-lang.org%2Ffiles%2Farchive%2Fspec%2F2.12%2F04-basic-declarations-and-definitions.html%23value-declarations-and-definitions\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGChmxN9xamauVjgQQ7e50RbWv1Ig&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.scala-lang.org%2Ffiles%2Farchive%2Fspec%2F2.12%2F04-basic-declarations-and-definitions.html%23value-declarations-and-definitions\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGChmxN9xamauVjgQQ7e50RbWv1Ig&#39;;return true;">http://www.scala-lang.org/files/archive/spec/2.12/04-basic-declarations-and-definitions.html#value-declarations-and-definitions

a constant value definition "final val x = e" requires "e" to be a constant expression. However, the compiler happily accepts a final val declaration with a non-constant expression. Does the final modifier have any effect in that case?

If not, wouldn't it be good if the compiler warned, since the programmer might have put a non-constant expression there by mistake?

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...