[vtk-developers] chaining vtk Set... methods

Francois Bertel francois.bertel at kitware.com
Fri Jan 29 11:19:40 EST 2010


Hello,

I'm again that. See other comments about the "Declarative API":

http://vtk.uservoice.com/forums/31508-general/suggestions/361302-declarative-api?ref=title

On Fri, Jan 29, 2010 at 10:45 AM, Michael Halle <mhalle at bwh.harvard.edu> wrote:
> All this talk of vtkNew and vtkSmartPointer has got me thinking.
>
> One of the elements of VTK syntax that has always bothered me is the
> redundancy of multiple "object->SetXXX();" method calls in typical
> applications.  You end up with a long list of "object->Set...;" lines.  That
> quite redundant. It's also error prone, since cutting and pasting code might
> result in an "object2->..." being added when "object->..." was intended.
>
> The fix, as far as I know, is actually relatively simple (if not highly
> pervasive): change the return value of SetXXX() methods to be "this".
>
> I don't know why I didn't think about it years ago, when the change would
> not have been as major.
>
> // Compare (old way):
>
>  vtkProperty *property = vtkProperty::New();
>  property->SetColor(1.0, 1.0, 1.0);
>  property->SetDiffuse(0.7);
>  property->SetSpecular(0.4);
>  property->SetSpecularPower(20);
>
> // new way:
>
>  vtkNew<vtkProperty> property;  // or old-style New() call
>  property->
>    SetColor(1.0, 1.0, 1.0)->
>    SetDiffuse(0.7)->
>    SetSpecular(0.4)->
>    SetSpecularPower(20);
>
> // or
>  actor->GetProperty()->
>    SetColor(1.0, 1.0, 1.0)->
>    SetDiffuse(0.7)->
>    SetSpecular(0.4)->
>    SetSpecularPower(20);
>
>
> I believe the change to be backwards compatible (since SetXXX methods
> invariably return "void" now), and also compatible with the scripting
> wrappers.  You get natural indentation, reduced verbosity without loss of
> clarity, and the ability to pick and choose between old and new syntax.
>
> Perhaps I missed a discussion about this idea sometime in the history of
> VTK, but are there technical downsides to this scheme?  The only major
> problem I see is that the new calling syntax can't be used with unmodified
> third party libraries that still return void instead of "this".
>
> Insane?
>
> --Mike
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtk-developers
>
>



-- 
François Bertel, PhD  | Kitware Inc. Suite 204
1 (518) 371 3971 x113 | 28 Corporate Drive
                      | Clifton Park NY 12065, USA



More information about the vtk-developers mailing list