Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Intrinsic Function overloading

Status
Not open for further replies.

ya0037

Programmer
Oct 7, 2010
30
DE

Hi,

I am wondering if any one knows how to define new meaning for intrinsic functions:

I mean I have defined new data types, and in this regards I had to define new meaning for operators(*, +, -, /) and assignment(=) and there would be no problem.

Now I want to define intrinsic functions (e.g: Int, Real, Nint, ....) in a way to work with my new data types.

Do you have any idea? can I define them in a way I define operators function?

Regards,
ya0037
 
I think what you're after is generic procedures -- seeing as you can "overwrite" intrinsic procedures, I reckon you could probably make them generic too.

So, if you wanted to overwrite the "max" function, you would put an interface to external functions in a module, such as:

Code:
interface max
    function max1(x1) result(y1)
    <remainder of interface>
    end function max1

    function max2(x2) result(y2)
    <remainder of interface>
    end function max2
end interface max

Alternatively, if the functions "max1" and "max2" are contained in the module itself, then you can just use

Code:
interface max
    module procedure max1, max2
end interface max

Disclaimer: I haven't tried this, and the syntax of the second chunk of code might be incorrect, as I don't use that very often.

Regardless, that's the general idea.

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
Thanks to your answer,

I know this one, but I am looking for a way not to override normal intrinsic.

Like the way using for operators, you do not override on normal acts of operator plus you can use them for new datatypes, so I am looking for a way not to override intrinsic functions and add some new features to them to use them for my new datatypes.

In this regards, if I had normal data the operator will act as it should be, and if I have new datatype the operator will act on them as I asked it to do.

I am really looking forward to hearing from you.

Regards,
ya0037
 
So you want to augment the existing intrinsic with new data types?

My suggestion would be to do as I said above, but have a wrapper for the intrinsic "max" in "max1" -- this way, "max1" has all of the inputs and outputs of the intrinsic max, and it calls the intrinsic "max".

Seems like quite a bit of work, but it would get you the desired result.

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
As NickFort explained, a generic interface for MAX does not override the intrinsic function MAX but either modifies its behavior for particular cases (not advised) or simply adds new possibilities for using it (the most usual situation).

Example :

Code:
module max_mod
  implicit none
  interface max
    module procedure max1
  end interface
  contains
  function max1(i1,r1) result(r)
    integer,intent(in) :: i1
    real,intent(in) :: r1
    real :: r
    r=max(real(i1),r1)
  end function
end module

program test
  use max_mod
  implicit none
  integer :: i1=1
  real :: r1=2,r2=1
  write(*,*) max(i1,r1) ! use of max1
  write(*,*) max(r1,r2) ! use of the intrinsic
  !write(*,*) max(r1,i1) ! error : that case is not foreseen
end program

If you uncomment the third WRITE statement, you will get a compiling error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top