# Multiplication by juxtaposition

**URL:** <https://forums.swift.org/t/multiplication-by-juxtaposition/64128>\
**Category:** Swift Numerics\
**Tags:** april-fools\
**Created:** [April 1, 2023, 5:51pm UTC](https://forums.swift.org/t/multiplication-by-juxtaposition/64128 "2023-04-01T17:51:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [April 1, 2023, 5:51pm UTC](https://forums.swift.org/t/multiplication-by-juxtaposition/64128/1 "2023-04-01T17:51:45Z")

</div>

# Multiplication by juxtaposition

**Proposal:** SN–NNNN  
**Author:** @Nevin  
**Status:** Ready to go

# Introduction

We propose to allow multiplication to be written by juxtaposition, as in “n(n+1)”.

# Motivation

One of the goals of the Swift Numerics package is to make it easier to write mathematical code in a familiar and easy-to-understand way. Or at least, the authors of this proposal assume that’s a goal. We didn’t actually check.

Anyway, it is common practice in mathematical writing to denote multiplication by justaposition. For example, “2x” or “(x+1)(x-1)”. The first form, without parenthesis, is prone to ambiguity in Swift, because for example `2e-4` represents a floating-point literal, and it would be source-breaking to parse it as `2 * e - 4` instead. Thus, for the current pitch, we require the second term to be enclosed within parentheses.

# Proposed solution

We propose to allow multiplication by juxtaposition in Swift. This will allow people to write simple, easy-to-read code such as:

```swift
let y = 2(x+1)
let z = (x+1)(x-1)
let crossRatio = (a-c)(b-d) / (b-c)(a-d)

```

Notice, in that last example, the multiplication in the denominator has higher precedence than the division. We consider this a desirable feature, as it matches the way people naturally write and understand expressions of this form.

Moreover, multiplication by juxtaposition has higher priority than any other operation. Users who want a lower precedence should write out the multiplication operator as they currently do.

# Detailed design

This feature can be implemented in Swift as it exists today. No changes to the language are necessary. For edification purposes we present a complete, working implementation here. The particular details of this implementation are not salient to the proposal, so we have hidden the code to keep the length of this post manageable:

> **Implementation of multiplication by juxtaposition**
>
> ```swift
> extension Numeric {
> func callAsFunction(_ x: Self) -> Self {
> return self * x
> }
> }
> 
> ```

# Source and ABI compatibility

This is an additive feature.

# Future directions

As mentioned above, this feature currently requires that the second term of a product be enclosed in parentheses. We are hopeful that this limitation might be lifted in the future, to allow expressions like `2x` and `(n+1)n`. More precisely, we hope that someone else can figure out how to lift the restriction, because we ain’t got no freakin’ clue.

# Alternatives considered

We considered the alternative of not adding this feature, but, like, why wouldn’t we?

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [April 1, 2023, 6:35pm UTC](https://forums.swift.org/t/multiplication-by-juxtaposition/64128/2 "2023-04-01T18:35:19Z")

</div>

Playing it straight:

Julia is the most successful language I know of that has actually done this somewhat thoughtfully. IIRC they permit only the following cases:

- [numeric literal][identifier]
- [numeric literal][parenthesized expression]
- [parenthesized expression][identifier]

Of note, they permit `(x+1)x`, but not `(x+1)(x-1)`, precisely to avoid ambiguity with function application (`callAsFunction` in Swift). This matters when working with types like quaternions, where `q(v)` would often refer to the action of the quaternion by conjugation rather than the quaternion-vector product `qv`.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [April 1, 2023, 6:53pm UTC](https://forums.swift.org/t/multiplication-by-juxtaposition/64128/3 "2023-04-01T18:53:36Z")

</div>

You can do this without any new language changes using result builders.

```swift
let x: @Multiply () -> Int = {
  (x+1)
  (x-1)
}

```

---

<div class="post-metadata">

**Author:** ![davedelong](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davedelong/32/38708_2.png) [@davedelong](https://forums.swift.org/u/davedelong)\
**Post date:** [April 1, 2023, 7:32pm UTC](https://forums.swift.org/t/multiplication-by-juxtaposition/64128/4 "2023-04-01T19:32:43Z")

</div>

+1 sounds good to me.

This is more colloquially known as "implicit multiplication", and it's very feasible to implement. My math parsing library does this [as part of interpreting tokens](https://github.com/davedelong/DDMathParser/blob/master/MathParser/Sources/MathParser/TokenResolver.swift#L296-L314). My implementation is a _little_ simpler, because by the time I inject the implicit multiplication token, I already know if I'm dealing with a variable token or a potential "function name" token; I also assume that an identifier followed by an open parenthesis is always a function call.

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [April 1, 2023, 8:09pm UTC](https://forums.swift.org/t/multiplication-by-juxtaposition/64128/5 "2023-04-01T20:09:24Z")

</div>

This is a good start but it doesn't go far enough.

If you juxtapose a sequence of superscript numerals to the right of another `Numeric` term, the compiler should lower that to a type-appropriate exponentiation:

```swift
let x = 0.5² // let x = pow(0.5, 2)
let y = 2¹⁰ // let y = 1 << 10

```

Those are all currently lexed as identifiers, but Swift 6 would be the perfect time to source-break and treat them as a special kind of numeric token instead.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 3, 2023, 9:03am UTC](https://forums.swift.org/t/multiplication-by-juxtaposition/64128/6 "2023-04-03T09:03:57Z")

</div>

I take it that space is allowed between terms and (with or without space) `1/2 (3)` would give `1/6` under this proposal (vs `3/2` if there was an explicit `*`)?

We could also consider addition by juxtaposition: `9 3/4`.
