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

Add conceptFrom functions for ConceptAs and deprecate static from methods #11

Merged
merged 3 commits into from
Aug 28, 2020

Conversation

woksin
Copy link
Contributor

@woksin woksin commented Aug 27, 2020

Can use the functions like this:

class SomeId extends ConceptAs<Guid, 'SomeId'> {
    constructor(id: Guid) {
        super(id, 'SomeId');   
    }
}

export function SomeIdFrom(id: Guid): SomeId {
    return conceptFrom(SomeId, id);
    // or return conceptFromFor(SomeId)(id);
}

const someId = SomeIdFrom(Guid.create());

Copy link
Contributor

@joelhoisko joelhoisko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I added some tests for it too (and had to try it out myself 😋).

Comment on lines +136 to +138
export function conceptFromFor<C extends ConceptAs<T, U>, T extends ConceptBase, U extends string>(conceptConstructor: Constructor<ConceptAs<T, U>>): (concept: T) => C {
return (concept: T) => conceptFrom(conceptConstructor, concept) as C;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a fat comment here about how to use this extra fancy conceptFromFor()() thingy but I accidentally closed the tab so in short:

Is there a reason you'd ever want to use the ()() fancyness over the clear conceptFrom(SomeId, id) method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like you can add a static method like this to your concept class:

class SomeId ...{
    /// ... class stuff ...

    static from: (concept: SomeId) {
        return conceptFromFor(SomeId);
    }
}

const someId = SomeId.from(Guid.create());

Or you could make reference on spot:

const toSomeId = conceptFromFor(SomeId);
const someId = toSomeId(Guid.create());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was the scenarios I had in mind. Since we don't have a way to inherit this static method in a good way

@woksin woksin requested a review from joelhoisko August 28, 2020 08:14
@joelhoisko joelhoisko merged commit bf1a474 into master Aug 28, 2020
@woksin woksin deleted the improve-concepts branch August 28, 2020 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants