Changing precedence of / operator for my protocol?


(Rick M) #1

Working on dimensional analysis, I have some proof-of-concept code that seems to be working:

    let n1 = kilogram * meter / second * second
    ([(kg⋅m) / s]⋅s)

     let n2 = kilogram * meter / (second * second)
    [(kg⋅m) / (s⋅s)]

Note: () around unit products, [] around unit quotients.

I'd like to adjust the precedence of operator * for my Unit protocol to be higher than /. Is that possible? It wasn't at all clear to me how to do that in Swift 3, or if can even be done at all.

Thanks!

···

--
Rick Mann
rmann@latencyzero.com


(Greg Parker) #2

You can't. A Swift operator's precedence is the same for all types that implement that operator. Operators * and / cannot use the same precedence on Int but different precedence on Unit.

You could try to change the precedence of * and / globally - they're defined like any other operator in stdlib/public/core/Policy.swift - but you'll break lots of other code that way.

···

On Nov 29, 2016, at 2:55 AM, Rick Mann via swift-users <swift-users@swift.org> wrote:

Working on dimensional analysis, I have some proof-of-concept code that seems to be working:

   let n1 = kilogram * meter / second * second
   ([(kg⋅m) / s]⋅s)

    let n2 = kilogram * meter / (second * second)
   [(kg⋅m) / (s⋅s)]

Note: () around unit products, [] around unit quotients.

I'd like to adjust the precedence of operator * for my Unit protocol to be higher than /. Is that possible? It wasn't at all clear to me how to do that in Swift 3, or if can even be done at all.

--
Greg Parker gparker@apple.com Runtime Wrangler


(Howard Lovatt) #3

Why not define some other symbol so that you can get the precedence you
want?

  -- Howard.

···

On 30 November 2016 at 09:28, Greg Parker via swift-users < swift-users@swift.org> wrote:

> On Nov 29, 2016, at 2:55 AM, Rick Mann via swift-users < > swift-users@swift.org> wrote:
>
> Working on dimensional analysis, I have some proof-of-concept code that
seems to be working:
>
> let n1 = kilogram * meter / second * second
> ([(kg⋅m) / s]⋅s)
>
> let n2 = kilogram * meter / (second * second)
> [(kg⋅m) / (s⋅s)]
>
> Note: () around unit products, [] around unit quotients.
>
> I'd like to adjust the precedence of operator * for my Unit protocol to
be higher than /. Is that possible? It wasn't at all clear to me how to do
that in Swift 3, or if can even be done at all.

You can't. A Swift operator's precedence is the same for all types that
implement that operator. Operators * and / cannot use the same precedence
on Int but different precedence on Unit.

You could try to change the precedence of * and / globally - they're
defined like any other operator in stdlib/public/core/Policy.swift - but
you'll break lots of other code that way.

--
Greg Parker gparker@apple.com Runtime Wrangler

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rick M) #4

Why not define some other symbol so that you can get the precedence you want?

I could, but I think I'll just leave things as they are. No need to add confusion; it works to use parentheses.

···

On Nov 29, 2016, at 15:22 , Howard Lovatt <howard.lovatt@gmail.com> wrote:

  -- Howard.

On 30 November 2016 at 09:28, Greg Parker via swift-users <swift-users@swift.org> wrote:

> On Nov 29, 2016, at 2:55 AM, Rick Mann via swift-users <swift-users@swift.org> wrote:
>
> Working on dimensional analysis, I have some proof-of-concept code that seems to be working:
>
> let n1 = kilogram * meter / second * second
> ([(kg⋅m) / s]⋅s)
>
> let n2 = kilogram * meter / (second * second)
> [(kg⋅m) / (s⋅s)]
>
> Note: () around unit products, [] around unit quotients.
>
> I'd like to adjust the precedence of operator * for my Unit protocol to be higher than /. Is that possible? It wasn't at all clear to me how to do that in Swift 3, or if can even be done at all.

You can't. A Swift operator's precedence is the same for all types that implement that operator. Operators * and / cannot use the same precedence on Int but different precedence on Unit.

