Making module variables privateHow do I unload (reload) a Python module?How can I get a list of locally installed Python modules?Procedure copy in each instance of data typeFortran tips in large modulesHow to set a mpreal parameter inside a Fortran moduleHow to call public variables of a used module through a module with private statementStructure of a fortran program with modules and subroutinesFortran Modules and Global VariablesModule or main program array must have constant shape error in FortranFortran link modules for precision and global variable types

Found and corrected a mistake on someone's else paper -- praxis?

"was fiction" vs "were fictions"

Is that a case of "DOUBLE-NEGATIVES" as claimed by Grammarly?

Can Jimmy hang on his rope?

Why is a mixture of two normally distributed variables only bimodal if their means differ by at least two times the common standard deviation?

Is there a strong legal guarantee that the U.S. can give to another country that it won't attack them?

When I press the space bar it deletes the letters in front of it

Is it OK to leave real names & info visible in business card portfolio?

Addressing unnecessary daily meetings with manager?

What is a "Lear Processor" and how did it work?

Using Open with a filename that contains :

What is the correct parsing of お高くとまる?

Why would people still be chanting "Lock her up" at Trump rallies in 2019?

Why archangel Michael didn't save Jesus when he was crucified?

How effective would wooden scale armor be in a medieval setting?

What do three diagonal dots above a letter mean in the "Misal rico de Cisneros" (Spain, 1518)?

What is the right approach to quit a job during probation period for a competing offer?

Chorophyll and photosynthesis in plants with coloured leaves

Why do we need common sense in AI?

How do native German speakers usually express skepticism (using even) about a premise?

How to drill holes in 3/8" steel plates?

Data Encryption by Application vs Data Encryption in Database

How to tell someone I'd like to become friends without letting them think I'm romantically interested in them?

What are some further readings in Econometrics you recommend?



Making module variables private


How do I unload (reload) a Python module?How can I get a list of locally installed Python modules?Procedure copy in each instance of data typeFortran tips in large modulesHow to set a mpreal parameter inside a Fortran moduleHow to call public variables of a used module through a module with private statementStructure of a fortran program with modules and subroutinesFortran Modules and Global VariablesModule or main program array must have constant shape error in FortranFortran link modules for precision and global variable types






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















During the last few years, I have been creating several modules with subroutines that I then use for different projects. I am having problems when I define parameter variables in one of those project-specific files that conflict with a variable name defined within those modules. Is it possible to make those names subroutine-private or module-private?



Here is an example. Suppose I have the following module:



module mymod

implicit none
contains

subroutine test1(x)
real, intent(in) :: x(:)
print *, x**2.0

end subroutine test1

end module mymod


This module is then called by the main program



program main

use mymod
implicit none

real :: y
real,dimension(2,1),parameter :: x = [1.0,2.0]

y = 3.0
call test1(y)

end program main


In this case, given that x in the main program is defined as a parameter with different dimensions to the x in subroutine test1, there will be problems when compiling (shape matching rules violated). Is there any way of making x in module mymod private within the module?



I know an option could be to use "non-common" variable names in my modules or have a list of forbidden names, but that seems complicated at this point (requires editing too many files and lose consistency of notation with books/papers where these procedures are outlined), and would make collaboration with colleagues more difficult.










share|improve this question






















  • Add PRIVATE to the module after the IMPLICIT NONE. After the PRIVATE statement, add PUBLIC TEST1. This will expose only the test1 entity in the module.

    – evets
    Mar 26 at 4:28

















1















During the last few years, I have been creating several modules with subroutines that I then use for different projects. I am having problems when I define parameter variables in one of those project-specific files that conflict with a variable name defined within those modules. Is it possible to make those names subroutine-private or module-private?



Here is an example. Suppose I have the following module:



module mymod

implicit none
contains

subroutine test1(x)
real, intent(in) :: x(:)
print *, x**2.0

end subroutine test1

end module mymod


This module is then called by the main program



program main

use mymod
implicit none

real :: y
real,dimension(2,1),parameter :: x = [1.0,2.0]

y = 3.0
call test1(y)

end program main


In this case, given that x in the main program is defined as a parameter with different dimensions to the x in subroutine test1, there will be problems when compiling (shape matching rules violated). Is there any way of making x in module mymod private within the module?



I know an option could be to use "non-common" variable names in my modules or have a list of forbidden names, but that seems complicated at this point (requires editing too many files and lose consistency of notation with books/papers where these procedures are outlined), and would make collaboration with colleagues more difficult.










share|improve this question






















  • Add PRIVATE to the module after the IMPLICIT NONE. After the PRIVATE statement, add PUBLIC TEST1. This will expose only the test1 entity in the module.

    – evets
    Mar 26 at 4:28













