function initializationClassMethodForSubtype()

in src/plugins/algebraic-type-initialization.ts [192:243]


function initializationClassMethodForSubtype(
  algebraicType: AlgebraicType.Type,
  subtype: AlgebraicType.Subtype,
): ObjC.Method {
  const openingCode: string[] = [
    algebraicType.name +
      ' *' +
      nameOfObjectWithinInitializer() +
      ' = [(Class)self new];',
    nameOfObjectWithinInitializer() +
      '->' +
      AlgebraicTypeUtils.valueAccessorForInstanceVariableStoringSubtype() +
      ' = ' +
      AlgebraicTypeUtils.EnumerationValueNameForSubtype(
        algebraicType,
        subtype,
      ) +
      ';',
  ];
  const assumeNonnull: boolean =
    algebraicType.includes.indexOf('RMAssumeNonnull') >= 0;
  const attributes: AlgebraicType.SubtypeAttribute[] =
    AlgebraicTypeUtils.attributesFromSubtype(subtype);
  const requiredParameterAssertions: string[] = attributes
    .filter(canAssertExistenceForTypeOfAttribute)
    .filter((attribute) => isRequiredAttribute(assumeNonnull, attribute))
    .map(toRequiredAssertion);
  const setterStatements: string[] = attributes.map((attribute) =>
    internalValueSettingCodeForAttribute(subtype, attribute),
  );

  return {
    preprocessors: [],
    belongsToProtocol: null,
    code: requiredParameterAssertions
      .concat(openingCode)
      .concat(setterStatements)
      .concat('return object;'),
    comments: ObjCCommentUtils.commentsAsBlockFromStringArray(
      commentsForSubtype(subtype),
    ),
    compilerAttributes: Maybe.catMaybes([swiftNameForSubtype(subtype)]),
    keywords: keywordsForSubtype(subtype),
    returnType: {
      type: {
        name: 'instancetype',
        reference: 'instancetype',
      },
      modifiers: [],
    },
  };
}