# Overloading constructor

**URL:** <https://discourse.julialang.org/t/overloading-constructor/107959>\
**Category:** New to Julia\
**Tags:** question, package\
**Created:** [December 22, 2023, 9:05pm UTC](https://discourse.julialang.org/t/overloading-constructor/107959 "2023-12-22T21:05:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jabru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jabru/32/205670_2.png) [@jabru](https://discourse.julialang.org/u/jabru)\
**Post date:** [December 22, 2023, 9:05pm UTC](https://discourse.julialang.org/t/overloading-constructor/107959/1 "2023-12-22T21:05:42Z")

</div>

Hi there,

so I am currently working on a Julia script that reads unformatted binary Fortran files.  
For this, I am using the package “FortranFiles.jl”. After importing, one can create FortranFile objects with the command

`FortranFile("filename.bin")`

and then read the content of the file with the base method

`read(f, **kwargs)`

However, it is not possible to use this nice

```julia
FortranFile(filname) do f 
     some_code
   end

```

In the documentation of the open function, I found that one of the versions of the open function is defined as

```julia
function open(f::Function, args...; kwargs...)
    io = open(args...; kwargs...)
    try
        f(io)
    finally
        close(io)
    end
end

```

But if I do the same thing with the FortranFile, i.e. I define

```julia
function FortranFile(f::Function, args...; kwargs...)
    io = FortranFile(args...; kwargs...)
    try
        f(io)
    finally
        close(io)
    end
end 

```

Julia seems to forget the original FortranFile constructor. Every time I call the function above in

`FortranFile(filname) do f`

construct I get a method error and when I run

`methods(FortranFile)`

I only get the function I defined.

Can someone tell me, how I properly define the function?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 22, 2023, 10:01pm UTC](https://discourse.julialang.org/t/overloading-constructor/107959/2 "2023-12-22T22:01:57Z")

</div>

It looks like you are trying to extend a method from an existing package, FortranFiles.jl.

If you want to extend a method, you must either:

1. Explicitly `import` it as opposed to just `using` the package.
2. Explicitly provide the module name in your function declaration.

```julia
import FortranFiles: FortranFile

function FortranFile(f::Function, args...; kwargs...)
    # ...
end

```

Or

```julia
using FortranFiles

function FortranFiles.FortranFile(f::Function, args...; kwargs...)
    # ...
end

```

I should note that you may be committing type piracy here.

[https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy)

This may be fine if you are writing an end-user application, but would be an issue if you are planning to publish a package for others to use.

It may be better to fork the package and add this function there. I see the package has not been updated in a while (4 years), but you could try a pull request. If that fails, perhaps you could consider taking over maintainership.

---

<div class="post-metadata">

**Author:** ![jabru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jabru/32/205670_2.png) [@jabru](https://discourse.julialang.org/u/jabru)\
**Post date:** [December 23, 2023, 10:37am UTC](https://discourse.julialang.org/t/overloading-constructor/107959/3 "2023-12-23T10:37:21Z")

</div>

Thank you! That is exactly what I was looking for!
