是否有可能实施“抽象” Fortran 2003中的类型变量? [英] Is it possible to implement an "abstract" variable inside a type in Fortran 2003?

查看:190
本文介绍了是否有可能实施“抽象” Fortran 2003中的类型变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个抽象类型

$ p $ type,abstract :: Vehicle
real,dimension(: ),allocatable :: Wheels
包含
过程(Compute_Weight),deferred :: VehicleWeight
结束类型Vehicle

那就是我希望在数组的抽象类型中拥有一个占位符,以便可以在扩展类型中重写或重新定义它,例如

  type,extend(Vehicle):: Bike 
allocate(Wheels(2))
包含
过程: :VehicleWeight => BikeWeight
结束类型Bike

类型,extends(Vehicle):: Car
allocate(Wheels(4))
包含
过程:: VehicleWeight = > CarWeight
结束类型Car

GCC编译器抱怨(当然我猜),唯一的解决方案我发现这个问题只是为了不声明抽象类型中的可分配函数,并直接在类型中声明具有正确大小的变量。
尽管如此,我希望有一种占位符来强制执行Wheels所描述的基本财产(原型)。解决方案

组件的分配是一个可执行的动作 - 它需要出现在源代码的可执行部分。考虑如下所示:

  type,abstract :: vehicle 
real,dimension(:),allocatable :: wheels
...
结束类型

类型,延伸(车辆)::自行车
...
结束类型自行车

类型,延伸(车辆)::汽车
...
终端类型汽车

界面自行车
过程bike_constructor
结束界面自行车

界面车
程序car_constructor
结束界面车

...

功能bike_constructor()
类型(自行车):: bike_constructor
allocate(bike_constructor%wheels(2))
...
结束函数bike_constructor

函数car_constructor()
类型(汽车):: car_constructor
allocate(car_constructor%wheels(4))
...
end function car_constructor

在Fortran 2008中,可以采用以下直接方式:

  class车辆),allocatable :: obj 
IF(i_feel_like_some_练习)THEN
obj = bike()
ELSE
obj = car()
END IF
PRINT('My object has',I0''wheels! '),SIZE(obj%wheels)

在Fortran 2003中,对多态对象的内在赋值是不支持。需要使用变通办法,例如在ALLOCATE语句中使用SOURCE说明符。



公共和私有组件的适当应用可以进一步指导和约束客户端代码进行交互类型正确。


I would like to write an abstract type

type, abstract :: Vehicle
    real, dimension(:), allocatable:: Wheels 
    contains
     procedure (Compute_Weight), deferred :: VehicleWeight
end type Vehicle

that is I'd like to have a placeholder in the abstract type for an array in such a way that it could be overridden or redefined in the extended type with something like

type, extends(Vehicle) :: Bike
     allocate(Wheels(2))
    contains
     procedure :: VehicleWeight => BikeWeight
end type Bike

    type, extends(Vehicle) :: Car
     allocate(Wheels(4))
    contains
     procedure :: VehicleWeight => CarWeight
end type Car

GCC compiler complains (rightfully I guess), and the onlys solution I could find to this problem is just to not declare the allocatable function in the abstract type and declare the variable directly with the correct size inside the type. Nevertheless I would like to have a sort of placeholder to enforce the implementation of the fundamental property descrived by Wheels (a prototype). I

解决方案

Allocation of an component is an executable action - it needs to appear in an executable part of your source. Consider something like:

type, abstract :: vehicle
  real, dimension(:), allocatable :: wheels
  ...
end type

type, extends(vehicle) :: bike
  ...
end type bike

type, extends(vehicle) :: car
  ...
end type car

interface bike
  procedure bike_constructor
end interface bike

interface car
  procedure car_constructor
end interface car

...

function bike_constructor()
  type(bike) :: bike_constructor
  allocate(bike_constructor%wheels(2))
  ...
end function bike_constructor

function car_constructor()
  type(car) :: car_constructor
  allocate(car_constructor%wheels(4))
  ...
end function car_constructor

In Fortran 2008, this can be used in the following straightforward manner:

class(vehicle), allocatable :: obj
IF (i_feel_like_some_exercise) THEN
  obj = bike()
ELSE
  obj = car()
END IF
PRINT "('My object has ',I0,' wheels!')", SIZE(obj%wheels)

In Fortran 2003, intrinsic assignment to a polymorphic object is not supported. Workarounds such as using a SOURCE specifier in an ALLOCATE statement need to be used.

Appropriate application of public and private to components and procedures can further guide and constrain client code to interact with the types in the right way.

这篇关于是否有可能实施“抽象” Fortran 2003中的类型变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