Scalar arguments in elemental subprograms. Why not?

Scalar arguments in elemental subprograms. Why not?

Sergio's picture

Hi,

Is it possible to create a function with the same interface as the elemental intrinsic bessel_jn(n, x), where n is always a scalar and x may be an array? It looks like it is not possible. Why not?

In general that would be nice, but it introduces one serious limitation: elemental type-bound procedures. I would like to write something like

type :: Foo
    contains
         procedure :: func => Foo_func
end type

type, extends(Foo) :: Bar
    real :: param
   contains
         procedure :: func => Bar_func
end type

elemental function Foo_func(self, x)
   class(Foo) :: self
   real :: x, Foo_func
   Foo_func=x+1.d0
end function

elemental function Bar_func(self, x)
   class(Bar) :: self
   real :: x, Bar_func
   Bar_func=x*self%param
end function

 But this would not work, because I would need to provide an array of self's.

Did I misunderstood something? Why shouldn't this be allowed?

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
IanH's picture
Best Reply

You forgot to add the INTENT(IN) attribute to the arguments.

Apart from that, I don't think there's an issue.  The arguments to an elemental function just need to be conformable.  A scalar is conformable with an array.

(Similarly your description of the intrinsic bessel_jn isn't quite right - n doesn't always have to be scalar - it can be an array with the same shape as the other argument.)

Sergio's picture

Uhm, now I feel extremely stupid. Why didn't I test the code before posting here!!!??? I had some stupid preconceived idea and went for it.

It works like a charm :) Thank you!

Login to leave a comment.