Objectless Calls

Revision as of 09:24, 14 March 2008 by Paulb (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Construction.png Not Ready for Review: This Page is Under Development!

In the proposal the author highlight some of the advantages of adding a new mechanism to the Eiffel language to permit objectless (known as static in C/C++ and derivative languages) calls. The introduction of objectless calls in Eiffel resolves issues regarding contract guarantees, creation contracts, overuse of "shared" inheritance and can solve a performance related issue using dynamic dispatch.

It is in the author's opinion that objectless calls are a necessary addition for the continued improvement of the Eiffel language, and were overlooked when defining the ECMA standard.

The Creation Problem

Overuse of Inheritance

The Performance Problem

A Proposal

In order to complete this document the author indicates a proposal for such a mechanism for permitting objectless calls.

Due to objectless calls being a language mechanism rather than a run-time attribute (such as a once's process/thread status) a new keyword is going to be required, minimizing possible conflicts with feature names use in existing systems. For this proposal the author suggest objectless, indicating a objectless call.

Objectless class should be permitted on function, both regular and once functions. It is up to debate to indicate if procedures and class attributes are permitted to be objectless

The objectless keyword should appear as a routine modifier, as with the frozen keyword. Just as the use of frozen is used to dictate to subclasses a routine cannot be redefined/undefined, objectless has a similar standing when being called, indicating (not dictating) to clients to use the static calling convention. Secondary to the argument such placement is clear to a reader the call is objectless. The only alternative foreseeable placement for an objectless keyword would be a prior to a routine's type keyword do, once, deferred, external, however in the case of the previous arguments of readability, it's the author's opinion the keyword should not appear there. There may be a suggestion to using a routine's note clause to indicate objectless status but this is a bad idea, not only for readability but because note clauses are informative structures that should not change routine semantics.

class A
 
feature -- Access
 
	objectless f: STRING_8
			-- Proposal for an objectless function.
			-- This seems much more readable
		do
		end
 
	f: STRING_8
			-- Proposal for an objectless function, alternative style.
			-- Less readable as an objectless call
		objectless do
		end
 
end

Using objectless is an indication to client, use the static calling convention is permitted but is not necessary. Making the same call on an attached entity will make


Solving the Creation Problem

class A
 
create
	make
 
feature {NONE} -- Initialization
 
	make (a_arg: ?VALUE)
		require
			a_arg_is_valid_value: is_valid_value (a_arg)
		do
			...
		end
 
feature -- Query
 
	objectless is_valid_value (a_value: ?VALUE): BOOLEAN
			-- Determines if an argument is valid
		do
			Result := a_value /= Void and then a_value.has_value
		ensure
			a_value_attached: Result implies a_value /= Void
			a_value_has_value: Result implies a_value.has_value
		end
 
end

For clients wanting to create an instance of A, the following code can now be written and guarenteed.

process (a_value: ?VALUE)
		-- Processes a value node.
	local
		l_a: A
	do
		if {A}.is_valid_value (a_value) then
				-- The value is valid, so we can safely create an instance of {A}.
			create l_a.make (a_value)
			...
		end
	end

Extensions

One extension to the proposal and possible advantage over languages with a similar mechanism is the possibility to redefine a objectless routine. The semantics of this have clear rules that govern the behavoir of calls and the calling convention used. This extension requires consideration now because of impacts on user code in the future if the language is to adopt the extension.

For the purpose of explaining the semantics of redefinition, the following classes are used.

class A
 
feature -- Access
 
	objectless f: STRING do ... end
 
end
class B
 
inherit 
	A
 
end

Objectless Redefinition

An objectless routine may be redefined objectlessly with redefined functionality.

When used as a client, calling {A}.f will always take the version from A. When calling {B}.f the version from A will be called unless B redefines f:

class B
 
inherit 
	A redefine f end
 
feature -- Access
 
	objectless f: STRING do ... end
 
end

Still, when call {A}.f the version from A will be taken, but calling {B}.f will call the version from B as f has been redefined objectlessly.

Instance Redefinition

An objectless routine may be redefined to an instance routine but not the other way around. This is permitted because the original objectless routine requires no access to instance attributes or instance routines of the class. An instance redefined objectless routine may call the precursor safely in this respect.

class B
 
inherit 
	A
		redefine
			f
		end
 
feature -- Access
 
	f: STRING
		do
			Result := Precursor {A}.twin
			Result.as_lower
		end
 
end

In instance redefinition, when call {A}.f the version from A will be taken because a client called f using the static calling convention. However, attempting to call {B}.f will result in a compile-time error because f is not accessible statically, since it has been redefined as an instance routine.

Instance Call Semantics

When calling f on an attached entity, of type A or a subtype, the call semantics behave in the same way as every other call - using dynamic dispatch. If an instance of B is polymorphically assigned to an instance of A and B redefines f, either objectlessly or otherwise, the call is made on the dynamic type of the attached entity, in this case B.

From within the confines of A, or a subclass of it, an an unqualified call to f the call should be treated in the same way as using an attached entity Current.f, that is, the call is dynamically dispatched. This behavoir can be overridden using the static access calling convention.

For example, when B redefines f, in A, it can be written {A}.f to ensure the version of f in A is called. Conversely in A calling f unqualified will dispatch the call, resulting in f's implementation in B being called.