From: Eli Osherovich on
Is there any way to check that a particular function has certain
(given) interface.

For example, I have a module that contains an abstract interface:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
module iface
abstract interface
function obj_f(x, flag) result(res)
real :: res
real, intent(in), dimension(2) :: x
real, intent(in), optional :: flag
end function obj_f
end interface

end module iface
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Now, I want to implement a function which has the interface given
above. Of course, there is no problem to implement it. However, I
would like a complier to check that the interface is indeed what I
expect.

Is there such a possibility ?

Thank you.
From: James Van Buskirk on
"Eli Osherovich" <eli.osherovich(a)gmail.com> wrote in message
news:9a188c46-855f-4284-ad23-8255d0caf90b(a)m37g2000yqf.googlegroups.com...

> Is there any way to check that a particular function has certain
> (given) interface.

> For example, I have a module that contains an abstract interface:

> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> module iface
> abstract interface
> function obj_f(x, flag) result(res)
> real :: res
> real, intent(in), dimension(2) :: x
> real, intent(in), optional :: flag
> end function obj_f
> end interface

> end module iface
> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

> Now, I want to implement a function which has the interface given
> above. Of course, there is no problem to implement it. However, I
> would like a complier to check that the interface is indeed what I
> expect.

C:\gfortran\clf\ifacechk>type ifacechk.f90
module iface
abstract interface
function obj_f(x, flag) result(res)
real :: res
real, intent(in), dimension(2) :: x
real, intent(in), optional :: flag
end function obj_f
end interface

end module iface

function test(x)
use iface
implicit none
real, intent(in) :: x(2)
real test
procedure(obj_f), pointer :: testp

if(.FALSE.) testp => test
end function test
C:\gfortran\clf\ifacechk>gfortran -c -Wall ifacechk.f90
ifacechk.f90:19.24:

if(.FALSE.) testp => test
1
Error: Interface mismatch in procedure pointer assignment at (1): 'test' has
the
wrong number of arguments

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


From: Colin Watters on

"James Van Buskirk" <not_valid(a)comcast.net> wrote in message
news:hm8tak$3u2$1(a)news.eternal-september.org...
> "Eli Osherovich" <eli.osherovich(a)gmail.com> wrote in message
> news:9a188c46-855f-4284-ad23-8255d0caf90b(a)m37g2000yqf.googlegroups.com...
>
>> Is there any way to check that a particular function has certain
>> (given) interface.
>
>> For example, I have a module that contains an abstract interface:
>
>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>> module iface
>> abstract interface
>> function obj_f(x, flag) result(res)
>> real :: res
>> real, intent(in), dimension(2) :: x
>> real, intent(in), optional :: flag
>> end function obj_f
>> end interface
>
>> end module iface
>> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>
>> Now, I want to implement a function which has the interface given
>> above. Of course, there is no problem to implement it. However, I
>> would like a complier to check that the interface is indeed what I
>> expect.
>
> C:\gfortran\clf\ifacechk>type ifacechk.f90
> module iface
> abstract interface
> function obj_f(x, flag) result(res)
> real :: res
> real, intent(in), dimension(2) :: x
> real, intent(in), optional :: flag
> end function obj_f
> end interface
>
> end module iface
>
> function test(x)
> use iface
> implicit none
> real, intent(in) :: x(2)
> real test
> procedure(obj_f), pointer :: testp
>
> if(.FALSE.) testp => test
> end function test
> C:\gfortran\clf\ifacechk>gfortran -c -Wall ifacechk.f90
> ifacechk.f90:19.24:
>
> if(.FALSE.) testp => test
> 1
> Error: Interface mismatch in procedure pointer assignment at (1): 'test'
> has the
> wrong number of arguments
>
> --
> write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
> 6.0134700243160014d-154/),(/'x'/)); end
>

Yes, but this tests that the "call" (or whatever term you use for calling a
function) conforms to the interface.

Eli (and I) would like a way to test that the subroutine/function/procedure
itself conforms.

--
Qolin

Email: my qname at domain dot com
Domain: qomputing
>


From: Reinhold Bader on
Hello,

Am 26.02.2010 21:00, schrieb Colin Watters:

>> C:\gfortran\clf\ifacechk>gfortran -c -Wall ifacechk.f90
>> ifacechk.f90:19.24:
>>
>> if(.FALSE.) testp => test
>> 1
>> Error: Interface mismatch in procedure pointer assignment at (1): 'test'
>> has the
>> wrong number of arguments
>>
>> --
>> write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
>> 6.0134700243160014d-154/),(/'x'/)); end
>>
>
> Yes, but this tests that the "call" (or whatever term you use for calling a
> function) conforms to the interface.
>
> Eli (and I) would like a way to test that the subroutine/function/procedure
> itself conforms.


I'd contend that James' example does just the latter. Note that his test program
does not perform a function invocation at all, but only procedure pointer
assignment; since the interface for the procedure pointer "testp" is the
previously defined abstract one, the compiler can (probably even must) check
whether "test" itself conforms.


Regards
Reinhold


From: Jim Xia on
On Feb 26, 8:24 am, Eli Osherovich <eli.osherov...(a)gmail.com> wrote:
> Is there any way to check that a particular function has certain
> (given) interface.
>
> For example, I have a module that contains an abstract interface:
>
> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> module iface
>   abstract interface
>      function obj_f(x, flag) result(res)
>        real :: res
>        real, intent(in), dimension(2) :: x
>        real, intent(in), optional :: flag
>      end function obj_f
>   end interface
>
> end module iface
> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>
> Now, I want to implement a function which has the interface given
> above. Of course, there is no problem to implement it. However, I
> would like a complier to check that the interface is indeed what I
> expect.


Some compilers have a switch to check for the interface mismatch by
the linker. For example on AIX XL compilers can insert information on
procedure interface so that the linker can detect any mismatches.

In general, (real) compilers can only check the mismatch if the
declaration of the interface is pulled into the same scope as the real
procedure definition. That's a difficult task to live upto the user
expectation. Only linkers have that view. Some source-to-source
compilers may have further advantage to see the declarations of the
interface across multiple scopes. So they may be able to detect mis-
matches.

The above discussion is assuming this is what you meant

in main

procedure(obj_f) foo


In definition of foo

function foo (...)
which can have different interfaces from obj_f.

And if a compiler can do something to detect this.


Cheers,

Jim