7 ms·
Types as Interfaces
- brabel 2y agoIs this something professional programmers are having trouble with?? This is the kind of problem you face in your first year working, no? I am honetly curious what others think. Do you have trouble deciding when to use an interface (assuming your language has that), or a type wrapper (I don't think that's the brightest idea), or a function to extract the field to sort by (most languages do that)??
- Sakos 2y ago> Is this something professional programmers are having trouble with?? Nobody said this. There is value in thinking about things like this sometimes, because it has long-term consequences for the projects we work on. Even if you're a "professional" programmer (whatever that means), it's valuable to go back to beliefs and knowledge you've established long ago to evaluate whether to change them in the face of new experiences you've made since the last time you've thought about it. If you think "professional" programmers don't get this sort of thing wrong in some form or another, I have a bridge to sell you.
- brabel 2y agoWhy scare quotes though? Professional programmer just means someone who writes software for a living. Software is your profession... there's no doubt what that means , is there?? If you do that, you'll have run into this sort of decision very early in your career, and hopefully will understand the best way to handle it, which IMHO just depends on the language (because certain features lead to different optimum solutions) and the culture around it. But sure, I am happy to discuss fundamental topics like this, that's why I am engaging and asking what others think.
- yccs27 2y agoMost professional programmers don't worry much about elegance and just write code that gets the job done.
- sickblastoise 2y agoSometimes I do, not everyone is a big brain coder.
- js8 2y ago> Is this something professional programmers are having trouble with?? I would say, yes. So does everybody else. If you don't believe it, I don't think you appreciate the depth of these problems. I am reminded of Eric Meijer saying that the interface (in the Java sense) part of the contract between types is the least interesting part. What is important are their algebraic properties, which can be modeled using functional types and other bunch of advanced type-theoretical ideas (like linear types for instance). Modelling stuff with types is not easy, it's an art.
- jddj 2y agoYour confidence that there is an obviously optimal way to model something reads as either brilliance or hubris and brilliance is a lot rarer. Typically there are subtle trade-offs and compromises which only prove themselves to be useful/detrimental as the software changes over time. You can place bets based on experience but you can only really be cocky about your choices when looking back not looking forward
- cxr 2y ago> Your confidence that there is an obviously optimal way to model something You're imagining things (uncharitably); that's not in the comment you replied to.
- jddj 2y agoYou might be right you know. I can't edit anymore but please mentally strike out that first paragraph. I did read a lot into the '??' punctuation which might not have been intended.
- dgb23 2y agoIt’s useful to examine fundamentals now and then.
- posix_monad 2y agoMLs require a lot of ceremony modelling simple record types. What we want to express here is an object with a map of properties (name to type): string Type map For the OOP minded: Map<string, Type> And also compose those: type Foo = { "_foo", int } type Bar = { "_bar", string } type FooBar = mergeMaps Foo Bar But at compile-time, of course. Have any languages achieved this? I know TypeScript can do some of these things, but it's clunky.
- mamcx 2y agoI raise the bar and say the relational model has it and can be made to work at type level. type Person = User DESELECT password type Invoice = Customer JOIN Inv
- c-hendricks 2y agoSomething like this in TypeScript: type Person = Omit<User, 'password'> type Invoice = Customer & { invoice: Inv } // or type Invoice = Inv & { customer: Customer }
- ivanbakel 2y agoThis is often referred to as "row typing", and the only language I've ever used that implemented it first-class with polymorphism is Purescript[0]. It's possible to model such things in other languages with sufficiently powerful type systems, though they're normally not as nice to use. As an aside, Purescript is one of the most fun languages I've ever used for frontend work, and I lament that Elm seems to have overtaken it in the FP community. [0]: https://hgiasac.github.io/posts/2018-11-18-Record-Row-Type-and-Row-Polymorphism.html https://hgiasac.github.io/posts/2018-11-18-Record-Row-Type-a...
- jiehong 2y agoEven if Elm has had no new version in 4 years while purescript has? You made me want to try purescript :)
- tsss 2y ago
- sickblastoise 2y agoI would love to just throw a field Optional[Datetime.UTC] on Msg type and call it a day :). Cool article got me thinking at 6:00 AM
- jolux 2y agopublished…2024-09-17?
- croemer 2y agoNice catch!
- gasgiant 2y agoI wonder what else kqr can tell us about the future.
- AnimalMuppet 2y agoSounds like someone needed a type that expresses "date no later than now".
- tinix 2y agotypes are not interfaces. interfaces describe behavior, types describe shape and structure. the difference is subtle but important.
- Iceland_jack 2y agoIn Haskell behavour is type-directed (type classes) and they describe both.
- BugsJustFindMe 2y ago> interfaces describe behavior, types describe shape and structure Shape and structure are behavior. There's more to behavior than "abstractly produces X result". Behavior is "produces X result in Y form given Z in W form".
- CyberDildonics 2y agoShape and structure are behavior. They shouldn't be. Conflating two things that can be separated is just compounding complexity. You don't need to know how a database lays out memory or the structure behind a web server.
- BugsJustFindMe 2y ago> You don't need to know how a database lays out memory You do, however, need to know what logical columns are in a table and the types of those columns to be able to effectively query against the table. And you need to know what the types of the inputs to a query wrapper function are to be able to call it properly. Memory layout has nothing to do with type, because physical memory layout is completely separate from the semantic logical layout and form represented by the bits. You now appear to be the one conflating two unrelated things. That something is treated as an integer fundamentally matters to its use. That the thing comprises some number of adjacent bits in big/little-endian arrangement is a very unrelated implementation detail.
- CyberDildonics 2y ago
- auggierose 2y agoIndeed, TypeScript can do exactly this and more, without much ceremony: type FooBar = Foo & Bar I doubt you will find a language where it is less clunky. Edit: Oh, I typed this on mobile, this was supposed to be a comment on another comment by posix_monad.
- valenterry 2y agoTypescript has a phenomenal type-system indeed. However, its foundations are not very sound. The types are pragmatic and that's what counts mostly, but going forward it would be great to have type-systems that are even more powerful and also theoretically sound with a simpler foundation (= less edge cases and workarounds). Languages like Idris are working on this. Typescript can't really be this language because it is impeded by having to work with the javascript runtime, which makes this task much much harder to do.
- auggierose 2y agoTypeScript's type foundations are not sound, and I would argue that this is a major factor why it is so good. I have yet to see a sound type system that is as pleasant to use as TypeScript's—certainly not Idris. I am not a fan of encoding all sorts of correctness statements into a static type system. I'd much rather use a theorem proving environment for correctness, but one that is not based on type theory.
- agentultra 2y agoWhat do you gain from an unsound type system? Implicit casts still exist and one dependency returning Any means that your specification is meaningless (you can prove anything, including false).
- valenterry 2y agoThe point is that making a typesystem sound is really hard and requires trade-offs. I think Scala is a wonderful example. Check: https://www.baeldung.com/scala/dotty-scala-3 https://www.baeldung.com/scala/dotty-scala-3 To quote from there: > Scala 3 has dropped some unsound and useless features to make the language smaller and more regular. It has added some new constructs to increase its expressiveness. Also, it has changed some constructs to remove warts and increase simplicity, consistency, and usability. Some of the features they dropped because they were unsound were still useful (to me). Typescript tries neither to make their typesystem (perfectly) sound, nor to make it as elegant as possible. That results in it to be very useful/pragmatic for everyday-programming tasks.
- user3939382 2y agoI’ve come to believe that a type should be capable of reflecting any arbitrary rules a programmer knows about the bounds of a value. The only real implementations of this I see are mostly academic with a dependent type system like Idris. We’re many years from that becoming mainstream, if it ever will be.
- valenterry 2y agoSadly this is true and I 100% agree. After reading and working through type-level programmin in Idris, this is the conclusion I came to.
- pmarreck 2y agoWhat made it seem too restrictive to you? Or what were your experiences trying to apply it to real-world scenarios?
- valenterry 2y agoI'm not sure what you mean. I don't find Idris restrictive (except for the lack of union types with subtyping). Rather I find any mainstream language very restrictive - or is that what you meant?
- pmarreck 2y agoah, I think I misunderstood. Still getting through the Idris book :)
- pydry 2y agoI think there's a sharp rule of diminishing returns the stronger you make type systems. Type safety comes at a cost and that cost has to come with an associated payoff. This is why ultra strong type systems end up being little more than academic toys - the payoff when you dial type safety up to an extreme doesn't match the associated cost. Types and tests should meet somewhere in the middle because the same law of diminishing returns affects tests in reverse. They tend to be good at what the other is bad at and if you rely too heavily on one at the expense of the other they will start fraying around the edges.
- indulona 2y agoproblem of known types, or interfaces, as values is that conditions for the value format or validation changes based on context. so you might have a price that is valid in one context but not in another(like a negative value). the problem then occurs on the input where you might let in value that is valid on higher level but not on deeper level and by the time you detect it it might be too late(you have committed a transaction with invalid data).
- Xen9 2y agoDifferences in foundational understanding & unteachability of foundational research causes inevitably ton of confusion in texts on formal science ideas outside a known academic context. This text should not fascinate a programmer but create frustration of two types: (1) Frustration on one's lack of small pieces knowledge (2) Frustration that NOW you will not have the chance to invent this on your ow; your creative process takes damage. Out of which the second type should be the one that makes majority of cases. ALSO this should cause one to have respect of form: "hey, this programmer was probably at least smart enough to figure out this alone." Perhaps most polite woule be to, for every text, in the situation to have notice at beginning: "For programmers who already have thought about what would happen were you to add X to Y but so that Z enough to probably not to get new ideas in this context."
- dingosity 2y agomeh. or you could use mixins.
- spankalee 2y agoThis is a lot of complication for in what most OOP languages with interfaces would simply be something like: interface Timestamped { timestamp: UTCTime; } interface Msg { sender: PlayerId; } class Quote implements Timestamped, Msg { timestamp: UTCTime; sender: PlayerId; } Why is this so hard in Haskell? It doesn't have interface polymorphism?
- keybored 2y agoI think TFA is talking about nesting fields in whatever order and also being able to extract the fields without ceremony like `get(get(get...))`—just allow `get(...)`. If you wanted an example more close to the submission you would have a multi-field class inside that class. Then you would perhaps solve the nested field access boilerplate by manually implementing it in the interface methods.
- valcron1000 2y agoIt's not hard at all and it's actually what the second approach listed by the author shows: class HasRecipient a where get_receiver :: a -> PlayerId which adjusted to your example would be class Timestamped a where timestamp :: a -> UTCTime The problem with this approach is that you'll have duplicated data on all instances. In your example, `Quote` has the fields `timestamp` and `sender` in order to satisfy with `Timestamped` and `Msg`. If you had several classes like `Quote` and interfaces like `Timestamped` then you would end up with a lot of duplicated code.
- pierrebai 2y agoSome of the ideas in the blog must be speculative, given that it fell through a wormhole, being published September 17, 2024.
- fellowniusmonk 2y agoPrimitive types and schemas. Complex types and objects don't exist. Embrace Mereological Nihilism. It's fun at meetups to tell everyone your programming paradigm is Nihilism.
- platz 2y agoWhat is fundamental, types of bytes?
- fellowniusmonk 2y agoYeah, very roughly speaking. In the philosophic position of mereological nihilism only "simples" exist when discussing objects, etc, simples are akin to byte types for sure and primitive types maybe. Nothing complex, like objects, higher level types, etc. exist. Composition of simples in time and space is what determines everything other than simples. Anything that isn't a simple is just emergent from the space/time arrangement of the simples. So if the implications are that nothing is real outside of time/space/simples, then calling something higher level a "type" is to label something as discrete and real when it isn't, so now you are using language and models that are wrong to reason about things, which means your model will have frustrating drift from reality that you can't rectify. That's where schemas come in, we are just labeling common arrangements of simples for semantic reasons but they don't really exist so they shouldn't be elevated to the position of a type as that is a wrong abstraction and causes divergence and mess.
- fungiblecog 2y agoRich Hickey would say "just use maps" and avoid all this navel-gazing
- valcron1000 2y agoIt does not solve the problem. Would you rather have a function that takes a map and puts additional keys in it (ex. `timestamp`) or a function that takes a map and stores it in a new map alongside the additional keys? In any case, how would you write a generic function that works on those additional keys?
- LudwigNagasena 2y ago> People say that types-as-interfaces are not composable Who says that and what does it even suppose to mean?
- deleted 2y ago[deleted]
- ksoajl 2y ago[dead]