What is the difference between fields and properties in Julia?What is the equivalent of getattr() in JuliaStruct literals in JuliaDefining constant global Variables in JuliaWhat is the connection between Refs and Broadcasting in JuliaJulia: Modifying the same field in an array of Mutable StructsDifferences between Python and Julia convolutions?Julia - Mutable struct with Attribute which is a Function and @code_warntypeDoes randsample function exit in Julia?Are there subtile differences between one and multiline if expressions in Julia?Are immutable julia structs ever safe to mutate and under what conditions can it be done in safe way?

How do I escape from a hanging Ubuntu OS?

Pregnant spouse slipped abortion pills unknowingly. What would the legal ramifications be?

Six letter words from "MONSTER"

Table columns widths are not equal

Set identification

How to have a second parameter word as input from the user

Does the sterile cockpit rule mean flight attendants could not inform the pilots if a passenger is in the lavatory while on final?

"Sack" data structure in C#

Can I Haste, booming blade, green flame blade and smite?

Is paying for portrait photos good for the people in the community you're photographing?

How to write rhead only on the first page?

Can homotopy colimits recover cohomology sheaves?

The key signatures C-flat minor and G-flat minor: do they exist?

I've never seen this before. Is this primarily a "rote computational trick" for multiplication by 9 ...?

What does "x employee is no longer employed by XYZ company" mean?

Any reason not to use global lambdas?

Can you wield a a typical shield and Aegis of the Raven Queen at the same time?

Speedup or Caching for a Multi-Iteration MIP problem

How can I force a bank to close my account with them?

How to optimise the use of 10 nuclear fuel pellets in medieval period?

What is the PDF for the minimum difference between a random number and a set of random numbers

Why Roll20 gives me two results instead of one?

Is the endomorphism ring of a module over a non-commutative ring always non-commutative?

Undesired blank space between some words



What is the difference between fields and properties in Julia?


What is the equivalent of getattr() in JuliaStruct literals in JuliaDefining constant global Variables in JuliaWhat is the connection between Refs and Broadcasting in JuliaJulia: Modifying the same field in an array of Mutable StructsDifferences between Python and Julia convolutions?Julia - Mutable struct with Attribute which is a Function and @code_warntypeDoes randsample function exit in Julia?Are there subtile differences between one and multiline if expressions in Julia?Are immutable julia structs ever safe to mutate and under what conditions can it be done in safe way?






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









22


















Julia has the setter functions setproperty! and setfield! and the getter functions getproperty and getfield that operate on structs. What is the difference between properties and fields in Julia?



For example, the following seems to indicate that they do the same thing:



julia> mutable struct S
a
end

julia> s = S(2)
S(2)

julia> getfield(s, :a)
2

julia> getproperty(s, :a)
2

julia> setfield!(s, :a, 3)
3

julia> s
S(3)

julia> setproperty!(s, :a, 4)
4

julia> s
S(4)









