Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Generic structure constructors

ender01
New Contributor I
2,001 Views

Hello all,

I have a derived type, foo with two fields. I've defined a constructor (call it setFoo) and wish to define a generic procedure name foo. When I add the interface:

[cpp]interface foo
   module procedure setFoo
end interface[/cpp]

I get an error 6406: Conflicting attributes or multiple declaration of name. [foo]

Does the Intel compiler not support this?

Thanks,

Rich

0 Kudos
1 Solution
Steven_L_Intel1
Employee
2,001 Views

I keep discovering new things.. No, that is not yet implemented.

View solution in original post

0 Kudos
7 Replies
Steven_L_Intel1
Employee
2,001 Views

You can't have a derived type and a procedure with the same name visible in a scope. Can you show an example of how you'd want to use this? Note that you automatically get a structure constructor for a derived type, so you could say foo(1,2,'c') to create a value of that type. Note that this looks like a procedure call, so you couldn't define your own procedure by that name.

In the object-oriented features of F2003, there are destructors ("finalizers") but not constructors. You can supply default initialization for components, but if you want to do things such as allocate an array, you'll need a procedure. It just can't be called the same name as the type.

0 Kudos
ender01
New Contributor I
2,001 Views

You can't have a derived type and a procedure with the same name visible in a scope. Can you show an example of how you'd want to use this? Note that you automatically get a structure constructor for a derived type, so you could say foo(1,2,'c') to create a value of that type. Note that this looks like a procedure call, so you couldn't define your own procedure by that name.

In the object-oriented features of F2003, there are destructors ("finalizers") but not constructors. You can supply default initialization for components, but if you want to do things such as allocate an array, you'll need a procedure. It just can't be called the same name as the type.

Hi Steve,

Actually, under the Fortran 2003 standard you can with the special case of the default structure constructor:

"A generic name may be the same as the name of a derived type. In this case, the specific procedures must be functions. A reference to such a generic can appear identical in form to a structure constructor for the type (4.4.15), in which case the generic essentially overrides the structure constructor."

J. Adams et al., The Fortran 2003 Handbook, p. 464

Here's an example for MR&C:

[cpp]module mycomplex_module
   type mycomplex
      real :: argument, modulus
   end type
   interface mycomplex
      module procedure complex_to_mycomplex, two_reals_to_mycomplex
   end interface
   :
   contains
      type(mycomplex) function complex_to_mycomplex(c)
         complex, intent(in) :: c
         :
      end function complex_to_mycomplex
      type(mycomplex) function two_reals_to_mycomplex(x,y)
         real, intent(in)           :: x
         real, intent(in), optional :: y
         :
       end function two_reals_to_mycomplex
       :
    end module mycomplex_module
    :
    use mycomplex_module
    type(mycomplex) :: a, b, c
    :
    a = mycomplex(argument=5.6, modulus=1.0)  ! The structure constructor
    c = mycomplex(x=0.0, y=1.0)               ! A function reference[/cpp]

Thanks Steve. I appreciate your efforts to dig up the details on what's been implemented.

Cheers,

Rich

0 Kudos
Steven_L_Intel1
Employee
2,002 Views

I keep discovering new things.. No, that is not yet implemented.

0 Kudos
ender01
New Contributor I
2,001 Views

I keep discovering new things.. No, that is not yet implemented.

Hi Steve,

Thanks again. BTW, is there any publicly available roadmap that delineates if and when the remaining features of Fortran 2003 are going to be implemented? Thanks.

Cheers,

Rich

0 Kudos
Steven_L_Intel1
Employee
2,001 Views

If? Yes. We're committed to doing it all.

When? No. But look for a lot more mid-2009.

0 Kudos
Steven_L_Intel1
Employee
2,001 Views

Further investigation suggests that this may be another case where the "Fortran 2003 Handbook" has an error or is misleading. The F2003 standard says:

C489 (R457) If derived-type-spec is a type name that is the same as a generic name, the component-
spec-list shall not be a valid actual-arg-spec-list for a function reference that is resolvable as a generic reference (12.4.4.1).

P62-63

The text cited by the handbook is this:

A generic name may be the same as a derived-type name, in which case all of the procedures in the interface block shall be functions (P261)

What this tells me is that while you CAN have a generic name that is the same as a structure constructor, the signatures of the specifics cannot match that of what would be a structure constructor.

Does the book say anything more about this? Does it provide an example?

0 Kudos
ender01
New Contributor I
2,001 Views

Further investigation suggests that this may be another case where the "Fortran 2003 Handbook" has an error or is misleading. The F2003 standard says:

C489 (R457) If derived-type-spec is a type name that is the same as a generic name, the component-
spec-list shall not be a valid actual-arg-spec-list for a function reference that is resolvable as a generic reference (12.4.4.1).

P62-63

The text cited by the handbook is this:

A generic name may be the same as a derived-type name, in which case all of the procedures in the interface block shall be functions (P261)

What this tells me is that while you CAN have a generic name that is the same as a structure constructor, the signatures of the specifics cannot match that of what would be a structure constructor.

Does the book say anything more about this? Does it provide an example?

Hmmm, curiouser and curiouser. The full paragraph from which I quoted above is:

"A generic name may be the same as the name of a derived type. In this case, the specific procedures must be functions. A reference to such a generic can appear identical in form to a structure constructor for the type (4.4.15), in which case the generic essentially overrides the structure constructor. A form is interpreted as a structure constructor only if it does not resolve to a generic function reference."

The last sentence sort of paraphrases the constraint C489 (R457) but implies that generic function reference has precedence. Similarly Metcalf, Reid, and Cohen (Fortran 95/2003 Explained), p. 293, state:

"In Fortran 95, structure constructors look like function calls, except that keyword arguments are not allowed. In Fortran 2003, structure constructors can have keyword arguments and optional arguments; moreover, a generic procedure name can be the same as the structure constructor name (which is the same as the type name), with any specific procedures in the generic set taking precedence over the structure constructor if there is any ambiguity. This can be used effectively to produce extra 'constructors' for the type, as shown in Figure 17.1."

(NOTE: Figure 17.1 is the example posted above)

I read the constraint C489 the same way as you. To be honest, I had assumed that the specific functions associated with the generic function reference had to have different signatures. Did the copy of the standard you looked at have Note 4.56?

"The form name(...) is interpreted as a generic function-reference if possible; it is interpreted as
a structure-constructor only if it cannot be interpreted as a generic function-reference."

J3/04-007 (also J3/03-007R2), p. 64

The note, the Handbook, and MR&C all seem to say that the signature does not have to be different. I think that the constraint is confusing (though I can't see how it can be interpreted to say that the generic function reference has precedence) and the authors knew it (hence the note). I guess that I'm missing something in the wording of that constraint...

If it means anything, I think that I'd be perfectly happy making sure that my alternate constructors had different signatures....

Cheers,

Rich

0 Kudos
Reply