trait
Factory[Output] extends Serializable
Type Members
-
abstract
type
Constructor
Abstract Value Members
-
abstract
val
newInstance: Constructor
Inherited by implicit conversion any2stringadd from
Factory[Output] to any2stringadd[Factory[Output]]
Inherited by implicit conversion StringFormat from
Factory[Output] to StringFormat[Factory[Output]]
Inherited by implicit conversion Ensuring from
Factory[Output] to Ensuring[Factory[Output]]
Inherited by implicit conversion ArrowAssoc from
Factory[Output] to ArrowAssoc[Factory[Output]]
A factory to create new instances, especially dynamic mix-ins.
Author:
杨博 (Yang Bo) <[email protected]>
Given a trait that has abstract members.
When creating a factory for the trait.
val factory = Factory[Foo]
Then the newInstance method of the factory should accept named arguments according to abstract members.
When using unnamed parameters, the parameters should be passed in alphabetical order
Given two traits that have no abstract member.
When creating a factory for mix-in type of the two types.
Then the newInstance method of the factory should accept no parameters.
Given a trait that contains an abstract method annotated as @inject.
When creating a factory for the trait
Then the
@inject
method will be replaced to an implicit value.It will not compile if no implicit value found. For example,
Foo[Symbol]
requires an implicit value of typeOrdering[Symbol]
, which is not availble."Factory[Foo[Symbol]]" shouldNot compile
If the trait does not contain abstract methods other than
@inject
methods, then the factory type class is a Factory.Factory0, which can be summoned by Predef.implicitly,and newInstance method is available on the Factory.Factory0 as well.
nullaryFactory.newInstance().orderingA should be(implicitly[Ordering[Int]])
However, if the nested type is an alias to another type outside of the type to create, then it is allowed
,This Factory disallows creating types that has an abstract member whose type depends on nested types. Given an abstract
val inner
whose type is theInner
,then the attempt to create factory for
,Outer
, likeFactory[Outer].newInstance(inner = None)
, will cause avalue inner has incompatible type
compile error.@inject works on implicit abstract methods as well.
,Factories may be nested
,Factories may create types that contains refinement types