Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/traitify-ts/pulls/3
This commit was merged in pull request #3.
This commit is contained in:
28
src/Trait.ts
28
src/Trait.ts
@@ -1,9 +1,13 @@
|
||||
import { Fn, Pipe, Tuples } from "hotscript"
|
||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
||||
import { AbstractTag, TraitExpression, emptyTraitExpression } from "."
|
||||
import { TraitExpression, emptyTraitExpression } from "./TraitExpression"
|
||||
import { AbstractTag } from "./abstract"
|
||||
import { ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util"
|
||||
|
||||
|
||||
export type TraitApplierSuperTag = "@thilawyn/traitify-ts/TraitApplierSuper"
|
||||
|
||||
|
||||
export type AddAbstractToImplClass<
|
||||
ImplClass extends Class<{}, []>,
|
||||
Abstract extends {},
|
||||
@@ -16,14 +20,14 @@ export type AddAbstractToImplClass<
|
||||
)
|
||||
|
||||
export type RemoveAbstractFromImplClass<
|
||||
ImplClassWithAbstract extends Class<Abstract, []>,
|
||||
ImplClassWithAbstract extends Class<Abstract, []> & { _tag: TraitApplierSuperTag },
|
||||
Abstract extends {},
|
||||
> = (
|
||||
Class<
|
||||
Omit<InstanceType<ImplClassWithAbstract>, keyof Abstract>,
|
||||
ConstructorParameters<ImplClassWithAbstract>
|
||||
> &
|
||||
StaticMembers<ImplClassWithAbstract>
|
||||
Omit<StaticMembers<ImplClassWithAbstract>, "_tag">
|
||||
)
|
||||
|
||||
|
||||
@@ -110,7 +114,9 @@ export namespace Trait {
|
||||
Trait.Instance<T>,
|
||||
any[]
|
||||
> &
|
||||
Pipe<[...Trait.Supertraits<T>, T], [
|
||||
Pipe<T, [
|
||||
Trait.SupertraitsFn,
|
||||
Tuples.Append<T>,
|
||||
Tuples.Map<Trait.OwnClassFn>,
|
||||
Tuples.Map<StaticMembersFn>,
|
||||
ExtendFn,
|
||||
@@ -122,7 +128,9 @@ export namespace Trait {
|
||||
}
|
||||
|
||||
export type Instance<T> = (
|
||||
Pipe<[...Trait.Supertraits<T>, T], [
|
||||
Pipe<T, [
|
||||
Trait.SupertraitsFn,
|
||||
Tuples.Append<T>,
|
||||
Tuples.Map<Trait.OwnInstanceFn>,
|
||||
ExtendFn,
|
||||
SimplifyFn,
|
||||
@@ -134,16 +142,12 @@ export namespace Trait {
|
||||
}
|
||||
|
||||
|
||||
export type TraitApplierSuperTag = "@thilawyn/traitify-ts/TraitApplierSuper"
|
||||
|
||||
export function trait<
|
||||
Abstract extends {},
|
||||
ImplClassWithAbstract extends Class<Abstract, []>,
|
||||
ImplClassWithAbstract extends Class<Abstract, []> & { _tag: TraitApplierSuperTag },
|
||||
>(
|
||||
abstract: Opaque<Abstract, AbstractTag>,
|
||||
apply: (Super: Opaque<AbstractClass<Abstract>, TraitApplierSuperTag>) => (
|
||||
Opaque<ImplClassWithAbstract, TraitApplierSuperTag>
|
||||
),
|
||||
abstract: Opaque<Abstract, AbstractTag>,
|
||||
apply: (Super: AbstractClass<Abstract> & { _tag: TraitApplierSuperTag }) => ImplClassWithAbstract,
|
||||
) {
|
||||
return new Trait(
|
||||
emptyTraitExpression,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Call, Fn, Pipe, Tuples } from "hotscript"
|
||||
import { Fn, Pipe, Tuples } from "hotscript"
|
||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
||||
import { AbstractTag, RemoveAbstractFromImplClass, Trait, TraitApplierSuperTag } from "."
|
||||
import { RemoveAbstractFromImplClass, Trait, TraitApplierSuperTag } from "./Trait"
|
||||
import { AbstractTag } from "./abstract"
|
||||
import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
|
||||
|
||||
|
||||
@@ -12,9 +13,9 @@ import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
|
||||
|
||||
|
||||
export class TraitExpression<
|
||||
Superclass extends AbstractClass<{}>,
|
||||
OwnTraits extends Trait<any, any, any>[],
|
||||
AllTraits extends Trait<any, any, any>[],
|
||||
Superclass extends AbstractClass<{}>,
|
||||
const OwnTraits extends Trait<any, any, any>[],
|
||||
const AllTraits extends Trait<any, any, any>[],
|
||||
> {
|
||||
constructor(
|
||||
readonly superclass: Superclass,
|
||||
@@ -24,40 +25,38 @@ export class TraitExpression<
|
||||
|
||||
get extends(): (
|
||||
AbstractClass<
|
||||
Pipe<[
|
||||
InstanceType<Superclass>,
|
||||
...Call<Tuples.Map<Trait.OwnImplInstanceFn>, AllTraits>,
|
||||
], [
|
||||
ExtendFn,
|
||||
SimplifyFn,
|
||||
Pipe<AllTraits, [
|
||||
Tuples.Map<Trait.OwnImplInstanceFn>, // Map all the traits to the instance of their implementation class
|
||||
Tuples.Prepend<InstanceType<Superclass>>, // Add the instance of the superclass at the top of the list
|
||||
ExtendFn, // Reduce to a single instance that extends all the instances in the list
|
||||
SimplifyFn, // Make readable for IDEs
|
||||
]>,
|
||||
|
||||
ConstructorParameters<Superclass>
|
||||
> &
|
||||
|
||||
Pipe<[
|
||||
Superclass,
|
||||
...Call<Tuples.Map<Trait.OwnImplClassFn>, AllTraits>,
|
||||
], [
|
||||
Tuples.Map<StaticMembersFn>,
|
||||
ExtendFn,
|
||||
SimplifyFn,
|
||||
Pipe<AllTraits, [
|
||||
Tuples.Map<Trait.OwnImplClassFn>, // Map all the traits to their implementation class
|
||||
Tuples.Prepend<Superclass>, // Add the superclass at the top of the list
|
||||
Tuples.Map<StaticMembersFn>, // Map all the classes to an object containing their static members
|
||||
ExtendFn, // Reduce to a single object that extends all the objects in the list
|
||||
SimplifyFn, // Make readable for IDEs
|
||||
]>
|
||||
) {
|
||||
return this.allTraits.reduce(
|
||||
(previous, trait) => trait.apply(previous),
|
||||
this.superclass as Opaque<Superclass, TraitApplierSuperTag>,
|
||||
this.superclass,
|
||||
) as any
|
||||
}
|
||||
|
||||
subtrait<
|
||||
SubtraitAbstract extends Implements<typeof this>,
|
||||
SubtraitImplClassWithAbstract extends Class<SubtraitAbstract, []>,
|
||||
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any, any>,
|
||||
SubtraitAbstract extends Implements<This>,
|
||||
SubtraitImplClassWithAbstract extends Class<SubtraitAbstract, []> & { _tag: TraitApplierSuperTag },
|
||||
>(
|
||||
abstract: (expression: typeof this) => Opaque<SubtraitAbstract, AbstractTag>,
|
||||
apply: (Super: Opaque<AbstractClass<SubtraitAbstract>, TraitApplierSuperTag>) => (
|
||||
Opaque<SubtraitImplClassWithAbstract, TraitApplierSuperTag>
|
||||
),
|
||||
this: This,
|
||||
abstract: (expression: This) => Opaque<SubtraitAbstract, AbstractTag>,
|
||||
apply: (Super: AbstractClass<SubtraitAbstract> & { _tag: TraitApplierSuperTag }) => SubtraitImplClassWithAbstract,
|
||||
) {
|
||||
return new Trait(
|
||||
this,
|
||||
@@ -69,7 +68,9 @@ export class TraitExpression<
|
||||
}
|
||||
|
||||
export namespace TraitExpression {
|
||||
export class NullSuperclass {}
|
||||
export class NullSuperclass {
|
||||
static readonly _tag = "@thilawyn/traitify-ts/TraitExpression.NullSuperclass"
|
||||
}
|
||||
|
||||
export type Superclass<T> = (
|
||||
T extends TraitExpression<infer Superclass, any, any>
|
||||
@@ -99,19 +100,14 @@ export namespace TraitExpression {
|
||||
}
|
||||
}
|
||||
|
||||
export const emptyTraitExpression = new TraitExpression(
|
||||
TraitExpression.NullSuperclass,
|
||||
[] as const,
|
||||
[] as const,
|
||||
)
|
||||
export const emptyTraitExpression = new TraitExpression(TraitExpression.NullSuperclass, [], [])
|
||||
|
||||
|
||||
export type Implements<Exp extends TraitExpression<any, any, any>> = (
|
||||
Exp extends TraitExpression<any, infer AllTraits, any>
|
||||
? Pipe<AllTraits, [
|
||||
Tuples.Map<Trait.OwnAbstractFn>,
|
||||
ExtendFn,
|
||||
SimplifyFn,
|
||||
]>
|
||||
: never
|
||||
Pipe<Exp, [
|
||||
TraitExpression.AllTraitsFn,
|
||||
Tuples.Map<Trait.OwnAbstractFn>,
|
||||
ExtendFn,
|
||||
SimplifyFn,
|
||||
]>
|
||||
)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Call, Fn, Pipe, Tuples } from "hotscript"
|
||||
import { AbstractClass } from "type-fest"
|
||||
import { Trait, TraitExpression, emptyTraitExpression } from "."
|
||||
import { Trait } from "./Trait"
|
||||
import { TraitExpression, emptyTraitExpression } from "./TraitExpression"
|
||||
import { ExtendableFn, StaticMembersFn } from "./util"
|
||||
|
||||
|
||||
@@ -19,9 +20,9 @@ type AbstractMembersExtendable<
|
||||
Superclass extends AbstractClass<{}>,
|
||||
Traits extends Trait<any, any, any>[],
|
||||
> = (
|
||||
Call<ExtendableFn, [
|
||||
InstanceType<Superclass>,
|
||||
...Call<Tuples.Map<Trait.OwnAbstractFn>, Traits>,
|
||||
Pipe<Traits, [
|
||||
Tuples.Map<Trait.OwnAbstractFn>,
|
||||
Tuples.Prepend<InstanceType<Superclass>>,
|
||||
]>
|
||||
)
|
||||
|
||||
@@ -29,9 +30,9 @@ type ImplInstanceExtendable<
|
||||
Superclass extends AbstractClass<{}>,
|
||||
Traits extends Trait<any, any, any>[],
|
||||
> = (
|
||||
Call<ExtendableFn, [
|
||||
InstanceType<Superclass>,
|
||||
...Call<Tuples.Map<Trait.OwnImplInstanceFn>, Traits>,
|
||||
Pipe<Traits, [
|
||||
Tuples.Map<Trait.OwnImplInstanceFn>,
|
||||
Tuples.Prepend<InstanceType<Superclass>>,
|
||||
]>
|
||||
)
|
||||
|
||||
@@ -39,10 +40,9 @@ type ImplStaticMembersExtendable<
|
||||
Superclass extends AbstractClass<{}>,
|
||||
Traits extends Trait<any, any, any>[],
|
||||
> = (
|
||||
Pipe<[
|
||||
Superclass,
|
||||
...Call<Tuples.Map<Trait.OwnImplClassFn>, Traits>,
|
||||
], [
|
||||
Pipe<Traits, [
|
||||
Tuples.Map<Trait.OwnImplClassFn>,
|
||||
Tuples.Prepend<Superclass>,
|
||||
Tuples.Map<StaticMembersFn>,
|
||||
ExtendableFn,
|
||||
]>
|
||||
@@ -53,24 +53,22 @@ type BuildTraitExpression<
|
||||
OwnTraits extends Trait<any, any, any>[],
|
||||
AllTraits extends Trait<any, any, any>[],
|
||||
> = (
|
||||
Call<Tuples.IsEmpty, AllTraits> extends true
|
||||
? "Cannot express an empty list of traits."
|
||||
: AbstractMembersExtendable<Superclass, AllTraits> extends false
|
||||
? "Type conflict between the traits abstract members and/or the superclass instance."
|
||||
: ImplInstanceExtendable<Superclass, AllTraits> extends false
|
||||
? "Type conflict between the traits implementation instances and/or the superclass instance."
|
||||
: ImplStaticMembersExtendable<Superclass, AllTraits> extends false
|
||||
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
||||
: TraitExpression<Superclass, OwnTraits, AllTraits>
|
||||
AbstractMembersExtendable<Superclass, AllTraits> extends false
|
||||
? "Type conflict between the traits abstract members and/or the superclass instance."
|
||||
: ImplInstanceExtendable<Superclass, AllTraits> extends false
|
||||
? "Type conflict between the traits implementation instances and/or the superclass instance."
|
||||
: ImplStaticMembersExtendable<Superclass, AllTraits> extends false
|
||||
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
||||
: TraitExpression<Superclass, OwnTraits, AllTraits>
|
||||
)
|
||||
|
||||
|
||||
class TraitExpressionBuilder<
|
||||
Super extends AbstractClass<{}>,
|
||||
OwnTraits extends Trait<any, any, any>[],
|
||||
AllTraits extends Trait<any, any, any>[],
|
||||
Superclass extends AbstractClass<{}>,
|
||||
const OwnTraits extends Trait<any, any, any>[],
|
||||
const AllTraits extends Trait<any, any, any>[],
|
||||
> {
|
||||
constructor(private expression: TraitExpression<Super, OwnTraits, AllTraits>) {}
|
||||
constructor(private expression: TraitExpression<Superclass, OwnTraits, AllTraits>) {}
|
||||
|
||||
extends<
|
||||
Super extends AbstractClass<any>
|
||||
@@ -87,25 +85,25 @@ class TraitExpressionBuilder<
|
||||
}
|
||||
|
||||
expresses<
|
||||
Traits extends Trait<any, any, any>[]
|
||||
const Traits extends Trait<any, any, any>[]
|
||||
>(
|
||||
...traits: Traits
|
||||
): TraitExpressionBuilder<
|
||||
Super,
|
||||
Superclass,
|
||||
[...OwnTraits, ...Traits],
|
||||
[...AllTraits, ...SpreadSupertraits<Traits>]
|
||||
> {
|
||||
return new TraitExpressionBuilder(
|
||||
new TraitExpression(
|
||||
this.expression.superclass,
|
||||
[...this.expression.ownTraits, ...traits] as const,
|
||||
[...this.expression.allTraits, ...this.spreadSupertraits(traits)] as const,
|
||||
[...this.expression.ownTraits, ...traits],
|
||||
[...this.expression.allTraits, ...this.spreadSupertraits(traits)],
|
||||
)
|
||||
) as any
|
||||
)
|
||||
}
|
||||
|
||||
private spreadSupertraits<
|
||||
Traits extends Trait<
|
||||
const Traits extends Trait<
|
||||
TraitExpression<any, any, Trait<any, any, any>[]>,
|
||||
any,
|
||||
any
|
||||
@@ -120,7 +118,7 @@ class TraitExpressionBuilder<
|
||||
}
|
||||
|
||||
build() {
|
||||
return this.expression as BuildTraitExpression<Super, OwnTraits, AllTraits>
|
||||
return this.expression as BuildTraitExpression<Superclass, OwnTraits, AllTraits>
|
||||
}
|
||||
|
||||
then<V>(fn: (expression: ReturnType<typeof this.build>) => V): V {
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
export * from "./Trait"
|
||||
export * from "./TraitExpression"
|
||||
export * from "./TraitExpressionBuilder"
|
||||
export * from "./abstract"
|
||||
@@ -1,3 +1,4 @@
|
||||
export {
|
||||
AbstractTag, Implements, TraitApplierSuperTag, abstract, expression, trait, type Trait, type TraitExpression
|
||||
} from "."
|
||||
export { trait, type Trait } from "./Trait"
|
||||
export { Implements, type TraitExpression } from "./TraitExpression"
|
||||
export { expression } from "./TraitExpressionBuilder"
|
||||
export { abstract } from "./abstract"
|
||||
|
||||
13
src/tests.ts
13
src/tests.ts
@@ -1,17 +1,21 @@
|
||||
import { Simplify } from "type-fest"
|
||||
import { Implements, Trait, abstract, expression, trait } from "."
|
||||
import { Pipe } from "hotscript"
|
||||
import { Trait, trait } from "./Trait"
|
||||
import { Implements } from "./TraitExpression"
|
||||
import { expression } from "./TraitExpressionBuilder"
|
||||
import { abstract } from "./abstract"
|
||||
|
||||
|
||||
const PrintsHelloOnNew = trait(
|
||||
abstract(),
|
||||
Super => class PrintsHelloOnNew extends Super {
|
||||
static readonly isPrintsHelloOnNew = true
|
||||
|
||||
constructor(...args: any[]) {
|
||||
super(...args)
|
||||
console.log("Hello!")
|
||||
}
|
||||
},
|
||||
)
|
||||
type PrintsHelloOnNewClass = Trait.Class<typeof PrintsHelloOnNew>
|
||||
|
||||
const Identifiable = <ID>() => trait(
|
||||
abstract<{ readonly id: ID }>(),
|
||||
@@ -34,6 +38,7 @@ const StatefulSubscription = trait(
|
||||
|
||||
Super => class StatefulSubscription extends Super {},
|
||||
)
|
||||
type StatefulSubscriptionClass = Trait.Class<typeof StatefulSubscription>
|
||||
|
||||
const ActiveStatefulSubscription = expression
|
||||
.expresses(StatefulSubscription)
|
||||
@@ -51,7 +56,7 @@ const ActiveStatefulSubscription = expression
|
||||
Super => class ActiveStatefulSubscription extends Super {},
|
||||
)
|
||||
|
||||
type T = Trait.Instance<typeof ActiveStatefulSubscription>
|
||||
type ActiveStatefulSubscriptionClass = Trait.Class<typeof ActiveStatefulSubscription>
|
||||
|
||||
|
||||
class TestSuperclass {
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
export * from "./extend"
|
||||
export * from "./inheritance"
|
||||
export * from "./misc"
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
export {
|
||||
Extend,
|
||||
ExtendFn,
|
||||
Extendable,
|
||||
ExtendableFn
|
||||
} from "."
|
||||
Reference in New Issue
Block a user