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:
@@ -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,
|
||||
]>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user