Enhanced "Const"

Monkey Forums/Monkey Programming/Enhanced "Const"

Samah(Posted 2011) [#1]
Mark, could we get some more usage out of the Const keyword?
Essentially, I'd like it to do the following things:
Class Foo ... Const ' makes the class final so you can't extend it
Method Foo ... Const ' makes the method final so you can't override it

Obviously some keywords would be exclusive-or. It doesn't make sense to have both Abstract and Const on a method or class declaration. :)


TheRedFox(Posted 2011) [#2]
Someone's coming from a final Java background here...


AdamRedwoods(Posted 2011) [#3]
Can't you use "Final" or am I missing something?

I'd be nice (but not necessary) if we can make a Function() ... Const for C macro-like functions.


Samah(Posted 2011) [#4]
Someone's coming from a final Java background here...

Gasp... how ever did you guess? ;)


MikeHart(Posted 2011) [#5]
What is the use for this?


Samah(Posted 2011) [#6]
For library developers. Less ways to break means less bug reports. :)


marksibly(Posted 2011) [#7]
Hi,

I'd be in favor of using 'Final' here myself. I like that Const saves a new keyword, but I think it's a little confusing/misleading.

Any objections to 'Final'?


ziggy(Posted 2011) [#8]
Final is ok for me, I would raise an objection to Const


Samah(Posted 2011) [#9]
That's fine with me. :) I only suggested "Const" because it's an existing keyword and it does what "final" does in Java (for fields/variables).


NoOdle(Posted 2011) [#10]
I would have to agree that whilst it saves a keyword, it is a little misleading, +1 for Final. Nice suggestion Samah :)


Samah(Posted 2011) [#11]
Of course you should still be able to declare extended classes and overridden methods as Final.
Class Foo
End

Class Bar Extends Foo Final ' OK, Foo is not final, but Bar is
End

Class Test Extends Bar ' Fail, can't extend Bar
End

and...
Class Foo
  Method SomeMethod()
  End
End

Class Bar Extends Foo
  Method SomeMethod() Final ' overrides SomeMethod in Foo and makes it final
  End
End

Class Test Extends Bar
  Method SomeMethod() ' Fail, SomeMethod in Bar is final (even though in Foo it's not)
  End
End