Swift3 : how to handle precedencegroup now operator should be declare with a body?

12,015

As per SE-0077, the precedence of an operator is no longer determined by a magic number – instead you now use the higherThan and (if the group resides in another module) lowerThan precedencegroup relationships in order to define precedence relative to other groups.

For example (from the evolution proposal):

// module Swift
precedencegroup Additive { higherThan: Range }
precedencegroup Multiplicative { higherThan: Additive }

// module A
precedencegroup Equivalence {
  higherThan: Comparative
  lowerThan: Additive  // possible, because Additive lies in another module
}
infix operator ~ : Equivalence

1 + 2 ~ 3    // same as (1 + 2) ~ 3, because Additive > Equivalence
1 * 2 ~ 3    // same as (1 * 2) ~ 3, because Multiplicative > Additive > Equivalence
1 < 2 ~ 3    // same as 1 < (2 ~ 3), because Equivalence > Comparative
1 += 2 ~ 3   // same as 1 += (2 ~ 3), because Equivalence > Comparative > Assignment
1 ... 2 ~ 3  // error, because Range and Equivalence are unrelated

Although in your case, as it appears that your operator is used for multiplication, you could simply use the standard library's MultiplicationPrecedence group, which is used for the * operator:

infix operator × : MultiplicationPrecedence

It is defined as:

precedencegroup MultiplicationPrecedence {
  associativity: left
  higherThan: AdditionPrecedence
}

For a full list of standard library precedence groups, as well as more info about this change, see the evolution proposal.

Share:
12,015

Related videos on Youtube

Stéphane de Luca
Author by

Stéphane de Luca

My passion is to quickly materialize ideas: one of my latest pet project is ”Gas Discount” ⛽️ to get 20% off on your gas (https://apps.apple.com/nl/app/carburant-discount/id1465235480) (Swift) Released v1 @ xmas 2019 (ads+freemium model). Writing v2 in Dart/Flutter for early 2022. Currently, and since 2019: VP digital, CTO &amp; CPO at Olenergies (🔋 LiFP connected batteries) where I develop anEMS IoT ecosystem from crash that comprises — 1) an IoT platform that connects DERs and optimizes the energy of thousands batteries across the world (Azure / MongoDB / TSI / InfluxDB / PostgreSQL / MERN / GraphQL / Docker / WSS / MQTT / IoT Hub / DPS) — 2) a Linux-based smart controller (Beaglebone / NodeJS) and custom ESP32 PCB (ESP32 / CPP / FreeRTOS) that connects to batteries BMS (thru CANbus &amp; RS485) and charger/inverter (thru MODbus). w/ automatic updates and — 3) an iOS + Android BLE app to control the control everything (Flutter 2 / Dart). 2014 - 2016: CTO / CPO at Presstalis/ZEENS. I created a brand new B2C digital press service that exposes all journals &amp; mags published in France since 1947 via an all-you-can-eat subscription-based model on iPad (See video here https://stephanedeluca.com/zeens.php). I also designed and developed mobile-to-store and digital newsstand apps which includes social networking and bringing innovative BM to the press industry (50 % discount on mags) + in-store purchase of journals &amp; show tickets in partnership with Ville de Paris, JCDecaux, Mediakiosk, FNAC/France Billet, Logista and Presstalis) 2009 - 2014: member of the Executive Committee of the RIAM government funding organisation (CNC/OSEO): financing of innovative projects in the movie and computer-related industries at large (11M€/year). 2010 - 2014: VP Products and CTO at LeKiosque.fr, focused on the digital magazine distribution where I designed the first ever 3D newsstand for iPad, ranking #1 grossing app for iPad in France with more 1.5M downloads! 2009 - 2010, acted as COO for Mimesis Republic and industrialized the production (MMO/social network &amp; apps). In 2009, I published Santa-Claus for iPhone on AppStore, introducing for the 1st time the 3D parallax effect since natively integrated by Apple in iOS! 2007 - 2009, I was the MobiLuck.com CTO (geolocalized social network for mobiles). This LBS friends finder service displays 35M pages/month and has attracted more than 1.2M users (Q2 2009) since its launch in August 2007 and currently recruits an average of 5K users per day as per the current trend and we see our traffic doubling every quarter! From 2003, I was the In-Fusio Advanced Technology Manager where I created mobile phone massive cross-platform multi-player technology &amp; service. Before, I've been in the video game industry for 10y where I successively developed titles, created &amp; sold realtime 3D technologies &amp; finally took in charge a business unit of Kalisto (IPO in 1999) Get in touch @ +33 6 13 510 966 / [email protected]

Updated on August 25, 2020

Comments

  • Stéphane de Luca
    Stéphane de Luca almost 4 years

    Former Swift 3 code for operator was:

    infix operator × {associativity left precedence 150}
    

    But now, as per Xcode 8 beta 6, this generate the following warning:

    "operator should not be declared with body"
    

    What's the right way to use precedencegroup predicate as no doc exists right now?

    I have tried this, but does not work:

    infix operator × : times
    precedencegroup times {
         associativity: left 
         precedence: 150
    }