Re: Is this legal and if not why not?



David <Contributor@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

> It seems to work but is it standard Fortran and if not why not?

> A base type:
>
> type TBase
> sequence
> integer :: a
> end type TBase
>
> A derived type:
>
> type TDerived
> sequence
> type(TBase) :: base
> integer :: b
> end type TDerived
>
> Casting:
>
> type(TBase) :: basetype
> type(TDerived):: derivedtype
>
> equivalence(basetype, derivedtype) ! is this standard/legal

No. Such "why" questions are mostly unanswerable with anything than
"because that's the way the votes came out". Sometimes one can posit
relatively simple explanations, but this isn't really such a case. There
are *LOTS* of intertwining issues on this one. For example, a full
explanation would no doubt have to get into the inclinations of some
people to avoid extending equivalence at all, the differring
inclinations of other people, and the process of resolving the conflicts
between those camps. You can never really get a good answer for things
like that - anyone that claims to be able to give a full unbiased answer
is probably just demonstrating his/her biases :-)

With that huge caveat that this won't be all of the reasons, and perhaps
not even the most important ones, let me mention at least one technical
issue.

You can *NOT* assume that even sequence types are actually laid out in
memory in the "obvious" way. Hardware alignmenmt issues get in the way.
The alignment requirements can and do vary among hardware platforms, so
things that depend on them are inherently non-portable. Such
non-portable things are exactly the kind of things where the standard
tends to say something is nonstandard. That doesn't necessarily mean all
compilers will reject it - but it does mean that it is likely to get
different results on different platforms, and some compilers might
reject it.

For example, this is *VERY* much parallel to the f77 restrictions
against equivalencing character and non-character data.

Basically, what sequence does for you is just allow you to have "the
same" (or close enough) type declaration in two different scoping units
and be sure that they will be compatible. Think of it as ensuring a
degree of consistency.... but that consistency does not have to imply
that things are laid down in exactly the order that you write the
components, and with no padding. There might be padding. The components
storage order might be rearranged (to avoid padding). Whatever. So in
your case above, the base component might not actualy be the first thing
in memory in TDerived. Ok, odds are that it will in this particular
case, but that doesn't necessarily generalize, and the hstandard doesn't
require it.

> or without need of the EQUIVALENCE
>
> call Base1(derivedtype%base)

That works. But sometimes it isn't convenient.

> What is missing? What OO function cannot be achieved with Fortran 90?

That's a big question. You *CAN*, of course, do anything. Fortran 90 is
Turing complete. Sometimes things get awkward, though. This post isn't a
good place to do a full treatment of the f2003 OO features and what they
do for you. I've tried to write up a bit on that, but it is more than I
can fit in the margin of this posting. :-) Besides, one of our security
folk wants to kick me off this machine for a while to install
patches/upgrades.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
.



Relevant Pages

  • Re: CVF and ADVANCE=NO
    ... > the absence of those characters that makes it a partial record in the ... Sequential I/O in Fortran is fundamentally record-based. ... definition of a sequential file is that it is a sequence of records. ... The Fortran standard describes how programs read and write records. ...
    (comp.lang.fortran)
  • Re: sequence points and the execution model
    ... the whole statement is a sequence point, if fortran had sequence points in ... its execution model, which it doesn't, because we generally shun side ... Fortran only considers the end of a statement as a sequence point. ... If you accept that functions in Fortran may have side effects (dubious by a strict reading of the standard, but true in practice), then I believe that it is the case that the only point in Fortran execution where you can be certain that all side effects within a statement have been discharged is at the end of a statement. ...
    (comp.lang.fortran)
  • Re: sequence points and the execution model
    ... I'm 417/712ths through the Adams text, which closely follows the standard, ... where there is no entry for sequence points. ... C and Fortran use similar semantic models. ...
    (comp.lang.fortran)
  • Re: A few syntax questions
    ... I think putting them in the 'action-stmt' syntax rule ... If I were to redo the bnf from scratch, ... I guess that means that a standard conforming compiler is supposed to overflow. ... I am hoping that F2003 features will regain some respect for Fortran in CS departments,, and not just be coinsidered an old archaic language for old programs, but such strong support of archaic Fortran standards is a major reason why Fortran popularity continues to dwindle. ...
    (comp.lang.fortran)
  • Re: Form k = i + j and test for overflow.
    ... Last night you suggested I was forgetting that overflow violates ... the Fortran standard. ... test that wouldn't work on the most common of implementations. ... that conforms to the Fortran standard. ...
    (comp.lang.fortran)

Loading