# C++2026 vs Julia syntax comparison

**URL:** <https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533>\
**Category:** Offtopic\
**Created:** [January 31, 2024, 9:37pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533 "2024-01-31T21:37:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [January 31, 2024, 9:37pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/1 "2024-01-31T21:37:38Z")

</div>

Are you kidding, guys?

C++

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/3/338574f63888526fc3a6193ef796a8739e896e2e.png)  
vs. Julia  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/f/9fd72761be57ffd66324f902a6cb53f5a3f163b6.png)

---

<div class="post-metadata">

**Author:** ![sbuercklin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbuercklin/32/15728_2.png) [@sbuercklin](https://discourse.julialang.org/u/sbuercklin)\
**Post date:** [January 31, 2024, 9:49pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/2 "2024-01-31T21:49:53Z")

</div>

The function body is only 2 extra lines if you ignore the `include` statements, and the extra lines seem to be for constructing the range. This doesn’t seem that bad to me.

---

<div class="post-metadata">

**Author:** ![rayegun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayegun/32/26729_2.png) [@rayegun](https://discourse.julialang.org/u/rayegun)\
**Post date:** [January 31, 2024, 9:50pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/3 "2024-01-31T21:50:00Z")

</div>

To be honest having executors as a first class feature in Julia, as shown in that example, would be fantastic. Transducers.jl has them of course, but it’s not quite the same. Similarly a lot of the STL container features recently are _really_ well thought out. It would be nice to have a similarly constructed container set up in Julia.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [January 31, 2024, 9:50pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/4 "2024-01-31T21:50:48Z")

</div>

It is not just about how many lines, is it?

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 31, 2024, 9:50pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/5 "2024-01-31T21:50:48Z")

</div>

it’s a different language with a different set of strengths

julia, are you kidding?

```julia
N = 40
x = float.(0:1:N-1)
x .*= 2
x .*= 3.0
for (i, e) in enumerate(x)
    @assert e == 6.0 * (i - 1)
end

```

vs spoken english:

```julia
multiply the numbers from 0 to 39 by 6 without error

```

---

<div class="post-metadata">

**Author:** ![sbuercklin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbuercklin/32/15728_2.png) [@sbuercklin](https://discourse.julialang.org/u/sbuercklin)\
**Post date:** [January 31, 2024, 9:51pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/6 "2024-01-31T21:51:20Z")

</div>

What do you find objectionable about the code?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 31, 2024, 9:52pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/7 "2024-01-31T21:52:12Z")

</div>

Parallel broadcasting syntax would be nice to have. I’m not sure what parallel means here though.

---

<div class="post-metadata">

**Author:** ![rayegun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayegun/32/26729_2.png) [@rayegun](https://discourse.julialang.org/u/rayegun)\
**Post date:** [January 31, 2024, 9:57pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/8 "2024-01-31T21:57:52Z")

</div>

That’s what the executors are nice for, you can exactly specify what resources are used etc

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [January 31, 2024, 10:33pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/9 "2024-01-31T22:33:35Z")

</div>

> [@adienes](#):
>
> `multiply the numbers from 0 to 39 by 6`

`collect(0:39 .* 6)`

> [@rayegun](#):
>
> [Transducers.jl](https://juliahub.com/ui/Packages/Transducers) has them of course, but it’s not quite the same. Similarly a lot of the STL container features recently are _really_ well thought out. It would be nice to have a similarly constructed container set up in Julia.

Not up-to-speed on the difference, and why does it need to be in Julia? In C++ you want a stdlib because of many compilers, in Julia you want as ~~just~~ _much_ as possible out of the stdlib, only what Julia needs for itself and to hide platform differences. Actually I want OpenBLAS (and LinearAlgebra and more, already on the way) gone.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 31, 2024, 10:54pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/10 "2024-01-31T22:54:17Z")

</div>

> [@Palli](#):
>
> in Julia you want as just as possible out of the stdlib, only what Julia needs for itself and to hide platform differences.

because the semantics in Base does not allow such extention. If people already wrote their code with `a .=* b`, it won’t be possible to enable parallel or even distributed execution without re-writing the code.

---

<div class="post-metadata">

**Author:** ![rayegun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayegun/32/26729_2.png) [@rayegun](https://discourse.julialang.org/u/rayegun)\
**Post date:** [January 31, 2024, 11:18pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/11 "2024-01-31T23:18:12Z")

</div>

The Julia container ecosystem is very fragmented because the container _interfaces_ are both underdefined and underdocumented.

Julia’s AbstractArray interface is fantastic, and provides much of what C++ is only now just catching up to. It’s still better in a lot of ways. But for all the extensibility that Julia provides, we lack rigorous interfaces for _what_ an AbstractArray actually _is_, what it can do, and how it’s allowed to do it. Even more so for iterators and Dicts etc. Because we lack even guidelines around a lot of this it’s difficult to parallelize automatically, and to do STL style executors and abstract algorithms.

In Julia you get so much of this for free, but in many ways that freedom has led to stagnation vs the STL. There have been many attempts to solve this: ArrayInterface, ArrayIteration, KernelAbstractions, Swizzles, the wonderful Transducers, etc all address some part of this.

Julia _can_ do these things, I think much better than the juggernaut that is C++. In particular we don’t need to be as strict as C++ in a lot of ways. But it would be a Julia 2.0 level change, with an absolute _ton_ of work.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 31, 2024, 11:42pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/12 "2024-01-31T23:42:29Z")

</div>

Those don’t look equivalent to me, and in a way that makes Julia only a bit more concise. The Julia version doesn’t `const` `N`, there’s no `main`, the scaling by 3.0 doesn’t take a parallelization trait.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 1, 2024, 12:01am UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/13 "2024-02-01T00:01:39Z")

</div>

Sure, happy to oblige.

```julia
using Folds
const N = 10
x = float.(0:1:N-1)
x .*= 2
Folds.map!(a -> 3 * a, x, x, ThreadedEx())
for (i, e) in enumerate(x)
    @assert e == 6 * (i-1)
end

```

I will take the Julia version over the C++ bloviating anytime.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 1, 2024, 12:06am UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/14 "2024-02-01T00:06:15Z")

</div>

```julia
Folds.map!(a -> 3 * a, x, x, ThreadedEx())

```

Is that safe? I don’t think it would be safe with `Base.map!`.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 1, 2024, 12:12am UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/15 "2024-02-01T00:12:11Z")

</div>

IIRC unlike the functions with reductions, `map!` operations are safely independent when the indices of the aliased input and output match, but evaluation order matters when the indices are not aligned. I don’t know why this wasn’t added to the documentation for `map!` specifically along with the warning against output-input aliasing that was recently added to this class of functions.

---

<div class="post-metadata">

**Author:** ![rayegun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayegun/32/26729_2.png) [@rayegun](https://discourse.julialang.org/u/rayegun)\
**Post date:** [February 1, 2024, 9:32am UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/16 "2024-02-01T09:32:48Z")

</div>

I realize I kind of answered a different question in my initial reply. The reason you want _something_ similar to this in Base is that iteration is a fairly core part of most languages. We don’t necessarily want a ton of actual algorithms in the standard libraries, but having the definitions of the iteration framework for the language is a fairly uncontroversial idea. Executors are a small but equally sensible extension to that idea, allowing you to write generic algorithms that abstract the where (device vs. host), the what (container), and the how (executor) of an iterator.

This would get us two things:

1. Massively decreased code size across the ecosystem, by greatly increasing the genericity of iterator based algorithms. Think how much code has to treat SparseArrays differently from Arrays for instance, simply because an iterator will densify the SparseArray by default.
2. Those algorithms that are included in Base can do magic things like Chapel auto-parallelizing simply by passing in a new executor.

Of course settling on the universal minimum interface for good generic iteration is really difficult, as seen in the dozens of packages that try to make a bit of progress here. There’s no one size fits all, for instance flattening nested for loops into one iterator works for some codes but leads to tons of extraneous if statements for other codes. Luckily for us we can always write specialized dispatches for maximum speed where needed 🙂 .

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 1, 2024, 10:05am UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/17 "2024-02-01T10:05:46Z")

</div>

Perfect translation of the C `assert` into Julia `@assert` 😉 (both being an antipattern for this kind of checks since they can compile to nothing at all depending on the optimization flags).

As @Palli noted it’s funny that Julia tries to excise LinearAlgebra from Base while C++ adds it…

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [February 1, 2024, 10:54am UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/18 "2024-02-01T10:54:26Z")

</div>

> [@sijo](#):
>
> `@assert` 😉 (both being an antipattern […] since they can compile to nothing at all depending on the optimization flags).

It never compiles away in Julia, I checked. Hypothetically, that could change and I do want a debug mode, but unsure if it should be compiled away without it.

> [@rayegun](#):
>
> Of course settling on the universal minimum interface for good generic iteration is really difficult, as seen in the dozens of packages that try to make a bit of progress here.

I remember when the STL arrived (the more interesting part of C++, better then the OO part), but you are talking about recent STL containers, i.e. it took a very long time for C++ to develop to that point. I don’t know, maybe stuff was available earlier, in Boost, and I believe some stuff from Boost got into the C++ standard. More than strictly needed? People can and do use Boost (maybe never all compilers could use it), now they raise the minimum a certain C++ version compiler must support. I’m just saying Julia doesn’t have that same issue, and Base could add stuff after it’s been developed and perfected elsewhere, but strictly doesn’t need to.

---

<div class="post-metadata">

**Author:** ![xiaoxi](https://avatars.discourse-cdn.com/v4/letter/x/a9adbd/32.png) [@xiaoxi](https://discourse.julialang.org/u/xiaoxi)\
**Post date:** [February 1, 2024, 12:44pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/19 "2024-02-01T12:44:56Z")

</div>

I like the idea of telling the `scale` function how to execute. It reminds me of Chris’ talk “LinearSolve.jl: Because A\b is Not Good Enough”.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [February 10, 2024, 2:01pm UTC](https://discourse.julialang.org/t/c-2026-vs-julia-syntax-comparison/109533/20 "2024-02-10T14:01:09Z")

</div>

exactly this!

Quite often we have a forward model which contains some parallel functions (FFTs, …).  
But then the loss function evaluation is some broadcasting syntax and hence single-threaded.  
Tullio.jl kinda solves this for CPUs but then the code does not run very well with AD on GPUs.
