Is it possible to disable the intrinsic assignment for a specific derived type?

111 views Asked by At

Is it possible to disable the intrinsic assignment operator for a specific derived type in Fortran? Basically I would like to make the following a compile-time error:

type(MyType) :: a, b
a = b

I've already tried declaring a type-bound assignment operator and making it private, but this just results in the intrinsic assignment operator being called instead. Of course I could declare an assignment operator that stops the program in order to turn this into a run-time error, though I would really prefer if this would already fail at compile-time.

Is there any way?

1

There are 1 answers

0
IanH On BEST ANSWER

Assignment (and the ability to copy of the value of some object, which is a subtly different thing to assignment) cannot be suppressed in its entirety.

(Note that assignment in Fortran is either intrinsic or defined. If you provide type bound defined assignment, then intrinsic assignment is disabled. The question is really asking how to disable both varieties of assignment.)

But... if the type name is not accessible, and any accessible variables of that type are known (from the perspective of the constraints in the standard) to be not-definable (i.e. they are PROTECTED), then assignment cannot be used without a compile error of some sort. Whether this is useful depends on the circumstances.

Types with properties that require suppression of assignment for some reason may be best (again, it depends on circumstances) hidden from end users by keeping the type name private and wrapping objects of the type as a private pointer component in a different wrapper derived type that is public. Users can assign of the wrapper type to their hearts content, but all they are really doing is copying a reference.

(The Fortran language does not have the concept of an assignment operator, considering assignment as an operator is a well worn path to later confusion.)