-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
[Doc] Tweaked legacy documentation is misleading and does not fit in the context #34414
Comments
Could you spell out more directly what is wrong and misleading? |
Doc's thesis: Immutable composite types with no fields are singletons Doc uses I criticize that the Doc's example is not a valid proof. |
You mean it is not a valid "proof" because even if the struct would have fields it would still compare
But how is this relevant when it is explicitly mentioned that a singleton has to be non-mutable? |
Maybe my expressions are too complex. I'll post in the following the misleading paragraphs:
julia> struct NoFields
end
julia> NoFields() === NoFields()
true
You see? The last sentence is wrong. |
Counter-example: julia> struct YesFields
x
end
julia> YesFields(1) == YesFields(1)
true
julia> YesFields(1) === YesFields(1)
true |
So previously, |
Thank you for confirming that. I'll file a PR. |
No that is not a counter example. Your example proofs that there is ever only one instance of |
In another word, While it is also true that this property can now be seen as an extention of the semantics of immutable types, it does not invalidate the proof here. And despite this part of the semantics not being very special, singleton type still worth mentioning. In fact, this is where singleton type is defined on this page. If anything, you can add clarification that the semantics is consistent with normal composite types. The section about "Singleton Types" below does indeed seem confusing since it is used there to only mean |
===
is not a valid way to test singletons
Agreed on the confusing docs here, but it is worth mentioning in the devdocs that singleton types only ever have one boxed representation, which is fairly critical (so e.g. boxing |
It seems to me that the concept of singleton in Julia is not properly defined. In other languages, such as C++, a singleton's constructor is a private method. However, in Julia, you can freely and repeatedly construct it by calling the constructor: julia> Nothing() === nothing
true In addition, if the address is the golden rule of testing singletons, then the method of julia> a = "123"
"123"
julia> b = "123"
"123"
julia> pointer(a)
Ptr{UInt8} @0x00007fe97ae879f8
julia> pointer(b)
Ptr{UInt8} @0x00007fe97ae79318
julia> a === b
true |
It's very much well defined:
So a singleton type is a type for which at least one instance exists and for which all instances are |
Regarding julia> a = 1
1
julia> b = 1
1
julia> rand(a)
1-element Array{Float64,1}:
0.1397043590719047
julia> rand(b)
1-element Array{Float64,1}:
0.7414452172136339
julia> a === b
true |
@StefanKarpinski Maybe we shouldn't call it "singleton"; we could call it "equivalent class" formed by the relationship |
Sorry for my delayed reply: I was fighting against the epidemic in China. Finally, I realized the divergence between @StefanKarpinski and me.
If you agree, I would say the singleton type in Scala is equivalent to the value-type in Julia.
julia> struct NoFields
end
julia> NoFields() === NoFields()
true
|
Take care, I hope you are well and safe! If you can propose some clarification that would have helped you understand that the difference, that would be great. The word "pattern" doesn't appear anywhere when talking about singletons and if you parse the sentence you wanted to remove, the adjective "singleton" modifies the noun "type". Note that the word singleton predates the Gang of Four book and is more general than the design pattern—OOP does not own the word. |
I should, perhaps also point out, that the singleton design pattern is just a way of implementing a singleton type in a language that doesn't naturally have them. In OOP languages, since objects have object identity (because they are mutable and have private state, they have to), so the only way to implement a singleton type in such a language is to prevent instantiation of more than one instance at runtime. In languages like Julia, where real singleton types exist, that isn't necessary. |
In the types doc page, it says
Nonetheless,
===
is not a valid way to test singletons. In the doc page of===
, it saysTherefore,
===
is the same as==
for immutable types, and it does not compare the addresses. Indeed, I tried another immutable type but with fields; the result is the same.Then, I tested it for mutable types, and there was no chance:
In conclusion, the doc is wrong and misleading.
The text was updated successfully, but these errors were encountered: