Final should work on variables too

Description

See this: http://blog.adamcameron.me/2015/04/lucee-5-beta-final-keyword-redux.html

TL;DR:

This should be possible:

// FinalVariable.cfc component { final public publicVar = "set in CFC" }

As should this:

function f(final string x){ x = "something else" echo(x) }

I think there's a real use case for the latter to make sure passed-by-reference-copy values don't / can't get changed.

I also think if you're implementing final, it should be implemented comprehensively/completely.

is duplicated by

Activity

Show:

Zac Spitzer 12 August 2021 at 15:07

Adam Cameron 12 August 2021 at 07:26

This seems to be partially implemented. Consider this code:

// C.cfc component { static { final static.staticProperty = "value" } final this.instanceProperty = "value" }

 

 

<cfscript> // test.cfm import C writeOutput("Static property: #C::staticProperty#<br>") try { C::staticProperty = "new value" } catch (any e) { writeOutput("Exception type: [#e.type#]<br>") writeOutput("Exception message: [#e.message#]<br>") writeOutput("Exception detail: [#e.detail#]<br>") } o = new C() writeOutput("Instance property: #o.instanceProperty#<br>") try { o.instanceProperty = "new value" writeOutput("Instance property: #o.instanceProperty#<br>") } catch (any e) { writeOutput("Exception type: [#e.type#]<br>") writeOutput("Exception message: [#e.message#]<br>") writeOutput("Exception detail: [#e.detail#]<br>") } </cfscript>

 

Run:

 

Static property: value Exception type: [expression] Exception message: [Cannot update key [STATICPROPERTY] in static scope from component [nonWheelsTests.final.C], that member is set to final] Exception detail: [] Instance property: value Instance property: new value

So it works for statics, but not for instance variables.

(NB: this works how I would expect on CF… the second assignment errors-out too)

I also consider there to be a bug here. If it doesn’t work on non-static variables, then it should throw a compiler exception if one tries to use it. Otherwise you’re permitting misleading code and behaviour into people’s apps, which is less than ideal.

Michael Offner 15 April 2015 at 12:33

yeah of course, because of that we use the word "final" in all this cases.
what i tried to point out is that with method and component, "final" is used as opposite to "abstract", so you cannot extend that.
With variables this is something different not related.

In other words when you use final with components/methods you will not miss the support for final variables, because this feature have no direct relation.
So it is not like you have try, catch but not finally.
It maybe has the same name but that's where the relation ends.

i don't say this will not come on a later state, but properly not with Lucee 5.

Adam Cameron 15 April 2015 at 11:42
Edited

I don't think it's "completely different". For a class it means the class cannot be changed (ie: extended); for a method: the method cannot be changed (ie: overridden); for a variable: the variable cannot be changed (ie: the value cannot change). In the context of each "kind" of entity, it means the same thing, even if the actual way it's implemented and how it is used is different.

Still, that's semantics.

The point is, conceptually the whole lot are grouped together: if implementing final, the whole lot should be implemented. I say that just for clarity, not because I think you're not agreeing.

Michael Offner 15 April 2015 at 11:00

agree, but also have in mind that we speak about a complete different concept, final with component/functions is a rule you apply on your code, like access modifiers as well.
using final with variables means to make them read only or better you lock the variable name.

BTW at least in java the final modifier for variables does not mean i cannot change the object itself, it only means i cannot change the variable.

// Java Example final StringBuilder sb=new StringBuilder(); sb.append("Susi");// i can change the inner state of the object sb=null;// compiler exception, i cannot override the variable!

Details

Assignee

Reporter

New Issue warning screen

Before you create a new Issue, please post to the mailing list first https://dev.lucee.org

Once the issue has been verified, one of the Lucee team will ask you to file an issue

Affects versions

Priority

Created 15 April 2015 at 09:49
Updated 25 February 2022 at 12:40

Flag notifications