# Derived type question

**URL:** <https://fortran-lang.discourse.group/t/derived-type-question/2122>\
**Category:** Help\
**Created:** [October 20, 2021, 4:43am UTC](https://fortran-lang.discourse.group/t/derived-type-question/2122 "2021-10-20T04:43:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fortran4r](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@fortran4r](https://fortran-lang.discourse.group/u/fortran4r)\
**Post date:** [October 20, 2021, 4:43am UTC](https://fortran-lang.discourse.group/t/derived-type-question/2122/1 "2021-10-20T04:43:31Z")

</div>

I am currently learning derived types. I am not sure why this subroutine does not work:

```auto
subroutine type_test(n, x, res)
  integer(8) :: n
  real(8) :: x(n), res
  
  type my_type
    real(8) :: a(n)
    real(8) :: b(n)
  end type
  
  type(my_type) :: y
  
  y%a = 1d0
  y%b = 2d0 
  
  res = sum(x + y%a - y%b) 
end

```

I have to use allocatable arrays for a and b to make it work.

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [October 20, 2021, 10:20am UTC](https://fortran-lang.discourse.group/t/derived-type-question/2122/3 "2021-10-20T10:20:56Z")

</div>

About _Derived-type parameters_:

J3/18-007r1 [https://j3-fortran.org/doc/year/18/18-007r1.pdf](https://j3-fortran.org/doc/year/18/18-007r1.pdf)

Example from “ **Note 1** ”

```auto
TYPE humongous_matrix(k, d)
   INTEGER, KIND :: k = KIND (0.0)
   INTEGER (SELECTED_INT_KIND (12)), LEN :: d
   !-- Specify a potentially nondefault kind for d.
   REAL (k) :: element (d, d)
END TYPE

```

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [October 20, 2021, 10:43am UTC](https://fortran-lang.discourse.group/t/derived-type-question/2122/4 "2021-10-20T10:43:28Z")

</div>

Apart from your wish to learn about derived types, the example code that you showed really does not need any derived types. The subroutine that you displayed could be replaced by

```auto
subroutine type_test(n, x, res)
  integer(8) :: n
  real(8) :: x(n), res
!  
  res = sum(x) - n
end

```

Lacking a statement from you as to what you wish to accomplish with the derived type, we do not know what “does not work” means, and it is difficult to advise you how to “make it work”.

---

<div class="post-metadata">

**Author:** ![fortran4r](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@fortran4r](https://fortran-lang.discourse.group/u/fortran4r)\
**Post date:** [October 20, 2021, 11:59am UTC](https://fortran-lang.discourse.group/t/derived-type-question/2122/5 "2021-10-20T11:59:27Z")

</div>

I am simply using the made-up subroutine for practice purposes.

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [October 20, 2021, 12:15pm UTC](https://fortran-lang.discourse.group/t/derived-type-question/2122/6 "2021-10-20T12:15:55Z")

</div>

It doesn’t work because in array components of a derived type definition you are allowed to use an explicit-shape specification (e.g. a(n:m,i:j)) or a deferred-shape specification (e.g. a(:)). In the explicit-shape case, the Standard says about n,m,i,j

_C750 (R740) Each bound in the explicit-shape-spec shall be a specification expression in which there are no references to specification functions or the intrinsic functions ALLOCATED, ASSOCIATED, EXTENDS\_TYPE\_OF, PRESENT, or SAME\_TYPE\_AS, every specification inquiry reference is a constant expression, and **the value does not depend on the value of a variable**._

Your **n** is clearly a variable, so it cannot be used. The Parameterised Derived Type solution avoids this clause by using an n that is a _type parameter_, not a variable.

Deferred-shape would work but requires the component array to be ALLOCATABLE or POINTER.
