# Why \`"hello"^true\` is allowed?

**URL:** <https://discourse.julialang.org/t/why-hello-true-is-allowed/4849>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 14, 2017, 8:32am UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849 "2017-07-14T08:32:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 14, 2017, 8:32am UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/1 "2017-07-14T08:32:45Z")

</div>

Is there a use case for the method `"hello"^true`? Why allow exponentiation with a Boolean?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 14, 2017, 8:55am UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/2 "2017-07-14T08:55:20Z")

</div>

you can write

```julia
flag ? string : ""

```

more compactly as

```julia
string^flag

```

Don’t use it if you don’t like it, but it is harmless.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 14, 2017, 9:24am UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/3 "2017-07-14T09:24:29Z")

</div>

I see, thanks.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [July 14, 2017, 12:06pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/4 "2017-07-14T12:06:13Z")

</div>

It saves a few characters, at the expense of being harder to understand - if you see `foo^bar` in some program, your first thought would be that it is exponentiation, not repetition, which is why this sort of punning is frowned upon now (same issue of course with `foo*bar`)

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [July 14, 2017, 12:13pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/5 "2017-07-14T12:13:01Z")

</div>

Another reason not to use `string^flag`, besides being harder to understand when seen in code, it is 10x slower than using the ?: operator (which on Intel, at least, optimized to just 3 instructions, with no branching at all (using a cmovneq instruction).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 14, 2017, 12:14pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/6 "2017-07-14T12:14:48Z")

</div>

I am aware that you don’t like this operator, but I have no intention of restarting [#11030](https://github.com/JuliaLang/julia/issues/11030) here.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [July 14, 2017, 12:24pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/7 "2017-07-14T12:24:40Z")

</div>

Even Jeff and Stefan how said that doing such punning was not a good idea, and I think it’s important for people to understand the costs, both in readability and performance, of using those operators, when there are clearer, faster alternatives.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 14, 2017, 12:30pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/8 "2017-07-14T12:30:22Z")

</div>

I have to say I side with @ScottPJones on this. I don’t like `*` for string concatenation.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 14, 2017, 12:33pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/9 "2017-07-14T12:33:25Z")

</div>

Which is perfectly fine, I was just suggesting not to start that debate again here.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [July 14, 2017, 12:43pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/10 "2017-07-14T12:43:24Z")

</div>

I was just trying to counter what you had said, about it being “harmless”, which (not just according to me) it is harmful for readability, as well as being significantly slower (in case this is in some performance critical code).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 14, 2017, 1:09pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/11 "2017-07-14T13:09:35Z")

</div>

`str^n` is just a different way of writing `repeat(str, n)`, it has nothing to do with the `?:` operator. It just so happens that `Bool` gets promoted to `Int`. One can easily fix the performance problem by specializing `repeat` on `Bool`s.

As for this:

> [@ScottPJones](#):
>
> It saves a few characters, at the expense of being harder to understand - if you see foo^bar in some program, your first thought would be that it is exponentiation, not repetition,

I am agnostic about the `*` and `^` operators for strings, but I think the idea is that exponentiation _is_ repetition, and that the use use of those two operators is internally consistent.

`x^4 = x*x*x*x = xxxx`.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [July 14, 2017, 2:08pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/12 "2017-07-14T14:08:37Z")

</div>

> [@DNF](#):
>
> str^n is just a different way of writing repeat(str, n)

Yes, and explicitly using a function name that only means one thing makes it more readable, as opposed to using a pun which requires you to figure out just what types the arguments are before you can understand what the code is actually doing.

Having one operator mean both `exp` and `repeat` based on the argument types does make the code harder to comprehend, which is my real objection, even if the performance issue were fixed.  
If you don’t believe me, ask the creators of Julia, who’ve stated that one shouldn’t use the same name for different concepts.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 14, 2017, 4:37pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/13 "2017-07-14T16:37:51Z")

</div>

> [@ScottPJones](#):
>
> which is why this sort of punning is frowned upon now (same issue of course with foo\*bar)

Please don’t spread this type of misinformation.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [July 14, 2017, 4:49pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/14 "2017-07-14T16:49:26Z")

</div>

Here is a quote from @StefanKarpinski: [https://github.com/JuliaLang/julia/issues/11030#issuecomment-96727303](https://github.com/JuliaLang/julia/issues/11030#issuecomment-96727303)

> My problem with \* for string concatenation is not that people find it unexpected but that it’s an inappropriate use of the \* generic function, which is agreed upon to mean numerical multiplication. The argument that strings form a monoid is kind of thin since lots of things form a monoid and we’re generally not using \* for them. At the time I introduced \* for strings, we were a lot less strict about operator punning – recall | and & for shell commands – we’ve gotten much stricter over time, which is a good thing. This is one of the last puns left in the standard library. The reason ++ would be better is not because it would be easier to learn (depends on where you’re coming from), but because the ++ operator in Julia would unequivocally mean sequence concatenation.

> Note that the operator punning is not an entirely academic concern. The Char corner case shows where the punning can cause problems: people might reasonably expect ‘x’ \* ‘y’ to produce either “xy” or 241. Because of this, we just make both of these operations no method errors, but it would be perfectly reasonable to allow ‘x’ ++ ‘y’ to produce “xy”. There’s a lot less of a case for having ‘x’ \* ‘y’ produce 241 or ‘ñ’, but the sequence concatenation operation does actually make sense.

Please don’t spread misinformation yourself, @kristoffer.carlsson

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 14, 2017, 5:16pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/15 "2017-07-14T17:16:42Z")

</div>

Easy there, folks. I’d say that this is just as much about having `Bool <: Number` ([#19168](https://github.com/JuliaLang/julia/issues/19168)) as it is about using `^` for string repetition (latest thoughts there are [#22461](https://github.com/JuliaLang/julia/pull/22461)). Neither is closed. Like it or not, this still isn’t completely black and white. Note that we’re in a usage question, not a Julia design one.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 14, 2017, 5:38pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/16 "2017-07-14T17:38:38Z")

</div>

Your argument for saying “this is frowned upon now” is a 2 year old Github comment from a single julia developer?

Please.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [July 14, 2017, 5:42pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/17 "2017-07-14T17:42:03Z")

</div>

There have been many more comments, from a variety of core Julia developers, over the last 2+ years.  
That one, from the person who introduced `*` and `^` for strings in the first place, I felt was pretty convincing.

Are you trying to say that operator punning (and punning in general) is _not_ considered bad Julia practice?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 14, 2017, 6:19pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/18 "2017-07-14T18:19:58Z")

</div>

The usage of \* and ^ for strings is idiomatic Julia that the official manual recommends. When this change, and the official manual recommends against this, you are free to say that “this is now frowned upon”. Until then you can say “_I_ frown upon it”.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [July 15, 2017, 5:00pm UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/19 "2017-07-15T17:00:48Z")

</div>

@kristoffer.carlsson is 100% correct. I did write that, but me writing a comment on GitHub is in no way official; I was just making an argument, and I’ve been known to change my mind, be wrong, and even not have my every whim reflected in the language 😱. Until a collective decision is reached on [#22461](https://github.com/JuliaLang/julia/pull/22461), using `*` for string concatenation is not “frowned upon” at all, it is still _the_ syntax for string concatenation and accordingly `s^n` is the syntax for string repetition.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [July 16, 2017, 10:29am UTC](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849/20 "2017-07-16T10:29:06Z")

</div>

To be perfectly clear, I said that that sort of operator punning is frowned upon now, which I believe is still the case, whether or not the particular instances of operator punning with `*` and `^` are officially frowned upon.

I’ve also heard the same about using the same function name for different meanings, from Jeff the first time.  
In practice since then, when reviewing other’s code I’ve seen how much harder it can be to read where the same operator or function name can mean radically different things, depending on the types of their arguments.

Do you honestly think that seeing `foo^n` in the middle of some code is more comprehensible than `repeat(foo, n)`, when the type of `foo` may not even be given in the function?

[Next page](https://discourse.julialang.org/t/why-hello-true-is-allowed/4849.md?page=2)
