Eiffel Implicit Conversions PreviousNext

In Eiffel it is possible to have code of the form:

	local
		foo: FOO
		bar: BAR
	do
		...
		foo := bar
		...
	end
even if BAR does not conform to FOO. The mechanism allowing that is called conversion, and is specified in convert clauses near the top of class FOO or BAR. For example in class CHARACTER_8 we can see:
	convert
		...
		to_character_32: {CHARACTER_32}
indicating that objects of type CHARACTER_8 can be converted to CHARACTER_32 using routine to_character_32.

This mechanism looks nice at first sight, and we can end up with a lot of such conversions in the code. This can be an issue because these conversions are implicit in the code, and the user will not necessarily notice that a potentially time consuming or memory consuming feature call is involved in what looks like a simple assignment at first glance.

gedoc provides two formats to help with this issue. One to get the list of implicit conversions in the code, and another one to replace them with explicit conversions (i.e. explicit calls to conversion features).

Showing implicit conversions

Here is how to get the list of all implicit conversions in all classes of a project:

	gedoc --format=implicit_converts project.ecf
where project.ecf is an ECF file describing the Eiffel project. And to restrict to just the conversions from FOO to BAR:
	gedoc --format=implicit_converts --variable=convert=FOO->BAR project.ecf
where FOO and BAR are class names, possibly containing wildcards such as CHARACTER_* or ?(READABLE_)STRING_8. Depending on the underlying operating system and shell, the entire --variable option may need to be enclosed in double quotes when the wildcards contain characters recognized by the shell.

The output will contain lines of the form:

	[CONVERT] MY_CLASS (85,30): conversion from 'CHARACTER_8' to 'CHARACTER_32' using feature `to_character_32`.
indicating that there is an implicit conversion from CHARACTER_8 to CHARACTER_32 in class MY_CLASS at line 85 and column 30.

Making them explicit

Here is how to make explicit all implicit conversions in a project:

	gedoc --format=explicit_converts --force project.ecf
or just those from STRING_32 (or READABLE_STRING_32) to STRING_8 (or READABLE_STRING_8):
	gedoc --format=explicit_converts --force --variable=convert=?(READABLE_)STRING_32->?(READABLE_)STRING_8 project.ecf
In addition to showing the list of implicit conversions, these commands will make them explicit in the class text. For example if we have:
	local
		s8: STRING_8
		s32: STRING_32
	do
		...
		s8 := s32
		...
	end
gedoc will regenerate the text of the class as follows:
	local
		s8: STRING_8
		s32: STRING_32
	do
		...
		s8 := s32.as_string_8
		...
	end
assuming that there is a conversion routine as_string_8 declared in class STRING_32 to convert to STRING_8

Note that text of the classes which do not contain implicit conversions will not be regenerated. The command-line option --force tells gedoc to overwrite the file containing the class being modified. Without this option, files will not be overwritten and gedoc will emit an error message. Alternatively, the command-line option --interactive can be used. It will ask confirmation to the user before overwriting files. If you want to generate the modified files in a directory different from the one containing the original class text files, you should use the command-line option --output.


Copyright © 2020, Eric Bezault
mailto:ericb@gobosoft.com
http://www.gobosoft.com
Last Updated: 30 May 2020
HomeTocPreviousNext