From: Richard Maine on
yaqi <yaqiwang(a)gmail.com> wrote:

> For an integer array, we can do the following:
>
> integer, parameter, dimension(2) :: a =(/1, 2/)
>
> I guess this array will be in the memory before the process starts.

Likely in fact, but in terms of the standard there is no guarantee that
it will ever be in memory. This is a constant - not a variable.
Constants are basically compile-time things and don't necessarily have
to turn into memory at all. Or a single constant might be stored at
multiple places in memory for one reason or other.

> My question is that can we do the similar thing for a user-defined
> type, for example,
>
> type mytype
> integer, pointer, dimension(:) :: ptr => null()
> end type mytype
>
> type(mytype), parameter :: c = mytype(ptr =>(/1,2/))

This has a problem unrelated to derived types. As noted above, constants
do not inherently have memory addresses. The fact that they might happen
to have memory addresses in fact is beside the point that the standard
treats them as though they don't. You can't have a pointer to a
constant. Period. That's completely independent of questions about
constructors. So no, you can't have a constructor with a pointer to a
constant because you can't have such a pointer in the first place.

You should be able to specify a pointer target in a constructor, but it
has to be a valid pointer target, which a constant is not.

One other niggly point now that I check is that you have to use just "="
instead of "=>" in this context. I'm not quite sure why that is, but it
seems to be so.

Do note that the ptr= part is an f2003-ism. The following code is valid
f95, as far as I can see. Both nag and g95 accept it.

program main
type my_type
integer, pointer :: p
end type
integer, target :: i
type(my_type) :: x
x = my_type(i)
end

If I change the (i) to the f2003 (p=i) form, g95 accepts it, but nag
doesn't. As noted, that's an f2003 feature.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: Reinhold Bader on
Hello,

Am 30.03.2010 21:46, schrieb yaqi:
> Hi,
>
> For an integer array, we can do the following:
>
> integer, parameter, dimension(2) :: a =(/1, 2/)
>
> I guess this array will be in the memory before the process starts.
>
> My question is that can we do the similar thing for a user-defined
> type, for example,
>
> type mytype
> integer, pointer, dimension(:) :: ptr => null()
> end type mytype
>
> type(mytype), parameter :: c = mytype(ptr =>(/1,2/))
> (the above line is not compilable, it is used only for illustrating
> the idea.)

In Fortran 2003, it is not possible to default-initialize pointers or
pointer components to anything but null().

This changes with the upcoming Fortran 2008 standard, where you presumably
will be allowed to write

integer, target :: a = (/1, 2/)
type(mytype) :: c = mytype(a)

(I'm not sure whether you'd be allowed to make c a parameter).

Regards
Reinhold
>
> Thanks.

From: yaqi on
On Mar 30, 2:49 pm, Reinhold Bader <Ba...(a)lrz.de> wrote:
> Hello,
>
> Am 30.03.2010 21:46, schrieb yaqi:
>
>
>
>
>
> > Hi,
>
> > For an integer array, we can do the following:
>
> > integer, parameter, dimension(2) :: a =(/1, 2/)
>
> > I guess this array will be in the memory before the process starts.
>
> > My question is that can we do the similar thing for a user-defined
> > type, for example,
>
> > type mytype
> >   integer, pointer, dimension(:) :: ptr => null()
> > end type mytype
>
> > type(mytype), parameter :: c = mytype(ptr =>(/1,2/))
> > (the above line is not compilable, it is used only for illustrating
> > the idea.)
>
> In Fortran 2003, it is not possible to default-initialize pointers or
> pointer components to anything but null().
>
> This changes with the upcoming Fortran 2008 standard, where you presumably
> will be allowed to write
>
> integer, target :: a = (/1, 2/)
> type(mytype) :: c = mytype(a)
>
> (I'm not sure whether you'd be allowed to make c a parameter).
>
> Regards
> Reinhold
>
>
>
>
>
> > Thanks.- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

Thanks guys.

I guess I have to explicitly have a subroutine to initialize my
constant type variable at the beginning of my main. I will live with
it although it is not so elegant.

Best,
yaqi
From: Richard Maine on
Reinhold Bader <Bader(a)lrz.de> wrote:

> > type(mytype), parameter :: c = mytype(ptr =>(/1,2/))
>
> In Fortran 2003, it is not possible to default-initialize pointers or
> pointer components to anything but null().
>
> This changes with the upcoming Fortran 2008 standard, where you presumably
> will be allowed to write
>
> integer, target :: a = (/1, 2/)
> type(mytype) :: c = mytype(a)

You can do that in f95 even. It is not default initialization. See my
previously posted example, which was different only in that I was using
a scalar instead of an array.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: yaqi on
On Mar 30, 3:24 pm, nos...(a)see.signature (Richard Maine) wrote:
> Reinhold Bader <Ba...(a)lrz.de> wrote:
> > > type(mytype), parameter :: c = mytype(ptr =>(/1,2/))
>
> > In Fortran 2003, it is not possible to default-initialize pointers or
> > pointer components to anything but null().
>
> > This changes with the upcoming Fortran 2008 standard, where you presumably
> > will be allowed to write
>
> > integer, target :: a = (/1, 2/)
> > type(mytype) :: c = mytype(a)
>
> You can do that in f95 even. It is not default initialization. See my
> previously posted example, which was different only in that I was using
> a scalar instead of an array.
>
> --
> Richard Maine                    | Good judgment comes from experience;
> email: last name at domain . net | experience comes from bad judgment.
> domain: summertriangle           |  -- Mark Twain

Hi Richard,

I guess it depends on the compiler.

With IVF, the following does not work:

type mytype
integer, pointer, dimension(:) :: ptr => null()
end type mytype
integer, target :: a(2) = (/1, 2/)
type(mytype) :: c = mytype(a)

The error message is "error #6592: This symbol must be a defined
parameter, an enumerator, or an argument of an inquiry function that
evaluates to a compile-time constant. [A]".

I did not try f95 or gfortran though.

--yaqi