std.digest: can we get rid of start() functions?

Marco Leise Marco.Leise at gmx.de
Wed Oct 10 06:02:40 PDT 2012


Am Wed, 10 Oct 2012 12:55:59 +0200
schrieb Piotr Szturmaj <bncrbme at jadamspam.pl>:

> And replace them with global reset function:
> 
> void reset(T)(ref T digest)
>      if (isDigest!T)
> {
>      digest = T.init;
> }
> 
> Current usage:
> 
> SHA1 sha1;
> sha1.start();
> ... calculate hash
> sha1.start(); // start again
> ... calculate hash
> 
> New usage:
> 
> SHA1 sha1;
> ... calculate hash
> sha1.reset(); // start egain
> ... calculate hash

Unlike classes, structs don't mandate a ctor call, so in
general you need to disguise the constructor as 'start()'
'Create()' or similar, where T.init is not sufficient. If on
the other hand, you use a ".isInitialized = false" field
instead of start(), then you'd have to unnecessarily check
that every time data is added to the digest, instead of doing
it once. (That might be personal taste though :) )

I think the constraints are:
* no start() method ever takes arguments
* no digest needs to allocate to start
* no digest depends on global/static variables

-- 
Marco



More information about the Digitalmars-d mailing list