Live data from Hacker News

Use Your Type System

dzombak.com

271–280 of 357 posts

Re: Use Your Type System

#271

Type systems, like any other tool in the toolbox, have an 80/20 rule associated with them. It is quite easy to overdo types and make working with a library extremely burdensome for little to no to negative benefit. I know what a UUID (or a String) is. I don't know what an AccountID, UserID, etc. is. Now I need to know what those are (and how to make them, etc. as well) to use your software. Maybe an elaborate type sy…

> I know what a UUID (or a String) is. I now know I never know whenever "a UUID" is stored or represented as a GUIDv1 or a UUIDv4/UUIDv7. I know it's supposed to be "just 128 bits", but somehow, I had a bunch of issues running old Java servlets+old Java persistence+old MS SQL stack that insisted, when "converting" between java.util.UUID to MS SQL Transact-SQL uniqueidentifier, every now and then, that it would be "sm…

UUIDs all have the same storage+representation.

They are generated with different algorithms, if you find these distinctions to be semantically useful to operations, carry that distinction into the type.

Seems like 98% of the time it wouldn’t matter.

Re: Use Your Type System

#272
post #264
post #262

This is also an incredibly useful technique with LLMs. If you alias types (e.g. str to DateStr) the LLM can better infer which functions to select and how to compose them

How would you do this? Don't you have to define your types using JSON schema which supports only a limited set?

For pure LLM use, not MCPs.

Re: Use Your Type System

#273
post #227

Earlier quoted context omitted.

in raku, that’s spelled subset OneToTen of Int where 1..10:

Off topic: do you use raku in day to day life? I tried learning it but perl5 remains my go-to when I just need to whip something up

yes … I find it as useful as I used to find perl (spent 4 years as a perl coder back in the day) and use for ad hoc scripts … data migration, PDF/CSV scraping, some LLM prompt engineering in a business setting, wrote a module to manage/install Wordpress on EC2 for example

  raws-ec2 —eip —nsu launch
  raws-ec2 —q connect | pbcopy
  
https://raku.land/zef:librasteve/CLI::AWS::EC2-Simple

mainly I find Raku (and the community) much -Ofun

Re: Use Your Type System

#274
post #80

Earlier quoted context omitted.

Have you used this in production? It seems appealing but seems so anti-thetical to the common sorts of engineering cultures I've seen where this sort of rigorous thinking does not exactly abound.

Source generators hide too many details from the user. I prefer to have the generated code to be the part of the code repo. That's why I use code templates instead of source generators. But a properly constructed ID type has a non-trivial amount of code: https://github.com/vborovikov/pwsh/blob/main/Templates/ItemT...

> a properly constructed ID type has a non-trivial amount of code

That is correct, I've looked at the generated code and it's non-trivial, especially when validation, serialisation and casting concerns are present. And when you have multiple id types, and the allowed casts can change over time (i.e. lock it down when the migration is complete)

That's why I'd want it to be common, tested code.

Re: Use Your Type System

#275

Earlier quoted context omitted.

It can be annoying though. I think Rich Hickey has a point that bugs like this almost certain get caught by running the program. If they make it into production it usually results in an obscure edge case. I’m sure there are exceptions but unless you’re designing for the worst case (safety critical etc) rather than average case (web app), types come with a lot of trade offs. I’ve been on the fence about types for a lo…

An engineer getting up to speed on a 10 year old web app that uses dynamic types will likely have a very different opinion. No types anywhere, so making a change is SCARY! And all the original engineers have usually moved on. Fun times. Types are a form of forced documentation after all, and help catch an entire class of bugs. If you’re really lucky, the project has good unit tests. I think dynamic typing is wonderfu…

I think you’re right.

But most startups aren’t building for 10 years out. If you use a lot of typing, you’ll probably die way before then. But yeah if you’re building a code base for the long term then use types unless you’re disciplined enough to write comments and good code.

As for refactoring, that is exactly what test suites are for.

Re: Use Your Type System

#277

Earlier quoted context omitted.

It can be annoying though. I think Rich Hickey has a point that bugs like this almost certain get caught by running the program. If they make it into production it usually results in an obscure edge case. I’m sure there are exceptions but unless you’re designing for the worst case (safety critical etc) rather than average case (web app), types come with a lot of trade offs. I’ve been on the fence about types for a lo…

> I think Rich Hickey has a point that bugs like this almost certain get caught by running the program. That is certainly correct... but that doesn't make it a good thing. One wants to catch bugs before the program is running, not after.

Depends which is quicker. If I catch a trivial bug on the first run, I just saved myself writing the type system to find it ahead of time.

Re: Use Your Type System

#278
post #161

Earlier quoted context omitted.

There are libraries for that, such as Vogen https://github.com/SteveDunn/Vogen The name means "Value Object Generator" as it uses Source generation to generate the "Value object" types. That readme has links to similar libraries and further reading.

This seems like overkill. I’d prefer the few lines of code above to a whole library.

Is it "overkill" if it's already written and tested?

Once you have several of these types, and they have validation and other concerns then the cost-benefit might flip.

FYI, In modern c#, you could try using "readonly record struct" in order to get lots of equality and other concerns generated for you. It's like a "whole library" but it's a compiler feature.

Re: Use Your Type System

#279

Shoutout to the absolutely awesome beartype for python. With simple decorators it adds runtime type checking with virtually no performance cost!! https://beartype.readthedocs.io/en/latest/ Or see their page about performance: https://beartype.readthedocs.io/en/latest/faq/#faq-realtime

you never got a TypeError before? Python already has runtime type checking

Re: Use Your Type System

#280
post #196

Earlier quoted context omitted.

It has to work that way or else you can't use the standard library. What you want to block is not: StringUtils.trim(String foo); but myApp.doSomething(AnotherMyType amt); The latter is saying "I need not any string but a specific kind of string".

No I want to block both. I don't want to give devs the option of creating a function doSomething(String) that happens to accept MyType. If I need to call trim then I'll do StringUtils.trim(MyType.toString());

[dead]
Post reply on HN