5 ms·
Type-checked non-empty strings
- ivanjermakov 4mo agoLanguage 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.html#excludeuniontype-excludedmembers https://www.typescriptlang.org/docs/handbook/utility-types.h... Edit: nevermind, LLM fooled me.
- ralferoo 4mo agoIt is very much mentioned in the article title and the first sentence. It's just HN that's truncated the title.
- antipurist 4mo agoIt'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 4mo agoyup, it's not possible to do it safely with a simple unparameterised type: https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwKIFsygMrAE4EsEDmAPACpQQAewECAJgM5T074EB8UAvEy4eVTQZQyAfigIIANwjYoALmH9qdRgCJVUMROmyFJANwBYAFAmAZgFcEAY2C5EUAIb16M4PGRpMvYmUrKhZjxCNgAKfDALYAUPVHQQLGDfNgBKBWdXbGBGCKioXEZYrwSfUg4AbxMTAF8q4wy3IvjE1lDVOABrVRSTBqym7yS27t6XRsQ4wdb1J0Yg1h7TY2tEZigzODgFeb5udTq+9wnilsJQjbhFkxWENYAjR1k91QOx-uPmn1CH7Cvl1eAUAAjhYKFwoO0uq9MkdPJ8hiCKH8gA https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwK...
- Cthulhu_ 4mo agoSpeaking of TS, there's stuff in there for typing strings / string formats: https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html https://www.typescriptlang.org/docs/handbook/2/template-lite...
- nvme0n1p1 4mo agoDaily reminder that TypeScript's type checker is not sound. https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwKIFsygMrAE4EsEDmUAvFEgB4DGANgK4AmEAPAM474EA0UA5DwHwBuAFDDKiNlDhwwLErx4jxCSQkSp0IAFyx1aTO0LzpspRLjUIAOmpwCACjXJ9IGxELAAFgEpBQA https://www.typescriptlang.org/play/?#code/C4TwDgpgBAcg9gOwK...
- IceDane 4mo agoThis 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 };
- blue_pants 4mo agoIt 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/C4TwDgpgBAsgrgZ2AOQPbAEIQKIFsygA8AKlBAB7AQB2AJglEgE4CW1A5gHxQC8jwrDmUo16UYgFgAUFCgB+KNQgA3CE2myAXOOFU6DAOQH5ilWqjbiAbmnSAxqmpJFqPARABlAW3a8oJXVEGZh9OAApmbXgkNEwcfCJiTgBKXm5mGylbKQcnYCgmCAQARj9qVwTPbw4wgwAzVFQDZMyAelbZToA9OWzc50KEACYyivcvQXZa5raOzqgevsdncuoAGRYqJgBDABsJnz8wsNSebiNkk8z+-MGAZlG3UAOa1Y2tvZf2Ful2+YXelIgA https://www.typescriptlang.org/play/?jsx=0#code/C4TwDgpgBAsg...
- recursive 4mo agoDaily reminder that the unsoundness in Typescript's type checker is a practical compromise applied to a language that was never designed to be type checked in this way, and furthermore that many developers feel that it strikes a good balance between formal correctness and practical usability.
- IshKebab 4mo 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!
- qbane 4mo agoI pondered for a while, it IS the company I used to know
- flexagoon 4mo agoI'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 4mo agoIn 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.
- IshKebab 4mo agoWhat brands does the backpack community like the most?
- ambicapter 4mo agoI once saw a job ad for a company selling a horoscope app that required Haskell. An unusual conjunction for sure.
- gib444 4mo agoUsing Haskell for a horoscope app is like hiring a mathematician to read tea leaves
- saithound 4mo agoAstrology is a mixture of factual verifiable information (such as apparent positions of celestial bodies at the time and location a certain person was born) and random baseless divinations. The "whale" users who account for a disproportionately large percentage of an astrologer's revenue tend to know the factual information surrounding their birth fairly well. An app/astrologer who doesn't get these facts right, even for a handful of clients, will get a bad reputation fairly quickly. I reckon the same principle would hold in cultural bubbles where reading tea leaves is a customary means of divination. If the client recognizes recognize black tea, but the fortuneteller insists it is rooibos, there won't be much trust in the rest of the prophecy. Advertising that the horoscope shop uses Haskell is actually a solid business idea. It pre-filters for the sort of dev who will be able to do the math.
- swordlucky666 4mo ago[flagged]
- muglug 4mo agoVery 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.
- gwerbin 4mo ago> Very cool that the language allows specification of a type in this way. Check out Idris 2. Dependent types can get very hairy to work with, but that language is an absolute playground for inductive type-level verification. And the standard library is like a nice Haskell standard library to me in a lot of ways, although the language itself is (unsurprisingly) not nearly as "production ready" as Haskell.
- VMG 4mo agoEmpty strings are usually an artifact of lazy developers paying a minimal "empty" value for a type (just as 0 for numbers). A type like NonEmptyString is a weak defense against that, as a lazy dev can just pass a single space character or something similar.