1












1








1








During the last few years, I have been creating several modules with subroutines that I then use for different projects. I am having problems when I define parameter variables in one of those project-specific files that conflict with a variable name defined within those modules. Is it possible to make those names subroutine-private or module-private?



Here is an example. Suppose I have the following module:



module mymod

implicit none
contains

subroutine test1(x)
real, intent(in) :: x(:)
print *, x**2.0

end subroutine test1

end module mymod


This module is then called by the main program



program main

use mymod
implicit none

real :: y
real,dimension(2,1),parameter :: x = [1.0,2.0]

y = 3.0
call test1(y)

end program main


In this case, given that x in the main program is defined as a parameter with different dimensions to the x in subroutine test1, there will be problems when compiling (shape matching rules violated). Is there any way of making x in module mymod private within the module?



I know an option could be to use "non-common" variable names in my modules or have a list of forbidden names, but that seems complicated at this point (requires editing too many files and lose consistency of notation with books/papers where these procedures are outlined), and would make collaboration with colleagues more difficult.










share|improve this question














During the last few years, I have been creating several modules with subroutines that I then use for different projects. I am having problems when I define parameter variables in one of those project-specific files that conflict with a variable name defined within those modules. Is it possible to make those names subroutine-private or module-private?



Here is an example. Suppose I have the following module:



module mymod

implicit none
contains

subroutine test1(x)
real, intent(in) :: x(:)
print *, x**2.0

end subroutine test1

end module mymod


This module is then called by the main program



program main

use mymod
implicit none

real :: y
real,dimension(2,1),parameter :: x = [1.0,2.0]

y = 3.0
call test1(y)

end program main


In this case, given that x in the main program is defined as a parameter with different dimensions to the x in subroutine test1, there will be problems when compiling (shape matching rules violated). Is there any way of making x in module mymod private within the module?



I know an option could be to use "non-common" variable names in my modules or have a list of forbidden names, but that seems complicated at this point (requires editing too many files and lose consistency of notation with books/papers where these procedures are outlined), and would make collaboration with colleagues more difficult.







module fortran






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 0:38









LooperLooper

969 bronze badges




969 bronze badges












  • Add PRIVATE to the module after the IMPLICIT NONE. After the PRIVATE statement, add PUBLIC TEST1. This will expose only the test1 entity in the module.

    – evets
    Mar 26 at 4:28

















  • Add PRIVATE to the module after the IMPLICIT NONE. After the PRIVATE statement, add PUBLIC TEST1. This will expose only the test1 entity in the module.

    – evets
    Mar 26 at 4:28
















Add PRIVATE to the module after the IMPLICIT NONE. After the PRIVATE statement, add PUBLIC TEST1. This will expose only the test1 entity in the module.

– evets
Mar 26 at 4:28





Add PRIVATE to the module after the IMPLICIT NONE. After the PRIVATE statement, add PUBLIC TEST1. This will expose only the test1 entity in the module.

– evets
Mar 26 at 4:28












1 Answer
1






active

oldest

votes


















-1














Two different questions in one:



Why is the example program failing to compile:



This has nothing to do with public or private, or x being defined in the program itself.



It has everything to do with the fact that in the module, x as a parameter is defined as a 1-d array, and in the main program, y is a scalar.



Try it, remove the declaration of x in the main program and it will still fail.



