Type-checked non-empty strings (exploring-better-ways.bellroy.com)

39 points by surprisetalk 3 days ago

muglug 3 hours ago

Very cool that the language allows specification of a type in this way.

I added a similar type — “non-empty-string” to a typechecker for PHP, and it’s been adopted widely in the PHP ecosystem. It turns out to be pretty handy, especially when there’s a full type system to support it.

IshKebab 3 hours ago

"Huh never heard of Bellroy... I wonder what they're using Haskell for..."

Turns out it's some kind of bags and accessories brand!

flexagoon 2 hours ago

I've had a Bellroy bag, they're not the most fashionable but super high quality and well thought-out. Just like Haskell code—maybe that's why they like it.

umpalumpaaa 19 minutes ago

In the backpack community bellroy is often seen as “meh”. Eg. they frequently don’t live up to the guarantees they give and to the quality they promise. Also overpriced.

qbane 3 hours ago

I pondered for a while, it IS the company I used to know

ambicapter an hour ago

I once saw a job ad for a company selling a horoscope app that required Haskell. An unusual conjunction for sure.

saithound 23 minutes ago

Astrology and Haskell are quite similar in that both are much much easier to do if you have a math degree.

recursive 4 minutes ago

gib444 36 minutes ago

Using Haskell for a horoscope app is like hiring a mathematician to read tea leaves

saithound 9 minutes ago

ivanjermakov 3 hours ago

Language is not mentioned in a title, so my first thought was about TypeScript type wizardry. Turns out it's as simple as `Exclude<string, "">`.

https://www.typescriptlang.org/docs/handbook/utility-types.h...

Edit: nevermind, LLM fooled me.

antipurist 3 hours ago

It's simple, and it doesn't work as `Exclude` only applies to union types. For type `string` it just returns the same type `string`.

phpnode 3 hours ago

yup, it's not possible to do it safely with a simple unparameterised type: https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwK...

ralferoo 3 hours ago

It is very much mentioned in the article title and the first sentence. It's just HN that's truncated the title.

Cthulhu_ 3 hours ago

Speaking of TS, there's stuff in there for typing strings / string formats: https://www.typescriptlang.org/docs/handbook/2/template-lite...

nvme0n1p1 3 hours ago

Daily reminder that TypeScript's type checker is not sound.

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwK...

blue_pants 3 minutes ago

It is true, but it wasn't meant to be sound, so it's okay.

You can do this trick for type-checking emptiness of string literals

https://www.typescriptlang.org/play/?jsx=0#code/C4TwDgpgBAsg...

IceDane 41 minutes ago

This example is not only wrong for what you intend to demonstrate but even if it wasn't, it's not problematic. In typescript the proper way to do this is using branded types and exporting only the safe constructor, making anyone who wants to violate the invariant go out of their way, which is no different from the situation in any number of programming languages or scenarios.

  declare const brand: unique symbol;
  type NonEmptyString = string & { readonly [brand]: 'NonEmptyString' };

  // the ONLY non-cast way to produce one
  export function nonEmptyString(s: string): NonEmptyString | undefined {
    return s.length > 0 ? (s as NonEmptyString) : undefined;
  }

  export type { NonEmptyString };