You could try to change the precedence of * and / globally - they're defined like any other operator in stdlib/public/core/Policy.swift - but you'll break lots of other code that way.

--
Greg Parker gparker@apple.com Runtime Wrangler

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com


(Jacob Bandes-Storch) #5

You could also define a ** exponentiation operator, i.e. "kilogram * meter
/ second**2".

···

On Tue, Nov 29, 2016 at 4:15 PM, Rick Mann via swift-users < swift-users@swift.org> wrote:

> On Nov 29, 2016, at 15:22 , Howard Lovatt <howard.lovatt@gmail.com> > wrote:
>
> Why not define some other symbol so that you can get the precedence you
want?

I could, but I think I'll just leave things as they are. No need to add
confusion; it works to use parentheses.

>
> -- Howard.
>
> On 30 November 2016 at 09:28, Greg Parker via swift-users < > swift-users@swift.org> wrote:
>
> > On Nov 29, 2016, at 2:55 AM, Rick Mann via swift-users < > swift-users@swift.org> wrote:
> >
> > Working on dimensional analysis, I have some proof-of-concept code
that seems to be working:
> >
> > let n1 = kilogram * meter / second * second
> > ([(kg⋅m) / s]⋅s)
> >
> > let n2 = kilogram * meter / (second * second)
> > [(kg⋅m) / (s⋅s)]
> >
> > Note: () around unit products, [] around unit quotients.
> >
> > I'd like to adjust the precedence of operator * for my Unit protocol
to be higher than /. Is that possible? It wasn't at all clear to me how to
do that in Swift 3, or if can even be done at all.
>
> You can't. A Swift operator's precedence is the same for all types that
implement that operator. Operators * and / cannot use the same precedence
on Int but different precedence on Unit.
>
> You could try to change the precedence of * and / globally - they're
defined like any other operator in stdlib/public/core/Policy.swift - but
you'll break lots of other code that way.
>
>
> --
> Greg Parker gparker@apple.com Runtime Wrangler
>
>
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rick M) #6

You could also define a ** exponentiation operator, i.e. "kilogram * meter / second**2".

Oh, good idea! Although I may use a carat. It also will simply s*s to s<sup>2</sup>, eventually.

···

On Nov 29, 2016, at 16:23 , Jacob Bandes-Storch <jtbandes@gmail.com> wrote:

On Tue, Nov 29, 2016 at 4:15 PM, Rick Mann via swift-users <swift-users@swift.org> wrote:

> On Nov 29, 2016, at 15:22 , Howard Lovatt <howard.lovatt@gmail.com> wrote:
>
> Why not define some other symbol so that you can get the precedence you want?

I could, but I think I'll just leave things as they are. No need to add confusion; it works to use parentheses.

>
> -- Howard.
>
> On 30 November 2016 at 09:28, Greg Parker via swift-users <swift-users@swift.org> wrote:
>
> > On Nov 29, 2016, at 2:55 AM, Rick Mann via swift-users <swift-users@swift.org> wrote:
> >
> > Working on dimensional analysis, I have some proof-of-concept code that seems to be working:
> >
> > let n1 = kilogram * meter / second * second
> > ([(kg⋅m) / s]⋅s)
> >
> > let n2 = kilogram * meter / (second * second)
> > [(kg⋅m) / (s⋅s)]
> >
> > Note: () around unit products, [] around unit quotients.
> >
> > I'd like to adjust the precedence of operator * for my Unit protocol to be higher than /. Is that possible? It wasn't at all clear to me how to do that in Swift 3, or if can even be done at all.
>
> You can't. A Swift operator's precedence is the same for all types that implement that operator. Operators * and / cannot use the same precedence on Int but different precedence on Unit.
>
> You could try to change the precedence of * and / globally - they're defined like any other operator in stdlib/public/core/Policy.swift - but you'll break lots of other code that way.
>
>
> --
> Greg Parker gparker@apple.com Runtime Wrangler
>
>
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>

--
Rick Mann
rmann@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com