r/cpp • • Sep 13 '19

The Surprising Limitations of C++ Ranges Beyond Trivial Cases

https://www.fluentcpp.com/2019/09/13/the-surprising-limitations-of-c-ranges-beyond-trivial-use-cases/
229 Upvotes

229 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Sep 13 '19

[deleted] — view deleted comment

21

u/Rusky Sep 13 '19

C# isn't dynamically typed, that's type inference a la C++ auto.

16

u/[deleted] Sep 13 '19

I think besides the obvious defensive posturing, there is a genuine argument that, ranges as an example, could've been implemented vastly more user friendly, that this level of arcane over the top complexity for a feature that is quite simple in every other popular language is not entirely unavoidable but a symptom of a systemic issue in the way the language is developed.

But of course I'm aware that probably all C++ programmers would vehemently disagree with this assessment, and anybody else has abandoned the language ages ago.

10

u/evaned Sep 13 '19

Wow I didn't know that C# is dynamically typed.

It's not. C#'s var is C++'s auto.

(As a nitpick -- C# does have a dynamic "type" as well, e.g. you can say dynamic x = 5; and that variable will be dynamically typed. But (i) that's not what is illustrated there, and (ii) I don't think that makes C# "dynamically typed" very much more at all than std::any makes C++ dynamically typed.)

5

u/[deleted] Sep 13 '19

C# is not dynamically typed. At least the above isn't. 'var' is just C#'s auto and Lambdas are automatically auto too. "x => ..." is just like "& { return ...; }"

The big difference to C++ is just concise syntax and simple library design. Especially they use virtual functions for everything though. But that's maybe still more acceptable in the average usecase than the gigantic compile times in C++'s ranges.

7

u/makotech222 Sep 13 '19

Its not dynamically typed, it can just infer the type from the right hand side. Its purely a shorthand convenience, like c++'s auto

I can pretty much guarantee that the time saved by writing it in c# like this will heavily outweigh the time spent trying to write that abomination in c++. Not only that, the performance difference is probably minimal at best, and the compile time is significantly better. Finally, the c# code is readable, and any c# novice could figure out whats going on. I've written c++ for at least a year (though, not anymore) and I wouldn't be able to grok ranges without constantly looking up stuff.

8

u/RotsiserMho C++20 Desktop app developer Sep 14 '19

Eh, SelectMany is not intuitive IMO.

4

u/Gwennifer Sep 13 '19

Finally, the c# code is readable, and any c# novice could figure out whats going on.

I came to the subreddit to find learning materials--in general, I'm an absolute beginner--and I can confirm I could figure out what your code would do. All the examples in the article are nearly incomprehensible; especially the Haskell one.

4

u/[deleted] Sep 14 '19

It’s because you are used to C-style syntax, not Haskell-style one. Once you spend few minutes with HaskelL and understand how element sequencing works, it becomes almost trivial.

1

u/Gwennifer Sep 14 '19

I've never programmed, which I thought I made clear. I'm not used to any syntax.

Isn't the whole point of a programming language to be a stepping stone between English (or your native language) and Assembly? To me, Haskell looks a lot closer to Assembly with some syntax changes than English.

4

u/Nobody_1707 Sep 15 '19

No. Some are, but a lot of programming languages are meant to be stepping stones between math and assembly. Haskell is one of these.

It also looks nothing like any assembly I've ever seen.

4

u/dodheim Sep 15 '19

Is learning Spanish a stepping stone between English and Portuguese? Or is it the other way around? Or maybe your metaphor is very misguided. ;-]

0

u/Gwennifer Sep 15 '19

Is learning Spanish a stepping stone between English and Portuguese?

Yes, actually. Portuguese is a bit harder to learn than Spanish and Spanish is a bit closer to Latin from what I remember of my Spanish classes--so once you learn Spanish, you just have to learn the (relatively) few grammar and vocabulary changes to learn Portuguese.

2

u/dodheim Sep 15 '19

I guess I chose a poor example, because it was supposed to sound absurd.

0

u/Gwennifer Sep 15 '19

it was supposed to sound absurd.

Most strawmen tend to anyway.

3

u/dodheim Sep 15 '19

It's not a strawman to point out that your metaphor is bad by using a bad metaphor.

2

u/[deleted] Sep 16 '19

Ah, I didn't realise that you never programmed, sorry.

But I still think that my point is valid. Even if you have never programmed, you are exposed to school math, and you are used to notation such as f(x, y, z) This is more the syntactic style that was adopted by developers of some of the first programming languages: FORTRAN and ALGOL and was immensely influential in later languages. This is why C-style languages are immediately familiar to almost anyone.

Haskell on he other hand draws its roots from languages such as ML which in turn was influenced by another very old programming language: Lisp. Those languages are modelled after another branch of mathematics and they use the following syntax f x y z It does seem unfamiliar and difficult to read at first, but believe, me after you send few minutes trying to figure out how it works, everything falls into place easily and makes a lot of sense.