Message from discussion
Function returning defined type?
Received: by 10.224.52.200 with SMTP id j8mr2266284qag.5.1350716932447;
Sat, 20 Oct 2012 00:08:52 -0700 (PDT)
Received: by 10.52.91.168 with SMTP id cf8mr646559vdb.6.1350716932423; Sat, 20
Oct 2012 00:08:52 -0700 (PDT)
Path: r17ni43198318qap.0!nntp.google.com!x14no1938821qar.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Sat, 20 Oct 2012 00:08:52 -0700 (PDT)
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=97.104.37.48; posting-account=vqCYcQoAAAC8PtUrH4O0uoPF3ID_IZ51
NNTP-Posting-Host: 97.104.37.48
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <92e0dca8-9495-41a3-beed-9d413715464d@googlegroups.com>
Subject: Function returning defined type?
From: john.chludzin...@gmail.com
Injection-Date: Sat, 20 Oct 2012 07:08:52 +0000
Content-Type: text/plain; charset=ISO-8859-1
Again, after doing due diligence with web searches, I couldn't resolve this.
I have:
module Coordinate
implicit none
integer, parameter :: wp = kind(1.0d0)
private :: WP
type Coords
real(wp) :: x
real(wp) :: y
real(wp) :: z
contains
procedure, nopass :: add
procedure, nopass :: mult
procedure, nopass :: less_than
end type Coords
...
end module Coordinate
module HashTbl
use Coordinate
...
contains
...
type(Coords) function get_sll(list, key)
class(sllist), target, intent(in) :: list
integer, intent(in) :: key
class(sllist), pointer :: current
current => list
do while (current%key /= key)
if ( .not. associated(current%next) ) then
stop
end if
current => current%next
end do
get_sll = current%val
end function get_sll
...
end module HashTbl
type(hash_tbl_sll) :: table
...
call table%put(1, Coords(1, 2, 3))
print*, table%get(1)%x
This results in:
$ gfortran HashTbl.f95
HashTbl.f95:173.22:
print*, table%get(1)%x
1
Error: Syntax error in PRINT statement at (1)
This may be too C/C++'esque but the returned value is type(Coords), shouldn't I be able to reference a member?
I solved it by:
type(Coords) :: tmp
tmp = table%get(1)
print *, tmp%x
---John