Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: allow to declare a method final. #7041

Closed
vicuna opened this issue Nov 13, 2015 · 4 comments
Closed

Feature request: allow to declare a method final. #7041

vicuna opened this issue Nov 13, 2015 · 4 comments

Comments

@vicuna
Copy link

vicuna commented Nov 13, 2015

Original bug ID: 7041
Reporter: @sbriais
Status: acknowledged (set by @xavierleroy on 2015-11-15T17:19:30Z)
Resolution: open
Priority: normal
Severity: feature
Category: language features
Monitored by: @gasche

Bug description

I think it would be useful to have a final qualifier to mark methods that cannot be overriden.

@vicuna
Copy link
Author

vicuna commented Nov 13, 2015

Comment author: @garrigue

I've thought of it a few times.
However, while it could be useful as a comment, it is not that useful from a compilation point of view: in ocaml you don't know statically the (super) class of an object, so a method being final doesn't give a way to look it up more efficiently (from outside).

On another note, this kind of additions have a very low probability of being accepted because objects in ocaml are not used much the way they are in OO languages. If you thought this by comparison with another language, you should maybe first check that there is not a more natural way to write your code in ocaml (maybe you don't need objects at all).

@vicuna
Copy link
Author

vicuna commented Nov 13, 2015

Comment author: @alainfrisch

while it could be useful as a comment

More like a compiler-checked assertion, which is much stronger than a comment and also useful to reason on the code.

not that useful from a compilation point of view

I don't think this was indeed the intention of the suggestion, but aren't there cases where one could benefit of "final", as in:

class c = object(this)
method foo x = this # bar (x + 1)
method final bar x = ...
end

?

Since one knows statically to what "this # bar" corresponds, this method call could be turned into a simple function call, no?

@vicuna
Copy link
Author

vicuna commented Nov 14, 2015

Comment author: @garrigue

More like a compiler-checked assertion, which is much stronger than a comment and also useful to reason on the code.

Right, the question is more how useful this would be in ocaml.
If this is really needed, we could have an attribute, which would trigger a warning.

Since one knows statically to what "this # bar" corresponds, this method call could be turned into a simple function call, no?

Method calls through self or an ancestor are already more efficient than from outside, as this just amounts to looking up the table at a fixed offset. Here is the code for it in the bytecode interpreter:

#define Lookup(obj, lab) Field (Field (obj, 0), Int_val(lab))
Instruct(GETMETHOD):
  accu = Lookup(sp[0], accu);
  Next;

This is much better than a dichotomic search.
Of course, this is an indirect call, and there is no inlining.
But doing inlining for objects would be a lot of work, as currently there is no static representation for classes.

@github-actions
Copy link

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant