JiscMail Logo
Email discussion lists for the UK Education and Research communities

Help for COMP-FORTRAN-90 Archives


COMP-FORTRAN-90 Archives

COMP-FORTRAN-90 Archives


COMP-FORTRAN-90@JISCMAIL.AC.UK


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

COMP-FORTRAN-90 Home

COMP-FORTRAN-90 Home

COMP-FORTRAN-90  July 2012

COMP-FORTRAN-90 July 2012

Options

Subscribe or Unsubscribe

Subscribe or Unsubscribe

Log In

Log In

Get Password

Get Password

Subject:

Re: fortran90.org webpage

From:

Ondřej Čertík <[log in to unmask]>

Reply-To:

Fortran 90 List <[log in to unmask]>

Date:

Mon, 9 Jul 2012 01:16:58 -0700

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (130 lines)

Neil,

On Thu, Jun 7, 2012 at 7:42 AM, Neil Carlson <[log in to unmask]> wrote:
> Ondřej,
>
> You've got a long section section on callbacks and the associated
> 'type casting'.  It's useful to see, in order to contrast and compare,
> the different ways it _could_ be done, but you've left out what is
> arguably the 'best' way to do this in modern Fortran.
>
> To adapt your example of an integration method, this would look
> something like this.
>
> module integrals
>
>   use types, only: dp
>   implicit none
>   private
>
>   public :: integrand, simpson
>
>   ! User extends this type
>   type, abstract :: integrand
>   contains
>     procedure(func), deferred :: eval
>   end type
>
>   abstract interface
>     function func(this, x) result(fx)
>       import :: integrand, dp
>       class(integrand) :: this
>       real(dp), intent(in) :: x
>       real(dp) :: fx
>     end function
>   end interface
>
> contains
>
>   real(dp) function simpson(f, a, b) result(s)
>     class(integrand) :: f
>     real(dp), intent(in) :: a, b
>     s = ((b-a)/6) * (f%eval(a) + 4*f%eval((a+b)/2) + f%eval(b))
>   end function
>
> end module
>
> The abstract type prescribes exactly what the integration routine
> needs, namely a method to evaluate the function, but imposes nothing
> else on the user.  The user extends this type, providing a concrete
> implementation of the eval type bound procedure and adding necessary
> context data as components of the extended type.
>
> Here's your usage example adapted to this better method:
>
> module example_usage
>
>   use types, only: dp
>   use integrals, only: integrand, simpson
>   implicit none
>   private
>
>   public :: foo
>
>   type, extends(integrand) :: my_integrand
>     real(dp) :: a, k
>   contains
>     procedure :: eval => f
>   end type
>
> contains
>
>   function f(this, x) result(fx)
>     class(my_integrand) :: this
>     real(dp), intent(in) :: x
>     real(dp) :: fx
>     fx = this%a*sin(this%k*x)
>   end function
>
>   subroutine foo(a, k)
>     real(dp) :: a, k
>     type(my_integrand) :: my_f
>     my_f%a = a
>     my_f%k = k
>     print *, simpson(my_f, 0.0_dp, 1.0_dp)
>     print *, simpson(my_f, 0.0_dp, 2.0_dp)
>   end subroutine
>
> end module

Thanks a lot for this approach! It took me longer but finally
I found time and put your example into the webpages:

http://fortran90.org/src/best-practices.html#vii-object-oriented-approach

(You might need to refresh your browser to see the changes.)
For now I just added it as a section VII in there (and added
your name into Contributors, if it's ok with you). This is really
cool that you can do this OO style in Fortran now, I didn't know about that.
I tried it in my gfortran 4.6 and it works great.

I don't know whether this, or some other approach is the "best".
The advantage of this approach is that this is how one would do it in
C++ or Python. One possible disadvantage is that it might be harder
to read and learn
how to use it --- one of the main advantages of Fortran is that it is easy
to learn and a working scientist or engineer can quickly grasp it and
get things done. Then there is also the question of run-time performance,
I don't know which approach is the fastest.

So for now I'll leave it there as it is, so that at least people know about
the options. Later it'd be nice to refactor this section a little bit,
possibly adding C++ and C examples side by side.

>
> A second general comment about the website.  It would appear to present
> itself as a consensus view of the Fortran community (especially given
> the domain name.)  Until that time it actually does, I'd suggest making
> it clear that you are the author and that the content really reflects
> your views on the topic.  Nevertheless, I think that websites like this
> would be immensely useful to the community, and I applaud you for your
> effort.

Thanks for the encouragement, and you are right about the consensus. So I've
added a section "About": http://fortran90.org/src/about.html,
as the first link in the table of contents at the front page. Please
let me know if you think it is ok and clear now, or whether
I should clarify it more.

Thanks,
Ondrej

Top of Message | Previous Page | Permalink

JISCMail Tools


RSS Feeds and Sharing


Advanced Options


Archives

April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
July 2013
June 2013
May 2013
April 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
August 2010
July 2010
June 2010
March 2010
February 2010
January 2010
December 2009
October 2009
August 2009
July 2009
June 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
January 2007
2006
2005
2004
2003
2002
2001
2000
1999
1998
1997


WWW.JISCMAIL.AC.UK

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager