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

strange errror overriding // for UDT or deferred len characters

martymike
Beginner
408 Views

As you know the language does not directly support arrays of strings (characters) of differing lengths. I was trying to create a type to surmount this (I’ve searched for solutions and cannot find one - I’m very surprised no one seems to have done this yet).

 

I’m getting a very strange error. I’ve included a very reduced version of the file below.

 

vtest.f90(14): error #5130: A prefix shall not specify ELEMENTAL, if proc-language-binding-spec appears in the function-stmt or subroutine-stmt.

vtest.f90(14): error #8064: This syntax is invalid; the keyword C is required when using the BIND attribute.   [RS]

vtest.f90(14): error #6708: A specific procedure must be a function for a defined OPERATOR.   [OP_CONCAT_CH_VS]

 

I can certainly believe that I have done something wrong (and that the third error is just a consequence of the first two). But these errors seem erroneous.

 

module tdefVstrs

implicit none
type vstrs
character(len=:),allocatable :: s
end type
interface operator(//)
module procedure op_concat_CH_VS
end interface operator(//)

CONTAINS

elemental subroutine op_concat_CH_VS(ch,vs) result (rs)
character(*),intent(in) :: ch
type(vstrs),intent(in) :: vs
character(:),allocatable :: rs
rs=ch//vs%s
end subroutine op_concat_CH_VS

end module tdefVstrs

 

0 Kudos
4 Replies
IanH
Black Belt
389 Views

You've confused the parser, because you've stuck a RESULT suffix on a SUBROUTINE statement.  Strange errors for strange syntax!

The only permitted suffix on a subroutine statement is BIND... error messages reflect the parser working on the assumption that's what you intended to type.  (I agree that's not probably the best assumption here (better might be "this is a bizarre suffix - I give up parsing this statement"), but neither should error reporting assume the user meant to type FUNCTION when they actually typed SUBROUTINE.)

 

(Many people have created types to support arrays of strings of varying length, a standardised interface for such a type is even described in part two of the ISO 1539 standard.  Search for implementations of the ISO_VARYING_STRING module.)

martymike
Beginner
359 Views

Thanks. Putting a result on a subroutine is a pretty silly mistake to make. I should have noticed that. Thanks for your gentle tone.

As for existing modules, I can find them, but they all use length 1 character arrays to implement them (that I have found so far). I even used one in production code a decade or so ago. I wanted one based on a structure containing a deferred length allocatable character. That I have not been able to find, so I am trying to write one, or at least part of one. What I have works, but it only has a few operations implemented. I was adding concatenate and got the error. Thanks again.

IanH
Black Belt
305 Views

@martymike wrote:

As for existing modules, I can find them, but they all use length 1 character arrays to implement them (that I have found so far). I even used one in production code a decade or so ago. I wanted one based on a structure containing a deferred length allocatable character. That I have not been able to find, so I am trying to write one, or at least part of one. What I have works, but it only has a few operations implemented. I was adding concatenate and got the error. Thanks again.


Other examples exist, but see https://www.megms.com.au/aniso_varying_string.htm for my attempt a few years back.

(If you can, keep at it with your own attempt - the more the merrier with this sort of thing.  I keep on finding bugs with mine...)

FortranFan
Honored Contributor II
350 Views
@martymike wrote:
strange errror overriding // for UDT or deferred len characters
As you know the language does not directly support arrays of strings (characters) of differing lengths. I was trying to create a type to surmount this (I’ve searched for solutions and cannot find one - I’m very surprised no one seems to have done this yet). ..

@martymike ,

As mentioned in the first reply, do note there have been several attempts to "support arrays of strings (characters) of differing lengths".

Also, note your questions are first and foremost about Fortran in general and under the circumstances please also consider posting your queries at the Fortran Discourse site: https://fortran-lang.discourse.group/ for additional feedback.

In addition, you may want to take a look at a "community" effort to put together a Fortran "standard library" that, among other things, also includes a "string type" which can "support arrays of .. differing lengths": https://stdlib.fortran-lang.org/

Reply