protocolbuffers / protocolbuffers.github.io

Other
38 stars 107 forks source link

Fortran | Protocol buffers | Object oriented programming #106

Open komahanb opened 9 months ago

komahanb commented 9 months ago

https://protobuf.dev/overview/

Protocol buffers are not well supported in non-object-oriented languages popular in scientific computing, such as Fortran and IDL.

I disagree with the remark about Fortran being not object-oriented. Fortran (since 2003) supports derived-data-types (a.k.a data-abstraction) and type-bound-procedures (a.k.a methods that are invoked on the held data). This, of course, extends to other capabilities such as interfaces, inheritance, polymorphism, etc. Could the documentation be specific about what aspect of Fortran is incompatible with protocol-buffer, or atleast clarify the Fortran versions that are not supported, so that developers are not misinformed?

A simple example from chat-gpt:

module ShapesModule
  type :: Shape
     real :: area
     contains
        procedure :: calculateArea
  end type Shape

contains

  subroutine calculateArea(this)
    class(Shape), intent(inout) :: this
    ! This is a generic method to calculate the area for different shapes
    ! Implement specific area calculations for each shape by overriding this method
    this%area = 0.0
  end subroutine calculateArea

end module ShapesModule

program ObjectOrientedFortran
  use ShapesModule

  type :: CircleType
     type(Shape) :: baseShape
     real :: radius
  end type CircleType

  contains

  subroutine calculateArea(this)
    class(CircleType), intent(inout) :: this
    this%baseShape%area = this%radius**2 * 3.14159
  end subroutine calculateArea

  type(CircleType) :: myCircle
  myCircle%radius = 5.0

  ! Calculate area using the overridden method for circles
  call myCircle%baseShape%calculateArea()

  ! Print the calculated area
  print *, "Area of the circle: ", myCircle%baseShape%area

end program ObjectOrientedFortran
Logofile commented 9 months ago

I will look into whether this information is outdated, or still applies. (It's possible that protobufs don't work well with Fortran even with its addition of some object oriented behaviors.)