Preventing Bool<->Number conversion/promotion

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Preventing Bool<->Number conversion/promotion

Penn Taylor
Is there a way to prevent automatic conversion/promotion between Bool and Number types?

Motivating examples:

type Bools
  a
::Bool
  b
::Bool
end

Bools(1, 0) #=> Bools(true,false)

type
Ints
  a
::Int64
  b
::Int64
end

Ints(true, false) #=> Ints(1,0)

In both of the above cases, I want the construction to fail.

I'm aware that I could write constructors for these types to explicitly reject Bool<->Number mismatch, but that gets tedious when there are more than a few Number or Bool fields in a type. Wondering if there's an easier way.
Reply | Threaded
Open this post in threaded view
|

Re: Preventing Bool<->Number conversion/promotion

Stefan Karpinski
At the moment no, although there's been discussion of making Bool not a subtype of Number.

On Mon, Oct 31, 2016 at 2:29 PM, Penn Taylor <[hidden email]> wrote:
Is there a way to prevent automatic conversion/promotion between Bool and Number types?

Motivating examples:

type Bools
  a
::Bool
  b
::Bool
end

Bools(1, 0) #=> Bools(true,false)

type
Ints
  a
::Int64
  b
::Int64
end

Ints(true, false) #=> Ints(1,0)

In both of the above cases, I want the construction to fail.

I'm aware that I could write constructors for these types to explicitly reject Bool<->Number mismatch, but that gets tedious when there are more than a few Number or Bool fields in a type. Wondering if there's an easier way.

Reply | Threaded
Open this post in threaded view
|

Re: Preventing Bool<->Number conversion/promotion

Stefan Karpinski
See #18367 and #19168.

On Mon, Oct 31, 2016 at 3:06 PM, Stefan Karpinski <[hidden email]> wrote:
At the moment no, although there's been discussion of making Bool not a subtype of Number.

On Mon, Oct 31, 2016 at 2:29 PM, Penn Taylor <[hidden email]> wrote:
Is there a way to prevent automatic conversion/promotion between Bool and Number types?

Motivating examples:

type Bools
  a
::Bool
  b
::Bool
end

Bools(1, 0) #=> Bools(true,false)

type
Ints
  a
::Int64
  b
::Int64
end

Ints(true, false) #=> Ints(1,0)

In both of the above cases, I want the construction to fail.

I'm aware that I could write constructors for these types to explicitly reject Bool<->Number mismatch, but that gets tedious when there are more than a few Number or Bool fields in a type. Wondering if there's an easier way.