6 ms·
> The main benefit of classes is as a way to define custom types. Otherwise you are stuck with the types built into the language I'm going to stop you right th
by chmod775 7y ago
> The main benefit of classes is as a way to define custom types. Otherwise you are stuck with the types built into the language
I'm going to stop you right there, because plenty of languages (especially functional ones) have ways of declaring custom complex types without using classes.
- INTPnerd 7y agoWhich languages would you say are really good for defining your own types? Would they be a good fit for the example I provided where a function needs to accept integers larger than zero? Do they also allow you to define your own operations on those types? That is the part where classes seem to be a good fit, methods are basically operations supported by a type.
- damnyou 7y agoSum types and typeclasses in Haskell (or enums and traits in Rust) are a much better fit than classes and the mess that is operator overloading.
- INTPnerd 7y agoThanks! These definitely sound like what I should explore next in my journey.
- PhaseLockk 7y agoA typical way to handle this in a functional language would be to create a datatype with a name like "PositiveInt", which just contains an Int inside it. However, in a language like OCaml, you can make it so that users of this type cannot directly create it, and instead must use a function like "makePosInt", which would check that its argument is positive, then give you back a value of type PositiveInt containing your data. I'm not too experienced with this though, so this is pretty much the extent of my knowledge on this topic.
- nicoburns 7y agoRust is excellent for this. You can define: struct PositiveInteger(u32); Then give it a constructor (which in rust is just a regular static function) that checks the non-zero variant. You can define method on this type, and make it implement traits (which are kind of like interfaces). The best bit: there is zero runtime cost to this, the memory-representation of this type is identical to that of the underlying u32. Rust-style enums which can contain data, and also have method implemented on them are even better. Doesn't mean classes (structs in Rust) aren't useful, but once you use a language that allows you to define other kinds of custom types, they seem very restrictive when they're the only available tool.
- dhimes 7y agoThat sounds like a class.
- paulmd 7y agoYep. This whole discussion just seems like a C vs C++ styleguide slapfight. Having the functions that operate on the struct attached directly to the struct declaration, vs having some functions that the first parameter is the struct on which the function operates, doesn't seem like a particularly meaningful distinction to me. OK, you like C-style programming in favor of C++-style programming, congrats. It's still a class either way.
- deleted 7y ago[deleted]
- damnyou 7y agoThe distinction you describe is not meaningful, but the key feature that separates classes from other forms of code organization/polymorphism, like typeclasses as in Haskell/Rust, is not that. It is inheritance.
- naasking 7y agoData types are not classes.
- telekid 7y agoI believe you're looking for dependent types: https://en.wikipedia.org/wiki/Dependent_type https://en.wikipedia.org/wiki/Dependent_type
- pmoriarty 7y agoYou actually don't need types or classes to do this. You could use design by contract, which is what I've done in Python and Perl, neither of which have very fancy type systems compared to something like Haskell. With design by contract you can put in whatever fancy constraints you want on function parameters and return values, and those will be enforced. As far as objects go, they're much more useful for me as just a means of passing state. Rather than using a bunch of global variables or having to pass in a ton of function arguments, I can just use an object which contains all the state I need. Of course, having lots of state can usher in its own set of problems, and there's something to be said for trying to make your code as stateless as possible. But sometimes you need state, and maybe even a lot of it.
- MaxBarraclough 7y ago> Which languages would you say are really good for defining your own types? Would they be a good fit for the example I provided where a function needs to accept integers larger than zero? I'm not an Ada expert, but it has excellent support for range-restricted integer types.[0] Ada's 'discriminated types' are also fun. They let you create members which only exist when they're applicable. [1] > Do they also allow you to define your own operations on those types Looks like Ada supports operator overloading, yes. [2] [0] https://www.adaic.org/resources/add_content/standards/05rm/html/RM-3-2-2.html https://www.adaic.org/resources/add_content/standards/05rm/h... [1] https://www.adaic.org/resources/add_content/standards/05rat/html/Rat-3-5.html https://www.adaic.org/resources/add_content/standards/05rat/... [2] https://www.adaic.org/resources/add_content/standards/05aarm/html/AA-6-6.html https://www.adaic.org/resources/add_content/standards/05aarm...
- kqr 7y agoAda is also great at separating the various aspects of OOP into separate language concepts, instead of going "everything is done with classes!!" as many popular languages do, leading to a lot of confusion in this thread.
- crooked-v 7y agoA simple TypeScript example: interface DuckTypedObject { quack: true bark?: false eyes: 'beady' } This will require that any object used as a DuckTypedObject must have the `quack` and `eyes` properties and may optionally have a `bark` of `false`, but doesn't otherwise prescribe what the object actually has to be.
- Scarbutt 7y agoThis is the best part of TS IMO (structural typing).
- ledauphin 7y agoagreed 100%. structural, statically-checkable typing of objects is the best thing to happen for reusable and testable code in my programming lifetime.
- deleted 7y ago[deleted]
- uk_programmer 7y agoSorry to be pedantic but that isn't quite right. I do use the same technique myself quite a lot with the options pattern. However it is worth keeping the following in mind. The compiler will check that in your code that these properties are present and assigned correctly. However at runtime nothing is guaranteed especially when dealing with the DOM. One of the things that I don't like about TypeScript (I have written a fair bit of it) is that you believe you have type safety when it is really type hinting.
- crooked-v 7y agoValidation functions are pretty easy to write, as long as your data is being handled in a predictable way.
- uk_programmer 7y ago