Replacement for NSNumber?


(Maury Markowitz) #1

I have a primary data structure that is currently defined such:

var data: [String: NSNumber]

This is the only place in my app where I use a Foundation type *as* a Foundation type (as opposed to a bridge), and I would strongly prefer this to be purely Swift types.

I used NSNumber because the original data is variant, a mix of Int and Double - in some instances the first two entries in the dict are Ints, while in others it's the first four, and others are a mix based on other data points (it's very annoying). I could use two dictionaries, but this would *greatly* confuse code further into the program.

I know I could use Double and then floor the ints. But I'm wondering if I'm missing some more natural solution? I don't believe there is a common base type for Int and Double, but perhaps there is another way to solve this?


(Jeff Kelley) #2

One possible solution is to create an empty Protocol that both Int and Double conform to, perhaps DataValue, and then create your data structure like this:

var data: [String: DataValue]

Jeff Kelley

SlaunchaMan@gmail.com | @SlaunchaMan <https://twitter.com/SlaunchaMan> | jeffkelley.org <http://jeffkelley.org/>

···

On Apr 11, 2016, at 11:04 AM, Maury Markowitz via swift-users <swift-users@swift.org> wrote:

I have a primary data structure that is currently defined such:

var data: [String: NSNumber]

This is the only place in my app where I use a Foundation type *as* a Foundation type (as opposed to a bridge), and I would strongly prefer this to be purely Swift types.

I used NSNumber because the original data is variant, a mix of Int and Double - in some instances the first two entries in the dict are Ints, while in others it's the first four, and others are a mix based on other data points (it's very annoying). I could use two dictionaries, but this would *greatly* confuse code further into the program.

I know I could use Double and then floor the ints. But I'm wondering if I'm missing some more natural solution? I don't believe there is a common base type for Int and Double, but perhaps there is another way to solve this?
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Kenny Leung) #3

Moving this to swift-evolution.

It’s inconvenient that structs can’t inherit, and so things like Int and Double aren’t known as numbers. Should there be a Number protocol that encompasses all things number-like?

-Kenny

···

On Apr 11, 2016, at 8:06 AM, Jeff Kelley via swift-users <swift-users@swift.org> wrote:

One possible solution is to create an empty Protocol that both Int and Double conform to, perhaps DataValue, and then create your data structure like this:

var data: [String: DataValue]

Jeff Kelley

SlaunchaMan@gmail.com | @SlaunchaMan | jeffkelley.org

On Apr 11, 2016, at 11:04 AM, Maury Markowitz via swift-users <swift-users@swift.org> wrote:

I have a primary data structure that is currently defined such:

var data: [String: NSNumber]

This is the only place in my app where I use a Foundation type *as* a Foundation type (as opposed to a bridge), and I would strongly prefer this to be purely Swift types.

I used NSNumber because the original data is variant, a mix of Int and Double - in some instances the first two entries in the dict are Ints, while in others it's the first four, and others are a mix based on other data points (it's very annoying). I could use two dictionaries, but this would *greatly* confuse code further into the program.

I know I could use Double and then floor the ints. But I'm wondering if I'm missing some more natural solution? I don't believe there is a common base type for Int and Double, but perhaps there is another way to solve this?
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

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


(Jens Alfke) #4

Or just create an enum with an Int and a Double field. I believe this will be more efficient since the values can be stored inline, whereas indirecting through a protocol will force it to be boxed into a heap object (right?)

—Jens

···

On Apr 11, 2016, at 8:06 AM, Jeff Kelley via swift-users <swift-users@swift.org> wrote:

One possible solution is to create an empty Protocol that both Int and Double conform to, perhaps DataValue, and then create your data structure like this:

var data: [String: DataValue]

Jeff Kelley

SlaunchaMan@gmail.com <mailto:SlaunchaMan@gmail.com> | @SlaunchaMan <https://twitter.com/SlaunchaMan> | jeffkelley.org <http://jeffkelley.org/>

On Apr 11, 2016, at 11:04 AM, Maury Markowitz via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

I have a primary data structure that is currently defined such:

var data: [String: NSNumber]

