www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Static Analysis at Mozilla, must_override

reply bearophile <bearophileHUGS lycos.com> writes:
A video, Large-Scale Static Analysis of C++ code at Mozilla, video, July 9th,
2010:
http://vimeo.com/12614626


From the video I have seen that Mozilla developers have felt the need to add
this new attribute in C++ (implemented in JavaScript through their hydra
plug-ins and used with a define NS_MUST_OVERRIDE):
http://mxr.mozilla.org/mozilla-central/source/xpcom/analysis/must-override.js

I have not felt a need for it (do you see any need for it in your programs?),
because I have not written one million lines long C++ programs, but if
necessary I think it's not hard to add it to D as the attribute:

 must_override

(Alternative name:  to_override). It's similar to the keyword "abstract", it
requires overloading in derived classes, but it allows the implementation of
the method in the original class too (that's not abstract).


class Foo {
   must_override int foo() { return 1; }
}
class Bar1 : Foo {
  int foo() { return 2; } // OK
}
class Bar2 : Foo { // error, missing foo()
}


If the creation of custom attributes in user code is allowed, then a (scoped)
 must_override can be defined and tried out. To implement custom attributes a
good amount of static introspection is needed.

Bye,
bearophile
Jul 12 2010
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 07/12/2010 07:15 AM, bearophile wrote:
 A video, Large-Scale Static Analysis of C++ code at Mozilla, video, July 9th,
2010:
 http://vimeo.com/12614626


  From the video I have seen that Mozilla developers have felt the need to add
this new attribute in C++ (implemented in JavaScript through their hydra
plug-ins and used with a define NS_MUST_OVERRIDE):
 http://mxr.mozilla.org/mozilla-central/source/xpcom/analysis/must-override.js

I discussed this with Walter. The pattern is of pretty narrow usefulness, but the scenarios are quite compelling: - cloning - opCmp - opEquals Such functions, if ever used, must be overridden transitively. Andrei
Jul 12 2010
parent Michel Fortin <michel.fortin michelf.com> writes:
On 2010-07-12 10:36:05 -0400, Andrei Alexandrescu 
<SeeWebsiteForEmail erdani.org> said:

 On 07/12/2010 07:15 AM, bearophile wrote:
 A video, Large-Scale Static Analysis of C++ code at Mozilla, video, 
 July 9th, 2010:
 http://vimeo.com/12614626
 
 
  From the video I have seen that Mozilla developers have felt the need 
 to add this new attribute in C++ (implemented in JavaScript through 
 their hydra plug-ins and used with a define NS_MUST_OVERRIDE):
 http://mxr.mozilla.org/mozilla-central/source/xpcom/analysis/must-override.js

I discussed this with Walter. The pattern is of pretty narrow usefulness, but the scenarios are quite compelling: - cloning - opCmp - opEquals Such functions, if ever used, must be overridden transitively.

Not necessarily. The basic implementations of opCmp and opEqual in Object are quite good for a variety of cases (when comparing the reference pointer is good enough). You only need to override them when two different instances having the same value makes sense. As for cloning, if we had a little more runtime reflection it'd be easily doable like this: T clone(T)(T o) { ClassInfo ci = o.classinfo; if (ci.hasConstructorWithArguments!(ref const(T))) { return ci.construct(o); } throw new Exception("No copy constructor"); } Don't try to make that compile, it won't work. But it could be made to... -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Jul 12 2010