Zeus
Zeus

Reputation: 1525

Fortran Type Is (Character)

I am getting the following error when trying to use character in Type Is

gfortran -o build/lib/larsa.o -c -ffree-form -g -J./build/lib lib/larsa.f
lib/larsa.f:1558.14:

 Type Is (Character)
          1
Error: The type-spec at (1) shall specify that each length type parameter is assumed
lib/larsa.f:1490.14:

 Type Is (Character)
          1
Error: The type-spec at (1) shall specify that each length type parameter is assumed

This is the subroutine I have coded

Subroutine splits                    &
  (                                  &
    s, delim,                        &
    t1, t2, t3, t4, t5, t6, t7, t8,  & 
    pos                              &
  )

Character (len=*), Intent (in) :: s, delim
Character (len=*), Intent (in), Optional :: pos

Class (*), Intent (out) :: t1
Class (*), Intent (out), Optional :: t2, t3, t4,  &
          t5, t6, t7, t8

Integer :: nf

Select Type (t1)

 Type Is (Character(len=*))

   If (Present (pos)) Then

     If (Present (t5)) Then

       If (Present (t8)) Then    
         Call splits_str_to_str (s, delim, t1, t2, t3, t4,  &
              t5, t6, t7, t8, pos)
       Else If (Present (t7)) Then
         Call splits_str_to_str (s, delim, t1, t2, t3, t4,  &
              t5, t6, t7, pos)
       Else If (Present (t6)) Then
         Call splits_str_to_str (s, delim, t1, t2, t3, t4,  &
              t5, t6, pos)

Furthermore, I am using unlimited polymorphic entities on quite a few arguments. Things then get complicated to have Select Type for all of them. The compiler is giving me the error

gfortran -o build/lib/larsa.o -c -ffree-form -g -J./build/lib lib/larsa.f
lib/larsa.f:1569.51:

         Call splits_str_to_str (s, delim, t1, t2, t3, t4,  &
                                               1
Error: Type mismatch in argument 's2' at (1); passed CLASS(*) to CHARACTER(1)
lib/larsa.f:1572.51:

         Call splits_str_to_str (s, delim, t1, t2, t3, t4,  &
                                               1
Error: Type mismatch in argument 's2' at (1); passed CLASS(*) to CHARACTER(1)
lib/larsa.f:1575.51:

Upvotes: 0

Views: 661

Answers (2)

In addition to the syntax problem explained by IanH - you must use character(*) in select type - I will add something to your second problem. Possibly that should have been a new question.

You cannot assume that you know that two objects have the same dynamic type. You have to list all of them as selectors in select type otherwise the non-listed ones remain polymorphic. This is the very same reason why you use select type for t1.

I will also ads that it seems that your routine splits_str_to_str accepts optional arguments. In that case you don't have to test presents of all of them, you can pass them as optional actual arguments even if not present.

Unfortunately, to use them in select type as selectors you still need to test their presence.

One big design question remains. Why doesn't have splits character dummy arguments? Why it needs unlimited polymorphics?

Upvotes: 2

IanH
IanH

Reputation: 21431

This is just a consequence of the syntax rules and constraints for SELECT TYPE.

Conceptually the type selection is limited to the dynamic type of the object and any kind parameters for that type. The block to be executed is not chosen based on length type parameters, hence the syntax requires the specification of the length type parameter to be assumed.

So you simply want:

TYPE IS (CHARACTER(*))

Without the assumed specification, the implication of the syntax would be that the length type parameter of the selector had to match the default value of that parameter for the character type, i.e. 1.

Upvotes: 2

Related Questions