Skip to content

Instantly share code, notes, and snippets.

Prefer Sum Types Over Strings and Booleans

Design domain types to make illegal states unrepresentable. Use enums/variants (sum types) instead of strings or booleans when a value has a fixed set of meaningful states.

Strings Allow Invalid Values

// ❌ WRONG: Any string is accepted, typos compile fine
struct Post {
    status: String, // "draft", "published", "archived"... or "publsihed"?
}
@alex35mil
alex35mil / hlc.rs
Created January 15, 2022 13:30
Hybrid Logical Clocks implementation in Rust
// Hybrid Logical Clocks implementation in Rust.
// Based on:
// - https://cse.buffalo.edu/tech-reports/2014-04.pdf
// - https://github.com/jlongster/crdt-example-app/blob/master/shared/timestamp.js
// - https://github.com/jaredly/hybrid-logical-clocks-example/blob/master/src/hlc.js
use std::{
char, cmp, fmt, num, ops, str,
time::{Duration, SystemTime, UNIX_EPOCH},
};

Promise.res

A slightly modified version of standard ReScript's Js.Promise module.

Key differences are:

  • t-first api
  • contains few helpers like Promise.result and Promise.wait (which is what I want most of the time in apps)

Soundness is on the same level as in the original bindings.

If you want more safety, consider reason-promise.

  1. Install these packages:
yarn install --dev @babel/core @babel/preset-env jest babel-jest
  1. Add babel.config.js (exactly babel.config.js, not .babelrc, not .babelrc.json etc):
module.exports = {
type transitionHook = Dom.htmlElement => unit;
module type Css = {
let duration: int;
let appear: string;
let enter: string;
let exit: string;
let appearActive: string;
let enterActive: string;
let exitActive: string;
open Belt;
type t(+'a) = Js.Promise.t('a);
type error = Js.Promise.error;
type result('ok, 'error) = t(Result.t('ok, 'error));
[@bs.new]
external make:
(
open Belt;
type t(+'a) = Js.Promise.t('a);
type error = Js.Promise.error;
type result('ok, 'error) = t(Result.t('ok, 'error));
external errorToString: error => string = "%identity";
[@bs.new]
@alex35mil
alex35mil / App.re
Last active May 24, 2020 13:55
FCM
<TextField
icon=(module IconOk)
value
onChange=change
/>
module Modifier = {
type t =
| Date(Js.Date.t)
| Range(Js.Date.t, Js.Date.t)
| Fn(Js.Date.t => bool);
let toJs =
(. x) =>
switch (x) {
| Date(date) => date