(In fact, the declaration doesn't work like that anyway, you declare x as a 2-d array (shape 2, 1), but then give it a 1-d array. You'd have to do something like:



real, dimension(2, 1), parameter x = reshape([1.0, 2.0], [2, 1])


But to get rid of the error you describe, you either need to change the subroutine interface by removing the (:) behind the real, intent(in) :: x, or change the call to call test1([y]).



What can you do when 2 modules import different variables of the same name:



It would be different if you were to say have this:



module modA
implicit none
real, parameter :: x = 2.0
contains
subroutine subA(k)
real, intent(in) :: k
print *, k*x
end subroutine subA
end module modA

module modB
implicit none
real :: x(3)
end module modB

program progtest
use modA
use modB
implicit none
call subA(x(1))
end program progtest


In this example, it would try to import the variable x from both modules.



Ways to avoid it:




  1. Make one x private:



    implicit none
    real, parameter, private :: x = 2.0


    or



    real, parameter :: x = 2.0
    private :: x


    or



    implicit none
    private
    real, parameter :: x = 2.0
    public :: subA



  2. Only import the parts that you need:



    program progtest
    use modA, only: subA
    use modB
    implicit none
    ...



  3. Rename one or both of the x:



    use modA
    use modB, only: xB => x
    ...
    call subA(xB(1))






share|improve this answer

























  • The key is to make everything in the module PRIVATE and only expose those entities in the module that need to be PUBLIC.

    – evets
    Mar 26 at 4:26











  • In the example OP posted, the issue was absolutely not the private/public issue. His problem, according to the example, was the parameter type mismatch.

    – chw21
    Mar 26 at 5:47











  • I've updated my answer.

    – chw21
    Mar 26 at 6:02










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348296%2fmaking-module-variables-private%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














Two different questions in one:



Why is the example program failing to compile:



This has nothing to do with public or private, or x being defined in the program itself.



It has everything to do with the fact that in the module, x as a parameter is defined as a 1-d array, and in the main program, y is a scalar.



Try it, remove the declaration of x in the main program and it will still fail.



(In fact, the declaration doesn't work like that anyway, you declare x as a 2-d array (shape 2, 1), but then give it a 1-d array. You'd have to do something like:



real, dimension(2, 1), parameter x = reshape([1.0, 2.0], [2, 1])


But to get rid of the error you describe, you either need to change the subroutine interface by removing the (:) behind the real, intent(in) :: x, or change the call to call test1([y]).



What can you do when 2 modules import different variables of the same name:



It would be different if you were to say have this:



module modA
implicit none
real, parameter :: x = 2.0
contains
subroutine subA(k)
real, intent(in) :: k
print *, k*x
end subroutine subA
end module modA

module modB
implicit none
real :: x(3)
end module modB

program progtest
use modA
use modB
implicit none
call subA(x(1))
end program progtest


In this example, it would try to import the variable x from both modules.



Ways to avoid it:




  1. Make one x private:



    implicit none
    real, parameter, private :: x = 2.0


    or



    real, parameter :: x = 2.0
    private :: x


    or



    implicit none
    private
    real, parameter :: x = 2.0
    public :: subA



  2. Only import the parts that you need:



    program progtest
    use modA, only: subA
    use modB
    implicit none
    ...



  3. Rename one or both of the x:



    use modA
    use modB, only: xB => x
    ...
    call subA(xB(1))






share|improve this answer

























  • The key is to make everything in the module PRIVATE and only expose those entities in the module that need to be PUBLIC.

    – evets
    Mar 26 at 4:26











  • In the example OP posted, the issue was absolutely not the private/public issue. His problem, according to the example, was the parameter type mismatch.

    – chw21
    Mar 26 at 5:47











  • I've updated my answer.

    – chw21
    Mar 26 at 6:02















-1














Two different questions in one:



Why is the example program failing to compile:



This has nothing to do with public or private, or x being defined in the program itself.



It has everything to do with the fact that in the module, x as a parameter is defined as a 1-d array, and in the main program, y is a scalar.



Try it, remove the declaration of x in the main program and it will still fail.



(In fact, the declaration doesn't work like that anyway, you declare x as a 2-d array (shape 2, 1), but then give it a 1-d array. You'd have to do something like:



real, dimension(2, 1), parameter x = reshape([1.0, 2.0], [2, 1])


But to get rid of the error you describe, you either need to change the subroutine interface by removing the (:) behind the real, intent(in) :: x, or change the call to call test1([y]).



What can you do when 2 modules import different variables of the same name:



It would be different if you were to say have this:



module modA
implicit none
real, parameter :: x = 2.0
contains
subroutine subA(k)
real, intent(in) :: k
print *, k*x
end subroutine subA
end module modA

module modB
implicit none
real :: x(3)
end module modB

program progtest
use modA
use modB
implicit none
call subA(x(1))
end program progtest


In this example, it would try to import the variable x from both modules.



Ways to avoid it:




  1. Make one x private:



    implicit none
    real, parameter, private :: x = 2.0


    or



    real, parameter :: x = 2.0
    private :: x


    or



    implicit none
    private
    real, parameter :: x = 2.0
    public :: subA



  2. Only import the parts that you need:



    program progtest
    use modA, only: subA
    use modB
    implicit none
    ...



  3. Rename one or both of the x:



    use modA
    use modB, only: xB => x
    ...
    call subA(xB(1))






share|improve this answer

























  • The key is to make everything in the module PRIVATE and only expose those entities in the module that need to be PUBLIC.

    – evets
    Mar 26 at 4:26











  • In the example OP posted, the issue was absolutely not the private/public issue. His problem, according to the example, was the parameter type mismatch.

    – chw21
    Mar 26 at 5:47











  • I've updated my answer.

    – chw21
    Mar 26 at 6:02













-1












-1








-1







Two different questions in one:



Why is the example program failing to compile:



This has nothing to do with public or private, or x being defined in the program itself.



It has everything to do with the fact that in the module, x as a parameter is defined as a 1-d array, and in the main program, y is a scalar.



Try it, remove the declaration of x in the main program and it will still fail.



(In fact, the declaration doesn't work like that anyway, you declare x as a 2-d array (shape 2, 1), but then give it a 1-d array. You'd have to do something like:



real, dimension(2, 1), parameter x = reshape([1.0, 2.0], [2, 1])


But to get rid of the error you describe, you either need to change the subroutine interface by removing the (:) behind the real, intent(in) :: x, or change the call to call test1([y]).



What can you do when 2 modules import different variables of the same name:



It would be different if you were to say have this:



module modA
implicit none
real, parameter :: x = 2.0
contains
subroutine subA(k)
real, intent(in) :: k
print *, k*x
end subroutine subA
end module modA

module modB
implicit none
real :: x(3)
end module modB

program progtest
use modA
use modB
implicit none
call subA(x(1))
end program progtest


In this example, it would try to import the variable x from both modules.



Ways to avoid it:




  1. Make one x private:



    implicit none
    real, parameter, private :: x = 2.0


    or



    real, parameter :: x = 2.0
    private :: x


    or



    implicit none
    private
    real, parameter :: x = 2.0
    public :: subA



  2. Only import the parts that you need:



    program progtest
    use modA, only: subA
    use modB
    implicit none
    ...



  3. Rename one or both of the x:



    use modA
    use modB, only: xB => x
    ...
    call subA(xB(1))






share|improve this answer















Two different questions in one:



Why is the example program failing to compile:



This has nothing to do with public or private, or x being defined in the program itself.



It has everything to do with the fact that in the module, x as a parameter is defined as a 1-d array, and in the main program, y is a scalar.



Try it, remove the declaration of x in the main program and it will still fail.



(In fact, the declaration doesn't work like that anyway, you declare x as a 2-d array (shape 2, 1), but then give it a 1-d array. You'd have to do something like:



real, dimension(2, 1), parameter x = reshape([1.0, 2.0], [2, 1])


But to get rid of the error you describe, you either need to change the subroutine interface by removing the (:) behind the real, intent(in) :: x, or change the call to call test1([y]).



What can you do when 2 modules import different variables of the same name:



It would be different if you were to say have this:



module modA
implicit none
real, parameter :: x = 2.0
contains
subroutine subA(k)
real, intent(in) :: k
print *, k*x
end subroutine subA
end module modA

module modB
implicit none
real :: x(3)
end module modB

program progtest
use modA
use modB
implicit none
call subA(x(1))
end program progtest


In this example, it would try to import the variable x from both modules.



Ways to avoid it:




  1. Make one x private:



    implicit none
    real, parameter, private :: x = 2.0


    or



    real, parameter :: x = 2.0
    private :: x


    or



    implicit none
    private
    real, parameter :: x = 2.0
    public :: subA



  2. Only import the parts that you need:



    program progtest
    use modA, only: subA
    use modB
    implicit none
    ...



  3. Rename one or both of the x:



    use modA
    use modB, only: xB => x
    ...
    call subA(xB(1))







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 26 at 6:01

























answered Mar 26 at 3:40









chw21chw21

6,1528 silver badges21 bronze badges




6,1528 silver badges21 bronze badges












  • The key is to make everything in the module PRIVATE and only expose those entities in the module that need to be PUBLIC.

    – evets
    Mar 26 at 4:26











  • In the example OP posted, the issue was absolutely not the private/public issue. His problem, according to the example, was the parameter type mismatch.

    – chw21
    Mar 26 at 5:47











  • I've updated my answer.

    – chw21
    Mar 26 at 6:02

















  • The key is to make everything in the module PRIVATE and only expose those entities in the module that need to be PUBLIC.

    – evets
    Mar 26 at 4:26











  • In the example OP posted, the issue was absolutely not the private/public issue. His problem, according to the example, was the parameter type mismatch.

    – chw21
    Mar 26 at 5:47











  • I've updated my answer.

    – chw21
    Mar 26 at 6:02
















The key is to make everything in the module PRIVATE and only expose those entities in the module that need to be PUBLIC.

– evets
Mar 26 at 4:26





The key is to make everything in the module PRIVATE and only expose those entities in the module that need to be PUBLIC.

– evets
Mar 26 at 4:26













In the example OP posted, the issue was absolutely not the private/public issue. His problem, according to the example, was the parameter type mismatch.

– chw21
Mar 26 at 5:47





In the example OP posted, the issue was absolutely not the private/public issue. His problem, according to the example, was the parameter type mismatch.

– chw21
Mar 26 at 5:47













I've updated my answer.

– chw21
Mar 26 at 6:02





I've updated my answer.

– chw21
Mar 26 at 6:02








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348296%2fmaking-module-variables-private%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript