Traits/interfaces in Fortran?

What if take_reference is here replaced with:

interface my_type_wrapper
   module procedure new_wrapper
end interface

! ... contains ...

function new_wrapper(ref)
  type(my_type), target, intent(in) :: ref
  type(my_type_wrapper) :: new_wrapper
  new_wrapper % obj => ref
end function

Then one could correctly call the same way?

call needs_trait(my_type_wrapper(my_type_obj))

I am still sometimes confused when target is needed.

One more question: what if my_type_obj is a temporary object (function result). How long does it life last? In the pointer scenario, should it cause segfault when referenced by needs_trait or does it last until the end of the statement?

I have been avoiding pointers at all cost in Fortran, but seems I need to make friends with them again. :slight_smile:

Dominik