Sure, I would retract the IllegalArgumentException idea. Clearly the documentation shows that fromProduct is calling the constructor directly, so that will be the assumption everyone has built code upon up to this point (but then there never was protection against classes that throw exceptions in their constructor - so to be truly a generic decoder you should naturally assume a constructor might throw).
fromProduct is still “unsafe” in that if you pass a Product of the wrong arity or containing fields that cannot cast to the constructor parameter’s expected type then you would get an exception anyway.
to go back to a more “safe” default - i am still in favor of the restriction to prevent compiler generated summon of Mirror outside the class scope if there is private constructor
this is quite hard to detect automatically as this is just a convention, not something we can specifically look for, so this would likely need an explicit opt-out (which you can do with a curried constructor - that will immediately refuse the “generic class” check)
One indispensable and crucial part of the smart constructor convention is that the class constructor is private. So it should be sufficient to enforce private access, no? In other words, no external code should be allowed to call the private constructor, period. Not by means of reflection, not by means of modifying the bytecode-level accessors, not by anything. The class author added the “private” keyword for a reason. All code outside should respect it.
yes but by hard to prevent “circumventing the smart constructor” i mean even if you did restrict summoning a mirror to only the scope of a class that has a private constructor, then the Mirror.fromProduct would still be incorrect as the compiler can’t detect whatever method happens to be your smart constructor without an explicit marker, (or by “blessing” some pattern officially, but im sure everyone has their preferred validation library type) so then as an alternative you can opt out of any mirror by making your class no longer “generic”
I would expect the mirror summoned in the scope of the class to just be the same mirror that would be summoned for a class without a private constructor. Presumably the author of the class knows how to use the mirror safely.
We don’t need any special blessing or marker. Publicly, the class simply does should not support macros that need Mirror.fromProduct. Whatever other facility the macro library has for non-trivial constructors has to be used instead.
case class is being supported with Shapeless if it’s constructor is public, it’s non-abstract, nothing prevent access to apply/unapply
if the callsite cannot access constructor/apply/unapply - derivation is disabled, simple as that. User should define implicit in the companion OR define it by hand where they need it, calling smart constructor by hand and wiring the result into type classes API for error handling
anything non-standard requires manual integration
That’s safe by design, it can we extended by macro author to support more things OOTB, but even when it’s completely safe macro calling smart constructor without user’s knowledge feels magical and most users hate it, so even then it needs a clear explanation and a way to preview the code.
I am against Mirror.ProductOf being available in places where constructor would not be. I already checked that on Scala 3 private constructor hides:
new
copy
fromProduct(!)
from call sites that would not be able to access it, and Mirrors should follow that behavior rather than circumvent it.
That’s the only thing I want to solve in this thread.
Whether or not there should additionally be some mechanism for the users, to define a Mirror for smart constructors in a safe way, is not my concern, it should be done in a different thread. IMHO it hardly make sense to define a standard mirror for them, if there is no standard on a smart constructor result type.
If we all agree that Mirror.ProductOf should not be available outside a class if the constructor is private, can we agree on whether it’s a bug (which could be fixed now, even in 3.9.1) or something that requires Pre-SIP (and would land in 3.10.2 or even 3.11.0)?
if someone does want to provide the same functionality then i think there is no choice but to manually write the companion as a mirror - binary compatible so far, except they would need to introduce a public accessor to provide the mirror (before compiler would just cast the companion)
If I understand the proposal correctly, then the “only” is still too much, and we should disallow even that. Consider the following code, allowed under @bishabosha’s proposal (as the companion object’s scope can call the private constructor):
object Email {
val address = summon[Mirror.ProductOf[Email]].fromProduct(Tuple1("X@Y"))
}
This code violates my expectations about the “private” keyword. When I make a constructor private, I expect that new Email is only called within the allowed scope (e.g., within the Email companion object). But here, new Email is not called within the companion object but somewhere else - within the standard library’s methods.
I haven’t allowed anyone (including the standard library) to call my private constructor. Then why do they call it freely, and why is the compiler complicit?
because the code for the mirror is defined within the companion object, so its within the scope - the compiler generates a fromProduct method in Email and Email extends Mirror.FromProduct.
The api of a case class is that it has a mirror, so yes we should restrict its access outside if the constructor is private. and leave it up to proposals for if there should be a “smart constructor” aware mirror
so therefore any consumer of a published library containing email will still be able to use it as a Mirror. My opinion is that we could perhaps introduce an Invisible modifier for traits so that a concrete class that extends them is not visible as that trait without casting the object.
But if you are really that conscious then for sure a quick change solves all of that:
- case class Email private (value: String)
+ case class Email private (value: String)() // no more Mirror
and this should be a contained change as only callsites within the class change