View Source cure_typeclass_codegen (cure v0.7.0)
Summary
Functions
Compiles an instance definition to BEAM code.
Compiles a typeclass definition to BEAM code.
Generates a callable function for a typeclass method dispatch.
Generates instance registration code for automatic registration on module load.
Processes a derive clause and generates instances.
Functions
-spec compile_instance(#instance_def{typeclass :: term(), type_args :: term(), constraints :: term(), methods :: term(), location :: term()}, term()) -> {ok, [term()], term()} | {error, term()}.
Compiles an instance definition to BEAM code.
Instances compile to regular Erlang functions with specialized naming:
show_Int/1for Show(Int).showeq_Point/2for Eq(Point).==compare_Person/2for Ord(Person).compare
Arguments
InstanceDef- Instance AST nodeState- Codegen state
Returns
{ok, CompiledFunctions, NewState}- List of compiled functions{error, Reason}- Compilation error
-spec compile_typeclass(#typeclass_def{name :: term(), type_params :: term(), constraints :: term(), methods :: term(), default_methods :: term(), location :: term()}, term()) -> {ok, term(), term()} | {error, term()}.
Compiles a typeclass definition to BEAM code.
Typeclasses compile to Erlang behaviour modules with:
- behaviour_info/1 callback specification
- Default method implementations
- Method signature documentation
Arguments
TypeclassDef- Typeclass AST nodeState- Codegen state
Returns
{ok, CompiledModule, NewState}- Compiled typeclass module{error, Reason}- Compilation error
-spec generate_instance_function(atom(), atom(), [term()], term()) -> {ok, term()} | {error, term()}.
Generates a callable function for a typeclass method dispatch.
Creates wrapper functions that dispatch to the correct instance:
show(X) when is_integer(X) -> show_Int(X);
show(X) when is_float(X) -> show_Float(X);
show(X) when is_list(X) -> show_List(X).Arguments
TypeclassName- Name of the typeclassMethodName- Name of the methodInstances- List of registered instancesState- Codegen state
Returns
{ok, DispatchFunction}- Generated dispatch function{error, Reason}- Generation error
-spec generate_instance_registration(atom(), atom(), [term()], term()) -> {ok, term(), term()} | {error, term()}.
Generates instance registration code for automatic registration on module load.
This creates an -on_load attribute and registration function that automatically registers the instance with cure_instance_registry when the module is loaded.
Arguments
TypeclassName- Name of the typeclass (e.g., 'Show', 'Eq')TypeName- Name of the type (e.g., 'Int', 'List')CompiledMethods- List of compiled method functionsState- Codegen state
Returns
- Registration code structure with on_load hook
-spec process_derive_clause(#derive_clause{typeclass :: term(), typeclasses :: term(), for_type :: term(), constraints :: term(), location :: term()}, #record_def{name :: term(), type_params :: term(), fields :: term(), derive_clause :: term(), location :: term()}, term()) -> {ok, [term()], term()} | {error, term()}.
Processes a derive clause and generates instances.
Called during record definition compilation to automatically generate typeclass instances using the derive mechanism.
Arguments
DeriveClause- Derive clause AST nodeRecordDef- Record definition being derived forState- Codegen state
Returns
{ok, GeneratedInstances, NewState}- List of generated instance functions{error, Reason}- Derivation error