Richard Lozes
Richard Lozes

Reputation: 83

Testing for null argument in Fortran 2003

I have declared a recursive type:

type treeNode
private
  class(treeNode), pointer  :: left     => null()
  class(treeNode), pointer  :: right    => null()
contains 
 procedure, non_overridable    :: IsNull           ! Returns true if link is null
end type treeNode

I wish now to implement the "IsNull" function.

! ----
pure function IsNull( theNode )
  logical(LGT)  :: IsNull
  class(treeNode), pointer, intent(IN)  :: theNode

  IsNull = .not. associated( theNode )

end function IsNull

Gfortran complains about the pointer attribute: "Error: Passed-object dummy argument of 'isnull' at (1) must not be POINTER"

If I remove the pointer attribute, replacing with the target attribute (or with nothing), I am unable to use the associated construct. Nor will gfortran let me test for equivalence to null().

Question: How can I test that the actual argument was null?

Upvotes: 2

Views: 2318

Answers (1)

IanH
IanH

Reputation: 21451

You can't, not this way anyway. See the characteristics of the passed-object dummy argument described in 4.5.4.5 of F2008. Further, if the object being used to reference the procedure (the object in object%IsNull()) is not associated, then your program is probably going to explode anyway (you violate F2008 12.5.1p2).

Do you really need the IsNull procedure to be type bound? Can you just use a "conventional" procedure?

Upvotes: 3

Related Questions