Skip to content

Instantly share code, notes, and snippets.

@maxcai314
Last active May 4, 2025 14:42
Show Gist options
  • Select an option

  • Save maxcai314/092a8220a5a36f6faf6a22e55976fbe5 to your computer and use it in GitHub Desktop.

Select an option

Save maxcai314/092a8220a5a36f6faf6a22e55976fbe5 to your computer and use it in GitHub Desktop.
Object-Oriented Programming in Google Sheets
=LET(
BEGIN_LIB_OOP, "smalltalk-style message handler framework",
message_match, LAMBDA(expected, LAMBDA(actual,
IFERROR((actual = expected), FALSE)
)),
is_nil, LAMBDA(obj, IFERROR(obj("is_nil__"), FALSE)),
as_err_string, LAMBDA(message, fallback_message,
IFERROR(
CONCAT("", message),
IFERROR(
IF(is_nil(message),
"[nil]",
CONCAT(CONCAT("Object[ ", message("displayString")), " ]")
),
fallback_message
)
)
),
next_error_message, LAMBDA(original_err_message, message,
CONCAT(CONCAT(
as_err_string(original_err_message, "[Invalid Trace]"),
" | "),
as_err_string(message, "[Unknown Argument]")
)
),
DEFINE_CLASS_INVALID_METHOD_TRACE, "object that accumulates traces of invalid method calls",
make_invalid_method_trace_bootstrap, LAMBDA(f, LAMBDA(err_message,
LAMBDA(message,
IF(message_match("displayString")(message),
CONCAT(CONCAT("InvalidMethodTrace[ Object | ", as_err_string(err_message, "Undefined")), " ]"),
f(f)(next_error_message(err_message, message))
)
)
)),
make_invalid_method_trace, (make_invalid_method_trace_bootstrap)(make_invalid_method_trace_bootstrap),
DEFINE_CLASS_NIL_TARGET_TRACE, "object that accumulates traces of method invocations on nil",
make_nil_target_trace_bootstrap, LAMBDA(f, LAMBDA(err_message,
LAMBDA(message,
IF(message_match("displayString")(message),
CONCAT(CONCAT("NilTargetTrace[ [nil] | ", as_err_string(err_message, "Undefined")), " ]"),
f(f)(next_error_message(as_err_string(err_message, "[Invalid Trace]"), message))
)
)
)),
make_nil_target_trace, (make_nil_target_trace_bootstrap)(make_nil_target_trace_bootstrap),
handle_unknown_message, LAMBDA(message,
IF(message_match("is_nil__")(message),
FALSE,
IF(message_match("displayString")(message),
"object",
make_invalid_method_trace(message)
))
),
DEFINE_OBJ_NIL, "special object that overrides is_nil__ to true",
nil, LAMBDA(message,
IF(message_match("displayString")(message),
"nil",
IF(message_match("is_nil__")(message),
TRUE,
make_nil_target_trace(message)
))
),
DEFINE_CLASS_NUMBER, "object wrapper for builtin numbers",
make_number_bootstrap, LAMBDA(f, LAMBDA(raw,
LAMBDA(message,
IF(message_match("rawNumVal")(message),
raw,
IF(message_match("+")(message),
LAMBDA(rhs, f(f)(raw + rhs("rawNumVal"))),
IF(message_match("*")(message),
LAMBDA(rhs, f(f)(raw * rhs("rawNumVal"))),
IF(message_match("factorial")(message),
f(f)(FACT(raw)),
IF(message_match("log")(message),
f(f)(LOG(raw)),
IF(message_match("displayString")(message),
CONCAT("", raw),
handle_unknown_message(message)
))))))
)
)),
make_number, (make_number_bootstrap)(make_number_bootstrap),
END_LIB_OOP, "smalltalk-style message handler framework",
BEGIN_USER_CODE, "user code for this program cell",
DEFINE_CLASS_RECTANGLE, "rectangle with length and width",
rectangle_class, LAMBDA(this, LAMBDA(l, w,
LAMBDA(message,
IF(message_match("length")(message),
l,
IF(message_match("width")(message),
w,
IF(message_match("area")(message),
(this("length"))("*")(this("width")),
IF(message_match("name")(message),
"Rectangle",
IF(message_match("description")(message),
CONCATENATE(
this("name"),
" [length=", this("length")("displayString"),
", width=", this("width")("displayString"), "]",
" has area=", this("area")("displayString"),
" and aria=", this("aria")("displayString")
),
handle_unknown_message(message)
)))))
)
)),
new_rectangle_bootstrap, LAMBDA(f, LAMBDA(l, w, rectangle_class(LAMBDA(message, f(f)(l, w)(message))) (l, w))),
new_rectangle, (new_rectangle_bootstrap)(new_rectangle_bootstrap),
DEFINE_CLASS_SQUARE, "subclass with a single side length parameter",
square_class, LAMBDA(this, LAMBDA(s,
LAMBDA(message,
IF(message_match("name")(message),
"Square",
rectangle_class(this)(s, s)(message)
)
)
)),
new_square_bootstrap, LAMBDA(f, LAMBDA(s, square_class(LAMBDA(message, f(f)(s)(message))) (s))),
new_square, (new_square_bootstrap)(new_square_bootstrap),
rect_obj, new_rectangle(make_number(50), make_number(60)),
square_obj, new_square(make_number(36)),
new_line, CHAR(10),
RETURN_VALUE, "the output of this program cell",
CONCATENATE(
"Regarde! ",
square_obj("description"),
new_line,
"Regarde! ",
rect_obj("description")
)
)
@maxcai314
Copy link
Author

This demonstrates the inheritance structure of the Square/Rectangle hierarchy, where square extends and overrides the "name" method, which is still available to the original implementation of "description" due to dynamic dispatch with the this parameter.
This current cell above returns this text:

Regarde! Square [length=36, width=36] has area=1296 and aria=InvalidMethodTrace[ Object | aria ]
Regarde! Rectangle [length=50, width=60] has area=3000 and aria=InvalidMethodTrace[ Object | aria ]

More example return values

Error propagation of invalid method calls and invocations on "nil"

RETURN_VALUE, "the output of this program cell",
nil("cope")("invalidmethod")("+")((nil)("*")(nil))(make_rectangle(nil, nil)("nonExistentMethod"))("displayString")

which returns this trace:

NilTargetTrace[ [nil] | cope | invalidmethod | + | Object[ NilTargetTrace[ [nil] | * | [nil] ] ] | Object[ InvalidMethodTrace[ Object | nonExistentMethod ] ] ]

Basic number message passing

(examples from the Wikipedia page for Smalltalk)

RETURN_VALUE, "the output of this program cell",
make_number(3)("factorial")("factorial")("log")("displayString")

which returns 2.85733249643127, and

three, make_number(3),
four,  make_number(4),
five,  make_number(5),

RETURN_VALUE, "the output of this program cell",
(three)("+")((four)("*")(five))("displayString")

which returns 23

More information

For more information, see this blog post

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment