Skip to content

Standardize constructor method names #1203

Open
@jnbooth

Description

@jnbooth

There are currently no standardized naming conventions for constructor methods. For example, consider these methods:

QDate::from_string_enum(string: &QString, format: DateFormat) -> Option<Self>
QDateTime::from_string(string: &QString, format: DateFormat) -> Option<Self>
QTime::from_string_enum(string: &QString, format: DateFormat) -> Self

QDate::from_string_enum and QDateTime::from_string have the same signature, but different names.
QDate::from_string_enum and QTime::from_string_enum have the same name, but different signatures.

Proposal 1: Constructor methods should be the names of each parameter type, prefixed by from_. If they return Option<Self> rather than Self, the name should be suffixed by _opt, which is a common pattern in other crates. So the above would be replaced by:

QDate::from_qstring_dateformat_opt(string: &QString, format: DateFormat) -> Option<Self>
QDateTime::from_qstring_dateformat_opt(string: &QString, format: DateFormat) -> Option<Self>
QTime::from_qstring_dateformat(string: &QString, format: DateFormat) -> Self

Proposal 2: Deprecate all the constructor methods and just use From and TryFrom with tuples. So the above would be replaced by:

impl TryFrom<(&QString, DateFormat)> for QDate

impl TryFrom<(&QString, DateFormat)> for QDateTime

impl From<(&QString, DateFormat)> for QTime

Proposal 3: Do proposal 1 and the implementations from proposal 2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions