5 ms·
> I do really hate the type systems of Java and C++ because I think they're restrictive Can you give an example of how the C++ type system restricts you? Is it
by MauranKilom 7y ago
> I do really hate the type systems of Java and C++ because I think they're restrictive
Can you give an example of how the C++ type system restricts you? Is it just lack of some inbuilt mechanisms/syntactic sugars or is there something that you can't actually model with it?
> I think that forces strong and unnecessary coupling
How do you model/maintain invariants of a set of data in FP? That's the part that I don't understand yet.
- tombert 7y agoI haven't touched C++ for several years at this point, so bear with me a bit, but in the category of "just really annoying, not really restrictive", there is the fact that you have to constantly re-type the types for everything, like `MyType x = new MyType()`, but my understanding is that has been addressed by the `auto` keyword. Is there some equivalent to a monad in C++? As in, something that will "pollute" the function so that if it's trying to call something with side effects, it's reflected in the type? I have no doubt that you could stitch something together with the templates to get something, but in Haskell, out of the box, your functions that don't have side effects if they don't return `IO`. Of course, this wouldn't be a sign of it being "restrictive", just a feature that's not in there. Maybe I'm being a bit unfair to C++ by tying its type system to Java, which is terrible. One thing that I really dislike is the fact that, in order to make class X part of interface Y, you need to have access to X's source, or extend it. I find this incredibly annoying, and with Haskell, you can attach any type to a typeclass by providing its implementation. I know C# has this with its extension methods, but AFAIK C++ doesn't have any equivalent. > How do you model/maintain invariants of a set of data in FP? That's the part that I don't understand yet. I don't really know what you mean by that; you can restrict the allowed input types with typeclasses or existential quantifiers, but I'm not sure that I'm answering your question.
- zabzonk 7y agoYou almost never use `new` in modern C++, and you would never use it as you suggest because it wouldn't compile, you would simply say: MyType x; C++ is nothing at all like Java.
- tombert 7y agoSorry, you'd need to use the MyType* x to use the `new` keyword; it's been awhile. It doesn't really deter from my point though; in order to heap-allocate something you have to use a pointer, and you end up doing something not that dissimilar from Java. > C++ is nothing at all like Java. Is that supposed to be a joke? Java was marketed specifically towards C++ engineers...
- saagarjha 7y ago> in order to heap-allocate something you have to use a pointer Usually the heap allocation is hidden behind a handle.
- pjmlp 7y agoNot on the libraries I usually have to touch. I would already be happy when I am allowed to re-write some of it into C++11.
- iainmerrick 7y agoJavascript was marketed towards Java programmers, and those languages are pretty different from each other too.
- tombert 7y agoI would say that C++ is closer to Java than JavaScript is; it's fairly easy to copy-paste and edit C++ code into a Java file with, just changing a small amount of syntax and get rid of the manual memory allocations. Try copy-pasting JS into a Java file, and there are semantics there that are completely foreign; you can't simply translate it.
- iainmerrick 7y agoI agree that JS and Java are very different; that was my point. I disagree about Java and C++. If I have a C++ file that uses RAII or references, for example, it won’t translate to Java easily. And those aren’t unusual, they’re both common techniques.
- tome 7y ago> How do you model/maintain invariants of a set of data in FP? That's the part that I don't understand yet. If you tell me how you do it in not-FP then I'll tell you how to do it in FP!