D2.0: an example of use-case for casting invariant away
Brad Roberts
braddr at puremagic.com
Mon Jun 18 00:30:35 PDT 2007
eao197 wrote:
> Hello!
>
> It is a question only for clarification for myself. If I understand
> http://www.digitalmars.com/d/final-const-invariant.html correctly the
> casting invariant away allowed for situations like this:
>
> // An interface for some third-party library
> // (which is probably written in some exotic language).
> extern(C) void initSomeSubsystem( char * initializationString );
> ...
> // Our D2.0 stuff.
> int main()
> {
> invariant char* initializationString = "...";
> // Casting invariant away is necessary here.
> initSomeSubsystem( cast(invariant) initializationString );
> }
>
> Am I correct?
>
> --Regards,
> Yauheni Akhotnikau
First, is the library actually treating the initString as read only? If
it's not, then you shouldn't pass an invariant (or const) string down to
it. The results would be undefined.
If it's actually treating the string as read-only, then I'd be tempted
to change the extern(c) declaration to pass the argument as const char *
rather than casting away the invariantness. Note, I haven't tried this,
just an off the top of my head suggestion.
Later,
Brad
More information about the Digitalmars-d
mailing list