Skip to content

DerivedEnvironment

tgrapperon edited this page Aug 22, 2022 · 8 revisions

DerivedEnvironment

Use this property wrapper to declare child ComposableEnvironment in a ComposableEnvironment subclass.

@propertyWrapper
public final class DerivedEnvironment<Environment> where Environment: ComposableEnvironment 

You only need to specify the subclass used and its name. You don't need to instantiate the subclass. For example, if ChildEnvironment is a ComposableEnvironment subclass, you can install a representant in ParentEnvironment as:

class ParentEnvironment: ComposableEnvironment {
  @DerivedEnvironment<ChildEnvironment> var child
}.

This exposes a var child: ChildEnvironment read-only property in the ParentEnvironment. This child environment inherits the current dependencies of all its ancestor. They can be exposed using the Dependency property wrapper.

You can also use this property wrapper is to define DependencyAlias's using the AliasBuilder closure from the intializers:

struct ParentEnvironment: GlobalEnvironment {
  @DerivedEnvironment<ChildEnvironment>(aliases: {
    $0.alias(\.main, to: \.mainQueue)
  }) var child
}

Initializers

init(wrappedValue:aliases:)

See DerivedEnvironment discussion

public init(
    wrappedValue: Environment,
    aliases: (
      (AliasBuilder<Environment>)
        -> AliasBuilder<Environment>
    )? = nil
  ) 

init(aliases:)

See DerivedEnvironment discussion

public init(aliases: ((AliasBuilder<Environment>) -> AliasBuilder<Environment>)? = nil) 

init(wrappedValue:aliases:)

See DerivedEnvironment discussion

public init(
    wrappedValue: Environment,
    aliases: ((AliasBuilder<Environment>) -> AliasBuilder<Environment>)? = nil
  ) 

init(aliases:)

See DerivedEnvironment discussion

public init(aliases: ((AliasBuilder<Environment>) -> AliasBuilder<Environment>)? = nil) 

Properties

wrappedValue

@available(
    *, unavailable,
    message: "@DerivedEnvironment should be used in a ComposableEnvironment class."
  )
  public var wrappedValue: Environment 

wrappedValue

public var wrappedValue: Environment