Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconsider self constructor #10

Open
jariji opened this issue Mar 3, 2024 · 1 comment
Open

Reconsider self constructor #10

jariji opened this issue Mar 3, 2024 · 1 comment

Comments

@jariji
Copy link

jariji commented Mar 3, 2024

:($T(self::$T) = self)

  • For collection types, I normally expect a constructor to copy it rather than returning it.
julia> x = []
Any[]

julia> y = Vector(x)
Any[]

julia> x === y
false
  • And for non-collection types with one field, it's not obvious that the result should even return a copy of the argument, rather than putting the argument into the field, as in a default constructor.
# Batteries
julia> mutable struct Foo 
           x
       end

julia> @batteries Foo
Foo

julia> x = Foo([])
Foo(Any[])

julia> y = Foo(x)
Foo(Any[])

# Normal

julia> struct Bar
           x
       end

julia> Bar(Bar([]))
Bar(Bar(Any[]))

Because this constructor behaves in unusual ways, I don't think it should be enabled by default. I'd lean toward just removing the option.

@jw3126
Copy link
Owner

jw3126 commented Mar 3, 2024

It is a good point, people might have different expectations of a selfconstructor. I agree, that selfconstructor=false may be the better default. But I am hesitant to do that breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants