Re: Good idea or gimmick: Go-style OO-programming in C++ ?
On Thursday, 28 February 2013 14:09:30 UTC+2, Stefan Ram wrote:
=D6=F6 Tiib <ootiib@hot.ee> writes:
No slightest bit of code. No way to add checks that verify that the
caller followed contract (preconditions) and if implementer
fulfilled it (post-conditions).
If someone wants to add such code, there are multiple ways.
For example, to use an abstract class instead of an interface.
One can not in Java: multiple inheritance is forbidden.
To complain that interfaces are not intended to contain such
code is like to complain that a cup of coffee does not
contain any food, so one cannot eat it if one is hungry.
But why interface may not contain checks that it is correctly used and impl=
emented? I constantly use exactly such interfaces:
class Interface
{
public:
/// doc describing what interface does for outside world
RetType AMethod( Param param )
{
if ( param.isBad() )
throw std::invalid_argument( "Interface::AMethod passed"
" argument 'param' should not be bad."=
);
if ( !isPreparedForAMethod() )
return RetType();
RetType ret( doAMethod( param ) );
if ( ret.isTrash() )
throw std::logic_error( "Interface::AMethod the implementation=
"
" returned trash that it should not.")=
;
return ret;
};
private:
/// doc describing what implementer should implement here
virtual bool isPreparedForAMethod() const = 0;
virtual RetType doAMethod( Param param ) = 0;
};
Ok, you say it is *not* interface but why should I care? For me it is exact=
ly
the *correct* way to implement an interface. It keeps concerns of interface
semantics and its implementation separated.
When one wants to eat something, one is free to order this in
addition or instead of coffee in the first place.
You can't in Java, that was my point.
(To validate value constraints, one can also add annotations
to a bean. See:
http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html
See yourself, there are no way how I call isBad() on argument or isTrash() =
on
return value.
.) There even are tricks to add implementations to an interface
in Java, IIRC:
Sure, there are hacks how to get somehow around the major shortcoming.
That is why I am surprised. They are so proud about that single inheritance
and code-less interfaces and then hack there and do tricks and are still
so proud about that.