Skip to content

keyword argument constructor for immutables #5333

Description

@Keno

@JeffBezanson and I were discussing adding a second default constructor with the semantics

immutable foo
bar::Int
baz::Int
# this is the usual default constructor
foo(bar,baz) = new(bar,baz)
# I am proposing this in addition
foo(x::foo;bar=nothing,baz=nothing) = 
    foo(bar === nothing ? x.bar : bar, baz === nothing ? x.baz : baz)
end

This would cover the case where you want to obtain a new value with one field changed e.g. in the example above

julia> foo(1,2)
foo(1,2)

julia> foo(ans,baz=3)
foo(1,3)

This is especially useful when the immutable has a lot of field. One example that comes to mind and where I already define a constructor like the above is URIParser.jl

Activity

  1. johnmyleswhite commented on Jan 9, 2014

    @johnmyleswhite
    Member

    I like this. If we're going to do something like this, could we also consider having both mutable and immutables types allow default values, which would allow the use of keyword arguments for construction from scratch (rather than based on a small variant of an existing immutable)? When writing my Vega wrapper I wound up writing up some hacky code to allow type fields to have default values and would love to have something more official.

  2. JeffBezanson commented on Jan 9, 2014

    @JeffBezanson
    SponsorMember

    Even better:

    foo(x::foo; bar=x.bar, baz=x.baz) = new(bar, baz)
    
  3. StefanKarpinski commented on Jan 9, 2014

    @StefanKarpinski
    SponsorMember

    I really like that last version. The business with nothing as a default feels icky. Any particular reason this only applies to immutables? I guess it's more useful with immutables since you can just copy and mutate a mutable object.

  4. added a commit that references this issue on Mar 12, 2014
    2802f21
  5. Keno commented on Aug 21, 2014

    @Keno
    MemberAuthor

    Relatedly it might make sense to have nice to have syntax sugar for modifying the field of an immutable when placed in an array.

  6. added this to the milestone on Aug 21, 2014
  7. rfourquet commented on Aug 22, 2014

    @rfourquet
    Member

    It would also be nice to not have to write down the type. In haskell it would be x=foo(1,2); y=x{baz=3} In elm (haskell-like pure language): y={x | baz <- 3} (see elm records). Is it too confusing to have x.baz = 3 translate to x = foo(x, baz=3) for immutables?

  8. JeffBezanson commented on Aug 22, 2014

    @JeffBezanson
    SponsorMember

    Yes, we dislike that idea since it means x.baz = 3 may or may not be a mutating operation depending on the type of x. That code will silently do subtly different things for different types of x.

  9. added 2 commits that reference this issue on Sep 21, 2014
    af56ba7
    42b476a
  10. added a commit that references this issue on Dec 9, 2014
    10384cb
  11. SimonDanisch commented on Dec 9, 2014

    @SimonDanisch
    Contributor

    Relatedly it might make sense to have nice to have syntax sugar for modifying the field of an immutable when placed in an array.
    @Keno, is there any work done in this direction?
    I actually wrote some code to do this, basically offering one more dimension for setindex, for arrays of immutables (well in my case fixed size arrays)...
    This is especially nice for editing color channels of images, when the color is an immutable.

  12. Keno commented on May 3, 2015

    @Keno
    MemberAuthor

    I think we need to start thinking about syntax for modifying immutables in place in arrays (and potentially in other mutables) now that that tuples can be used as fixed size arrays. Right now using them that way is quite painful.

  13. johnmyleswhite commented on Jun 27, 2015

    @johnmyleswhite
    Member

    Bump

  14. added a commit that references this issue on Jun 28, 2015
    9233943
  15. 2 remaining items

  16. ihnorton commented on Sep 28, 2015

    @ihnorton
    Member

    See #6122

  17. modified the milestones: 0.5.0, 0.4.x, 0.6.0 on Mar 30, 2016
  18. StefanKarpinski commented on Sep 13, 2016

    @StefanKarpinski
    SponsorMember

    I think we still want this – at least I periodically do and then wonder why we don't have it yet.

  19. modified the milestones: 1.0, 0.6.0 on Dec 28, 2016
  20. modified the milestones: , 1.0 on Jul 1, 2017
  21. StefanKarpinski commented on Jan 22, 2019

    @StefanKarpinski
    SponsorMember

    I don't believe this would be breaking, would it?

  22. tkf commented on Jan 22, 2019

    @tkf
    Member

    Wouldn't it break Adjoint?

  23. vtjnash commented on Jan 31, 2024

    @vtjnash
    SponsorMember

    Base now has @kwdef, which I guess pretty much replaces this, since we haven't had this for many releases now anyways

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions