Calls to struct methods and immutable
Joseph Rushton Wakeling
joseph.wakeling at webdrake.net
Thu Nov 15 06:24:18 PST 2012
On 11/15/2012 03:06 PM, bearophile wrote:
> void check() const pure nothrow {
> immutable real p = a ^^ 2 + b ^^ 2;
> assert(sqrt(p) < 10);
> }
Is it appropriate to have 'nothrow' given that the assert could fail?
> Strive to write D code as much cast-free as possible, because
> they are dangerous.
The practical situation I'm dealing with is that the a struct gets built inside
a function, based on data read from files. Once the data has been written to
the struct, it should never be changed again.
i.e.
struct Foo
{
// contains a few different arrays of data
void add( /* adds new data points to collection */) { ... }
}
auto makeFoo()
{
Foo foo;
foreach( /* lots of data */ )
foo.add( /* new data point */ );
return foo;
}
So, in practice, it seems like that function should cast it to immutable as it
returns -- and this would be safe, no?
By the way, I should add -- I recognize I'm deluging the list with a bunch of
questions in these last days, and I'm very grateful for the amount of advice you
and others have been giving. I hope it's not becoming too overwhelming or annoying!
More information about the Digitalmars-d-learn
mailing list