This is the only place in my app where I use a Foundation type *as* a Foundation type (as opposed to a bridge), and I would strongly prefer this to be purely Swift types.

I used NSNumber because the original data is variant, a mix of Int and Double - in some instances the first two entries in the dict are Ints, while in others it's the first four, and others are a mix based on other data points (it's very annoying). I could use two dictionaries, but this would *greatly* confuse code further into the program.

I know I could use Double and then floor the ints. But I'm wondering if I'm missing some more natural solution? I don't believe there is a common base type for Int and Double, but perhaps there is another way to solve this?
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

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


(Joe Groff) #5

I would recommend an enum as well if Int and Double are literally the only two types you need to care about; it'll definitely be more efficient. However, a protocol type does have enough inline space to fit both Int and Double without spilling into a heap allocation.

-Joe

···

On Apr 11, 2016, at 8:58 AM, Jens Alfke via swift-users <swift-users@swift.org> wrote:

Or just create an enum with an Int and a Double field. I believe this will be more efficient since the values can be stored inline, whereas indirecting through a protocol will force it to be boxed into a heap object (right?)


(Joanna Carter) #6

It’s inconvenient that structs can’t inherit, and so things like Int and Double aren’t known as numbers. Should there be a Number protocol that encompasses all things number-like?

Taje a look at the IntegerType and FloatingPointType

···

--
Joanna Carter
Carter Consulting

(de mon iPhone)


(Ross O'Brien) #7

Your pointing to two mutually exclusive protocols proves the point. If I
want to write a generic function for a numerical type which can be added, I
can't just require that the type conforms to IntegerArithmeticType because
that excludes all the floating point types. Protocols for various numerical
functions, e.g. NumericalArithmeticType, would be handy,

···

On Mon, Apr 11, 2016 at 4:55 PM, Joanna Carter via swift-evolution < swift-evolution@swift.org> wrote:

> It’s inconvenient that structs can’t inherit, and so things like Int and
Double aren’t known as numbers. Should there be a Number protocol that
encompasses all things number-like?

Taje a look at the IntegerType and FloatingPointType

--
Joanna Carter
Carter Consulting

(de mon iPhone)

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


(Jordan Rose) #8

This comes up often enough that we should probably write it somewhere, but we don't have this protocol for the simple reason that there are very few algorithms that are correct for both integers and floating-point numbers. Even something as simple as "average" needs to be handled differently.

Jordan

···

On Apr 11, 2016, at 9:04 , Ross O'Brien via swift-evolution <swift-evolution@swift.org> wrote:

Your pointing to two mutually exclusive protocols proves the point. If I want to write a generic function for a numerical type which can be added, I can't just require that the type conforms to IntegerArithmeticType because that excludes all the floating point types. Protocols for various numerical functions, e.g. NumericalArithmeticType, would be handy,

On Mon, Apr 11, 2016 at 4:55 PM, Joanna Carter via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

> It’s inconvenient that structs can’t inherit, and so things like Int and Double aren’t known as numbers. Should there be a Number protocol that encompasses all things number-like?

Taje a look at the IntegerType and FloatingPointType

--
Joanna Carter
Carter Consulting

(de mon iPhone)

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

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


(Dave Abrahams) #9

Your pointing to two mutually exclusive protocols proves the point. If I want to
write a generic function for a numerical type which can be added, I can't just
require that the type conforms to IntegerArithmeticType because that excludes
all the floating point types. Protocols for various numerical functions, e.g.
NumericalArithmeticType, would be handy,

The upcoming proposals for integer and floating point protocols have a
common type called Arithmetic that serves this purpose.

···

on Mon Apr 11 2016, Ross O'Brien <swift-evolution@swift.org> wrote:

On Mon, Apr 11, 2016 at 4:55 PM, Joanna Carter via swift-evolution > <swift-evolution@swift.org> wrote:

    > It’s inconvenient that structs can’t inherit, and so things like Int and
    Double aren’t known as numbers. Should there be a Number protocol that
    encompasses all things number-like?

    Taje a look at the IntegerType and FloatingPointType

    --
    Joanna Carter
    Carter Consulting

    (de mon iPhone)

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

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

--
Dave