share|improve this question































    22


















    Julia has the setter functions setproperty! and setfield! and the getter functions getproperty and getfield that operate on structs. What is the difference between properties and fields in Julia?



    For example, the following seems to indicate that they do the same thing:



    julia> mutable struct S
    a
    end

    julia> s = S(2)
    S(2)

    julia> getfield(s, :a)
    2

    julia> getproperty(s, :a)
    2

    julia> setfield!(s, :a, 3)
    3

    julia> s
    S(3)

    julia> setproperty!(s, :a, 4)
    4

    julia> s
    S(4)









    share|improve this question



























      22













      22









      22


      3






      Julia has the setter functions setproperty! and setfield! and the getter functions getproperty and getfield that operate on structs. What is the difference between properties and fields in Julia?



      For example, the following seems to indicate that they do the same thing:



      julia> mutable struct S
      a
      end

      julia> s = S(2)
      S(2)

      julia> getfield(s, :a)
      2

      julia> getproperty(s, :a)
      2

      julia> setfield!(s, :a, 3)
      3

      julia> s
      S(3)

      julia> setproperty!(s, :a, 4)
      4

      julia> s
      S(4)









      share|improve this question














      Julia has the setter functions setproperty! and setfield! and the getter functions getproperty and getfield that operate on structs. What is the difference between properties and fields in Julia?



      For example, the following seems to indicate that they do the same thing:



      julia> mutable struct S
      a
      end

      julia> s = S(2)
      S(2)

      julia> getfield(s, :a)
      2

      julia> getproperty(s, :a)
      2

      julia> setfield!(s, :a, 3)
      3

      julia> s
      S(3)

      julia> setproperty!(s, :a, 4)
      4

      julia> s
      S(4)






      julia






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 1 at 8:25









      Kristoffer CarlssonKristoffer Carlsson

      1,0102 silver badges9 bronze badges




      1,0102 silver badges9 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          26



















          fields are simply the "components" of a struct. The struct



          struct A
          b
          c::Int
          end


          has the fields b and c. A call to getfield returns the object that is bound to the field:



          julia> a = A("foo", 3)
          A("foo", 3)

          julia> getfield(a, :b)
          "foo"


          In early versions of Julia, the syntax a.b used to "lower", i.e. be the same as, writing getfield(a, :b). What has changed now is that a.b lowers to getproperty(a, :b) with the default fallback



          getproperty(a::Type, v::Symbol) = getfield(a, v)


          So by default, nothing has changed. However, authors of structs can overload getproperty (it is not possible to overload getfield) to provide extra functionality to the dot-syntax:



          julia> function Base.getproperty(a::A, v::Symbol)
          if v == :c
          return getfield(a, :c) * 2
          elseif v == :q
          return "q"
          else
          return getfield(a, v)
          end
          end

          julia> a.q
          "q"

          julia> getfield(a, :q)
          ERROR: type A has no field q

          julia> a.c
          6

          julia> getfield(a, :c)
          3

          julia> a.b
          "foo"


          So we can add extra functionality to the dot syntax (dynamically if we want). As a concrete example where this is useful is for the package PyCall.jl where you used to have to write pyobject[:field] while it is possible now to implement it such that you can write pyobject.field.



          The difference between setfield! and setproperty! is analogous to the difference between getfield and getproperty, explained above.



          In addition, it is possible to hook into the function Base.propertynames to provide tab completion of properties in the REPL. By default, only the field names will be shown:



          julia> a.<TAB><TAB>
          b c


          But by overloading propertynames we can make it also show the extra property q:



          julia> Base.propertynames(::A) = (:b, :c, :q)

          julia> a.<TAB><TAB>
          b c q





          share|improve this answer



























          • So you cannot overload getfield?

            – Alfaizkhan
            Oct 1 at 9:51






          • 3





            No, getfield is a special (built-in) function. Trying to overload it will give the error cannot add methods to a builtin function.

            – Kristoffer Carlsson
            Oct 1 at 11:07












          • Maybe add that info to the answer somewhere?

            – StefanKarpinski
            Oct 1 at 13:29






          • 2





            The answer already explicitly says " (it is not possible to overload getfield)" so, in a sense, it is already there.

            – Kristoffer Carlsson
            Oct 1 at 14:13












          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/4.0/"u003ecc by-sa 4.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%2f58180806%2fwhat-is-the-difference-between-fields-and-properties-in-julia%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









          26



















          fields are simply the "components" of a struct. The struct



          struct A
          b
          c::Int
          end


          has the fields b and c. A call to getfield returns the object that is bound to the field:



          julia> a = A("foo", 3)
          A("foo", 3)

          julia> getfield(a, :b)
          "foo"


          In early versions of Julia, the syntax a.b used to "lower", i.e. be the same as, writing getfield(a, :b). What has changed now is that a.b lowers to getproperty(a, :b) with the default fallback



          getproperty(a::Type, v::Symbol) = getfield(a, v)


          So by default, nothing has changed. However, authors of structs can overload getproperty (it is not possible to overload getfield) to provide extra functionality to the dot-syntax:



          julia> function Base.getproperty(a::A, v::Symbol)
          if v == :c
          return getfield(a, :c) * 2
          elseif v == :q
          return "q"
          else
          return getfield(a, v)
          end
          end

          julia> a.q
          "q"

          julia> getfield(a, :q)
          ERROR: type A has no field q

          julia> a.c
          6

          julia> getfield(a, :c)
          3

          julia> a.b
          "foo"


          So we can add extra functionality to the dot syntax (dynamically if we want). As a concrete example where this is useful is for the package PyCall.jl where you used to have to write pyobject[:field] while it is possible now to implement it such that you can write pyobject.field.



          The difference between setfield! and setproperty! is analogous to the difference between getfield and getproperty, explained above.



          In addition, it is possible to hook into the function Base.propertynames to provide tab completion of properties in the REPL. By default, only the field names will be shown:



          julia> a.<TAB><TAB>
          b c


          But by overloading propertynames we can make it also show the extra property q:



          julia> Base.propertynames(::A) = (:b, :c, :q)

          julia> a.<TAB><TAB>
          b c q





          share|improve this answer



























          • So you cannot overload getfield?

            – Alfaizkhan
            Oct 1 at 9:51






          • 3





            No, getfield is a special (built-in) function. Trying to overload it will give the error cannot add methods to a builtin function.

            – Kristoffer Carlsson
            Oct 1 at 11:07












          • Maybe add that info to the answer somewhere?

            – StefanKarpinski
            Oct 1 at 13:29






          • 2





            The answer already explicitly says " (it is not possible to overload getfield)" so, in a sense, it is already there.

            – Kristoffer Carlsson
            Oct 1 at 14:13















          26



















          fields are simply the "components" of a struct. The struct



          struct A
          b
          c::Int
          end


          has the fields b and c. A call to getfield returns the object that is bound to the field:



          julia> a = A("foo", 3)
          A("foo", 3)

          julia> getfield(a, :b)
          "foo"


          In early versions of Julia, the syntax a.b used to "lower", i.e. be the same as, writing getfield(a, :b). What has changed now is that a.b lowers to getproperty(a, :b) with the default fallback



          getproperty(a::Type, v::Symbol) = getfield(a, v)


          So by default, nothing has changed. However, authors of structs can overload getproperty (it is not possible to overload getfield) to provide extra functionality to the dot-syntax:



          julia> function Base.getproperty(a::A, v::Symbol)
          if v == :c
          return getfield(a, :c) * 2
          elseif v == :q
          return "q"
          else
          return getfield(a, v)
          end
          end

          julia> a.q
          "q"

          julia> getfield(a, :q)
          ERROR: type A has no field q

          julia> a.c
          6

          julia> getfield(a, :c)
          3

          julia> a.b
          "foo"


          So we can add extra functionality to the dot syntax (dynamically if we want). As a concrete example where this is useful is for the package PyCall.jl where you used to have to write pyobject[:field] while it is possible now to implement it such that you can write pyobject.field.



          The difference between setfield! and setproperty! is analogous to the difference between getfield and getproperty, explained above.



          In addition, it is possible to hook into the function Base.propertynames to provide tab completion of properties in the REPL. By default, only the field names will be shown:



          julia> a.<TAB><TAB>
          b c


          But by overloading propertynames we can make it also show the extra property q:



          julia> Base.propertynames(::A) = (:b, :c, :q)

          julia> a.<TAB><TAB>
          b c q





          share|improve this answer



























          • So you cannot overload getfield?

            – Alfaizkhan
            Oct 1 at 9:51






          • 3





            No, getfield is a special (built-in) function. Trying to overload it will give the error cannot add methods to a builtin function.

            – Kristoffer Carlsson
            Oct 1 at 11:07












          • Maybe add that info to the answer somewhere?

            – StefanKarpinski
            Oct 1 at 13:29






          • 2





            The answer already explicitly says " (it is not possible to overload getfield)" so, in a sense, it is already there.

            – Kristoffer Carlsson
            Oct 1 at 14:13













          26















          26











          26









          fields are simply the "components" of a struct. The struct



          struct A
          b
          c::Int
          end


          has the fields b and c. A call to getfield returns the object that is bound to the field:



          julia> a = A("foo", 3)
          A("foo", 3)

          julia> getfield(a, :b)
          "foo"


          In early versions of Julia, the syntax a.b used to "lower", i.e. be the same as, writing getfield(a, :b). What has changed now is that a.b lowers to getproperty(a, :b) with the default fallback



          getproperty(a::Type, v::Symbol) = getfield(a, v)


          So by default, nothing has changed. However, authors of structs can overload getproperty (it is not possible to overload getfield) to provide extra functionality to the dot-syntax:



          julia> function Base.getproperty(a::A, v::Symbol)
          if v == :c
          return getfield(a, :c) * 2
          elseif v == :q
          return "q"
          else
          return getfield(a, v)
          end
          end

          julia> a.q
          "q"

          julia> getfield(a, :q)
          ERROR: type A has no field q

          julia> a.c
          6

          julia> getfield(a, :c)
          3

          julia> a.b
          "foo"


          So we can add extra functionality to the dot syntax (dynamically if we want). As a concrete example where this is useful is for the package PyCall.jl where you used to have to write pyobject[:field] while it is possible now to implement it such that you can write pyobject.field.



          The difference between setfield! and setproperty! is analogous to the difference between getfield and getproperty, explained above.



          In addition, it is possible to hook into the function Base.propertynames to provide tab completion of properties in the REPL. By default, only the field names will be shown:



          julia> a.<TAB><TAB>
          b c


          But by overloading propertynames we can make it also show the extra property q:



          julia> Base.propertynames(::A) = (:b, :c, :q)

          julia> a.<TAB><TAB>
          b c q





          share|improve this answer
















          fields are simply the "components" of a struct. The struct



          struct A
          b
          c::Int
          end


          has the fields b and c. A call to getfield returns the object that is bound to the field:



          julia> a = A("foo", 3)
          A("foo", 3)

          julia> getfield(a, :b)
          "foo"


          In early versions of Julia, the syntax a.b used to "lower", i.e. be the same as, writing getfield(a, :b). What has changed now is that a.b lowers to getproperty(a, :b) with the default fallback



          getproperty(a::Type, v::Symbol) = getfield(a, v)


          So by default, nothing has changed. However, authors of structs can overload getproperty (it is not possible to overload getfield) to provide extra functionality to the dot-syntax:



          julia> function Base.getproperty(a::A, v::Symbol)
          if v == :c
          return getfield(a, :c) * 2
          elseif v == :q
          return "q"
          else
          return getfield(a, v)
          end
          end

          julia> a.q
          "q"

          julia> getfield(a, :q)
          ERROR: type A has no field q

          julia> a.c
          6

          julia> getfield(a, :c)
          3

          julia> a.b
          "foo"


          So we can add extra functionality to the dot syntax (dynamically if we want). As a concrete example where this is useful is for the package PyCall.jl where you used to have to write pyobject[:field] while it is possible now to implement it such that you can write pyobject.field.



          The difference between setfield! and setproperty! is analogous to the difference between getfield and getproperty, explained above.



          In addition, it is possible to hook into the function Base.propertynames to provide tab completion of properties in the REPL. By default, only the field names will be shown:



          julia> a.<TAB><TAB>
          b c


          But by overloading propertynames we can make it also show the extra property q:



          julia> Base.propertynames(::A) = (:b, :c, :q)

          julia> a.<TAB><TAB>
          b c q






          share|improve this answer















          share|improve this answer




          share|improve this answer








          edited Oct 1 at 9:46

























          answered Oct 1 at 8:32









          Kristoffer CarlssonKristoffer Carlsson

          1,0102 silver badges9 bronze badges




          1,0102 silver badges9 bronze badges















          • So you cannot overload getfield?

            – Alfaizkhan
            Oct 1 at 9:51






          • 3





            No, getfield is a special (built-in) function. Trying to overload it will give the error cannot add methods to a builtin function.

            – Kristoffer Carlsson
            Oct 1 at 11:07












          • Maybe add that info to the answer somewhere?

            – StefanKarpinski
            Oct 1 at 13:29






          • 2





            The answer already explicitly says " (it is not possible to overload getfield)" so, in a sense, it is already there.

            – Kristoffer Carlsson
            Oct 1 at 14:13

















          • So you cannot overload getfield?

            – Alfaizkhan
            Oct 1 at 9:51






          • 3





            No, getfield is a special (built-in) function. Trying to overload it will give the error cannot add methods to a builtin function.

            – Kristoffer Carlsson
            Oct 1 at 11:07












          • Maybe add that info to the answer somewhere?

            – StefanKarpinski
            Oct 1 at 13:29






          • 2





            The answer already explicitly says " (it is not possible to overload getfield)" so, in a sense, it is already there.

            – Kristoffer Carlsson
            Oct 1 at 14:13
















          So you cannot overload getfield?

          – Alfaizkhan
          Oct 1 at 9:51





          So you cannot overload getfield?

          – Alfaizkhan
          Oct 1 at 9:51




          3




          3





          No, getfield is a special (built-in) function. Trying to overload it will give the error cannot add methods to a builtin function.

          – Kristoffer Carlsson
          Oct 1 at 11:07






          No, getfield is a special (built-in) function. Trying to overload it will give the error cannot add methods to a builtin function.

          – Kristoffer Carlsson
          Oct 1 at 11:07














          Maybe add that info to the answer somewhere?

          – StefanKarpinski
          Oct 1 at 13:29





          Maybe add that info to the answer somewhere?

          – StefanKarpinski
          Oct 1 at 13:29




          2




          2





          The answer already explicitly says " (it is not possible to overload getfield)" so, in a sense, it is already there.

          – Kristoffer Carlsson
          Oct 1 at 14:13





          The answer already explicitly says " (it is not possible to overload getfield)" so, in a sense, it is already there.

          – Kristoffer Carlsson
          Oct 1 at 14:13




















          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%2f58180806%2fwhat-is-the-difference-between-fields-and-properties-in-julia%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

          Tamil (spriik) Luke uk diar | Nawigatjuun

          Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

          Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?