Skip to content

Instantly share code, notes, and snippets.

Turning programmer errors into type errors

An example problem

In Wordy, suppose that BinaryExpressionNode were designed like this, with the operator modeled as a string:

public class BinaryExpressionNode extends ExpressionNode {
    // Valid operator types include "addition", "subtraction", "multiplication",
 // "division", and "exponentiation". Other strings are not valid operators.
@pcantrell
pcantrell / metaprogramming.rb
Last active September 24, 2021 23:17
An example of metaprogramming in Ruby
# ------ Base class with metaprogramming ------
# This might make more sense if you skip ahead to the 🦄🦄🦄🌈🌈🌈 first
# and study the desired results, then come back here.
class Animal
def initialize(name)
@name = name
end
# How can we harmonize the types of a Square class and a Rectangle class
# so that it's possible to write a rescale function that works on both?
#
# As we saw in class Wed, if we’re doing this in Java, inheritance cannot
# solve this problem in a simple way: both `Square extends Rectangle`
# and `Rectangle extends Square` end up violating the Liskov Substitution
# Principle. While there is a nice “is-a” relationship here — all squares
# are rectangles! — that does not translate nicely into Java’s type system.
#
# In class today, we saw that having one inherit from the other doesn’t
@pcantrell
pcantrell / mastodon-macos.css
Last active December 8, 2023 20:35
Stylesheet to make Mastodon more macOS-like
body.app-body.layout-multiple-columns.theme-mastodon-light,
body.app-body.layout-multiple-columns.theme-default,
body.app-body.layout-multiple-columns.theme-contrast {
overflow: hidden !important;
}
body.app-body.layout-multiple-columns.theme-mastodon-light .columns-area,
body.app-body.layout-multiple-columns.theme-default .columns-area,
body.app-body.layout-multiple-columns.theme-contrast .columns-area {
margin: 0 -1px !important;

Map and HashMap

Learning objectives: