Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7754411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:18:03+00:00 2026-06-01T12:18:03+00:00

I take an example from Fortran 95/2003 explained by Metcalf et al, since m

  • 0

I take an example from “Fortran 95/2003 explained” by Metcalf et al, since m own code aims for the same thing.

type, abstract :: my_numeric_type
contains
    private
    procedure(op2), deferred :: add
    generic, public :: operator(+) => add
end type

abstract interface
    function op2(a,b) result (r)
        import :: my_numeric_type
        class(my_numeric type), intent(in) :: a,b
        class(my_numeric type), allocatable :: r
    end function op2
end interface

type, extends(my_numeric_type) :: my_integer
    integer, private :: value
contains
    procedure :: add => add_my_integer
end type

Now, my question is how do I properly implement the add_my_integer function. It seems like I am forced to cast the first argument as my_integer since it is a type bound procedure, but the second one has to be my_numeric_type to be compliant with the abstract interface. As for the result, should I allocate r to my_integer? here is what I came up with so far, it does compile, but it seems weird to check for type all the time, and it causes a segmentation fault (perhaps due to some other problem with my code though).

function add_my_integer(a,b) result(r)
    class(my_integer), intent(in) :: a
    class(my_numeric_type), intent(in) :: b
    class(my_numeric_type), allocatable :: r

    allocate(my_integer :: r)
    select type (b)
        type is (my_integer)
            r = a+b
    end select
end function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:18:05+00:00Added an answer on June 1, 2026 at 12:18 pm

    This works for me, but it looks quite complicated (too many select type). I made the value public for easy output only, otherwise you want a custom getter and setter.

    module num
    
      type, abstract :: my_numeric_type
      contains
          private
          procedure(op2), deferred :: add
          generic, public :: operator(+) => add
          procedure(op), deferred :: ass
          generic, public :: assignment(=) => ass
      end type
    
      abstract interface
          subroutine op(a,b)
              import :: my_numeric_type
              class(my_numeric_type), intent(out) :: a
              class(my_numeric_type), intent(in) :: b
          end subroutine op
          function op2(a,b) result (r)
              import :: my_numeric_type
              class(my_numeric_type), intent(in) :: a,b
              class(my_numeric_type), allocatable :: r
          end function op2
    
      end interface
    
      type, extends(my_numeric_type) :: my_integer
          integer, public :: value
      contains
          procedure :: add => add_my_integer
          procedure :: ass => ass_my_integer
      end type
    
      contains
    
        function add_my_integer(a,b) result(r)
            class(my_integer), intent(in) :: a
            class(my_numeric_type), intent(in) :: b
            class(my_numeric_type), allocatable :: r
    
            select type (b)
                type is (my_integer)
                    allocate(my_integer :: r)
                    select type (r)
                      type is (my_integer)
                        r%value = a%value+b%value
                    end select
            end select
        end function
    
    
        subroutine ass_my_integer(a,b)
            class(my_integer), intent(out) :: a
            class(my_numeric_type), intent(in) :: b
    
            select type (b)
                type is (my_integer)
                        a%value = b%value
            end select
        end subroutine
    
    end module
    
    program main
      use num
    
      class(my_integer), allocatable :: a, b, c
      allocate(my_integer :: a)
      allocate(my_integer :: b)
      allocate(my_integer :: c)
      a=my_integer(1)
      b=my_integer(2)
      c = a+b
      write (*,*) c%value
    end program
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take for example following code from a class: public class Employee : IEntity {
I want real example about dynamic site map take it's value from database
Some commands like svn log, for example will only take one input from the
Take this example code (ignore it being horribly inefficient for the moment) let listToString
take this example from google docs class BrowseHandler(webapp.RequestHandler): > def get(self, category, product_id): >
How do I get access to live DOM collections from jQuery? Take for example
I am just starting with MVC, moving from web forms. Lets take for example:
Lets take the example from Yelp: http://www.yelp.com/boston You can see that it's a website
Take the sample history example from Extjs framework. It works fine in Safari. Now
I'm trying to take this example from Google Maps and use an address instead

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.