News for hackers. Stuff that matters.
Re:Names are not type safety (2020) by b_e_n_t_o_n on Monday August 04, 2025 @12:34AM (#44782149)
Yeah you bring up a good point. A { name: string } dict needs to be treated differently from a { user_pw: string } dict. The difference is that happens in the domain layer instead of the type layer.
Re:Names are not type safety (2020) by SkiFire13 on Monday August 04, 2025 @01:23AM (#44782342)
That's no difference than using newtype structs. If you remove the extra layer you are left with `string` for both of them.

> The difference is that happens in the domain layer instead of the type layer

This view greatly reduces the usefulness of the type layer though, as that's the only automated tool that can help the domain layer with handling cases like this.

Re:Names are not type safety (2020) by b_e_n_t_o_n on Monday August 04, 2025 @01:41AM (#44782448)
It's not really automated though, it's just another layer of code written by a human, prone to the same types of human error.
Re:Names are not type safety (2020) by lmm on Tuesday August 05, 2025 @12:21AM (#44794310)
Bugs in the typechecker are rare (it's widely exercised if the language is at all popular) and generally fixed quickly. If you have an expression of type A you can be pretty confident you're getting a value that's passed through a constructor for type A.

Can a human encode something different by that than what they intended to encode? Certainly. But it's got the highest cost-benefit of any approach to double-checking your code I've found.

Re:Names are not type safety (2020) by b_e_n_t_o_n on Tuesday August 05, 2025 @06:02AM (#44796201)
That's not what I mean. It's trivial to encode your types in such a way that it incorrectly implements domain logic and there is no meta-meta language enforcing correctness there.
Re:Names are not type safety (2020) by lmm on Tuesday August 05, 2025 @08:41PM (#44806336)
How so? To the extent that you use your types with your code, getting your types wrong will lead to either type errors in correct code (in which case you notice and fix them) or overly loose types that will allow incorrect code to pass (in which case you don't get an actual bug unless you make a corresponding mistake in your code).
Re:Names are not type safety (2020) by valenterry on Monday August 04, 2025 @01:36AM (#44782416)
> The difference is that happens in the domain layer instead of the type layer.

What's those layers you are talking about? In my domain-logic code I use types of course so there is no dedicated "type layer".

Re:Names are not type safety (2020) by RossBencina on Monday August 04, 2025 @02:07AM (#44782545)
Depending on the language, you can define a PasswordString type that is entirely distinct from the string type. Perhaps it can be explicitly converted to a string (perhaps given a capability token). Then you have:

a { user_pw: PasswordString}

This is what it means to model the domain using types. It is not a separate layer, it is actually using the type system to model domain entities.

Re:Names are not type safety (2020) by b_e_n_t_o_n on Monday August 04, 2025 @02:34AM (#44782683)
Structurally typed languages can still support information hiding.

   type PasswordString struct {
      pw string
   }
   func(p PasswordString) String(token string) { ... }
I guess the point is that you can model your domain using data as well as types.
Re:Names are not type safety (2020) by lmm on Monday August 04, 2025 @03:00AM (#44782806)
> Structurally typed languages can still support information hiding.

But you haven't hidden the information, it's still a string. You can put the string in a wrapper struct but in a structural system that's not really any different from putting it a list or map - the data is still exposed, and if someone writes code to e.g. log objects by just enumerating all their fields (which is a very natural thing to do in those systems) then it will naturally print out your password and there's not really any way to make it ever not.

> I guess the point is that you can model your domain using data as well as types.

You want both in your toolbox though. Restricting yourself to only having types that are essentially lists, maps, or a handful of primitives has most of the same downsides as restricting yourself to not using types at all.

Re:Names are not type safety (2020) by b_e_n_t_o_n on Monday August 04, 2025 @03:16AM (#44782878)
You can implement the Stringer interface for the type which prevents it from being logged and since it's private, code from outside of the module can't enumerate it. Of course it's still accessible via reflection or memory dumps etc, but isn't that the case with Java etc? Storing a plain text password like this is a bad idea anyways.

I guess my point is that a structural type system can still allow for encapsulation.

Re:Names are not type safety (2020) by lmm on Monday August 04, 2025 @04:02AM (#44783139)
> You can implement the Stringer interface for the type which prevents it from being logged and since it's private, code from outside of the module can't enumerate it.

Those sound like decidedly non-structural features. And couldn't you undermine them by passing it to a function that expects a different `struct { pw string }` and logs its contents?

Re:Names are not type safety (2020) by b_e_n_t_o_n on Tuesday August 05, 2025 @06:03AM (#44796211)
Only in the same module. Outside, it's effectively hidden.

And yeah, structurally typed languages often have nominal features. They come in useful in a lot of scenarios! Unless you're talking about something like Clojure which is not statically typed.