Open cthulhu-rider opened 1 year ago
we may also provide decoding constructors
func NewFromBinary(b []byte) (T, error) {
var t T
return t, t.Unmarshal(b)
}
func NewFromJSON(j []byte) (T, error) {
var t T
return t, t.UnmarshalJSON(j)
}
and for some types
func NewFromString(s string) (T, error) {
var t T
return t, t.DecodeString(s)
}
could replace 2 instructions with 1 when there is no instance yet
there are several reasons, one of them https://github.com/nspcc-dev/neofs-sdk-go/pull/479#discussion_r1279235656
currently, we almost always initialize type instances via methods:
one of the obvious disadvantages of this approach is the need to explain in the docs (and hope that a beginner will read it) mandatory methods, without which the instance remains invalid
one of the obvious advantages is readability:
it's proposed to provide constructors - functions with required parameters. For example, for signatures:
with this
Calculate
/ReadFromV2
methods won't be needed (their use will remain in the reuse of the instance, but in practice this is rarely necessary)as we can see, we still need to write docs like
must be signed
and hope user will follow them. We can try to go further and make constructors finalizers, but that would require subtypes:or with different naming