Skip to content

Refactor: Standardize method checking pattern across container types #145

Description

@clank-hayen

Problem

The typechecker uses similar but not identical patterns for checking methods on different container types:

  1. check_list_method in `check_list.cpp`
  2. check_pair_method in `check_pair.cpp`
  3. check_tuple_method in `check_tuple.cpp`
  4. check_channel_method in `check_channel.cpp`
  5. check_str_method in `check_method_call.cpp`

Each has its own way of looking up method info:

// List uses:
auto method_info = bishop::get_list_method_info(mcall.method_name);

// Str uses:
auto method_info = bishop::get_str_method_info(mcall.method_name);

The argument checking logic is then repeated in each.

Suggested Solution

Create a unified generic method checking helper:

// In a new file: typechecker/check_builtin_method.hpp

struct BuiltinMethodInfo {
    vector<string> param_types;  // "T" means element type
    string return_type;          // "T" means element type  
    bool is_optional_return = false;
};

TypeInfo check_builtin_method(
    TypeCheckerState& state,
    const MethodCall& mcall,
    const string& type_name,           // e.g., "List", "Pair"
    const string& element_type,         // e.g., "int", "str"
    const BuiltinMethodInfo& method_info
);

Then each container uses this shared implementation:

TypeInfo check_list_method(...) {
    auto info = get_list_method_info(mcall.method_name);
    if (!info) { error(...); return unknown; }
    return check_builtin_method(state, mcall, "List", element_type, *info);
}

Impact

  • Reduces duplication across 5 files
  • Consistent error messages
  • Easier to add new container types

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions