Skip to content

Add Promoted to std.traits.#4419

Merged
JackStouffer merged 1 commit intodlang:masterfrom
tsbockman:more_traits
Oct 25, 2016
Merged

Add Promoted to std.traits.#4419
JackStouffer merged 1 commit intodlang:masterfrom
tsbockman:more_traits

Conversation

@tsbockman
Copy link
Contributor

@tsbockman tsbockman commented Jun 8, 2016

Add a Promoted!T trait which evaluates to the type used for (non-unary) arithmetic operations on type T. Example: static assert(is(typeof(Promoted!ubyte == int)));.

std/traits.d Outdated
if(isScalarType!T)
{
static if (isFloatingPoint!T)
enum int precision = T.mant_dig;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ushort?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static assert(is(typeof(real.mant_dig) == int));

@tsbockman tsbockman force-pushed the more_traits branch 2 times, most recently from 8af73c7 to d9435f0 Compare June 8, 2016 16:21
std/traits.d Outdated
/**
Detect whether `T` is a scalar type that promotes to an integral type (that is, a built-in
integral, character, or boolean type).
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "fixed point" the right term for this? I would expect a number with a fractional part (with fixed precision) behind that name.

Copy link
Contributor Author

@tsbockman tsbockman Jun 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "fixed point" the right term for this?

I intended it as the opposite of isFloatingPoint, since isFixedPoint == (isScalarType && !isFloatingPoint). Do you have a suggestion for a better name?

I would expect a number with a fractional part (with fixed precision) behind that name.

All matching types have a fixed precision of zero places after the decimal point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting rationale, but the converse of floating point is not fixed point; the latter is a mere alternative for implementing approximations of real numbers. Integrals and their ilk are a distinct matter.

How frequent is this likely to be? Should it include integral enum types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting rationale, but the converse of floating point is not fixed point...

I am not particularly attached to the isFixedPoint name - I'm just waiting for someone to suggest something better: descriptive and meaningfully shorter than the implementation.

How frequent is this likely to be [used]?

When converting functions into templates, people tend to replace the parameter int a with A a ... if (isIntegral!A). The two are not fully compatible, though, because int a will accept bool, char, and wchar as well.

The range of types accepted by A a ... if (isFixedPoint!A) is much closer to those accepted by int a. In my opinion most template functions with isIntegral in the constraints are candidates for conversion to isFixedPoint, instead.

Of course, the whole thing is just a hack to work around a weakness in the language: the inability to declare a template function in such a way that type deduction works properly when implicit conversions are involved.

Should it include integral enum types?

I considered it unwise to deviate from the behaviour of the existing isWhatever traits, so it does the same thing that isIntegral does: it gives the same answer for an enum type as it would for the enum's base type.

@JackStouffer
Copy link
Contributor

LGTM

You should email Andrei as these are name additions

std/traits.d Outdated
/**
Returns the number of full binary digits of precision representable by the scalar type `T`.
*/
template precision(T)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be an example of unifying FP and integral precision?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A floating-point type F can losslessly represent all possible values of an integral type N if-and-only-if precision!T >= precision!N.

This trait is used by smartOp to help compute the optimal common numeric type for multi-argument functions like binary().

@JackStouffer
Copy link
Contributor

Anyone else?

@tsbockman
Copy link
Contributor Author

Anyone else?

Unresolved:

  • A less confusing name is needed for isFixedPoint, but none has been suggested yet.
  • @andralex has not yet made a decision about precision.

@andralex
Copy link
Member

To move forward with this PR, I suggest it be reduced to only add Promoted. I don't think precision and isFloatingPoint (under any name) add enough value. At any rate those can be added later with the appropriate evidence.

@andralex
Copy link
Member

andralex commented Sep 7, 2016

(removing my label after posting my response)

@andralex andralex removed the @andralex label Sep 7, 2016
@JackStouffer
Copy link
Contributor

Ping @tsbockman

@tsbockman tsbockman changed the title Add isFixedPoint, precision, and Promoted to std.traits. Add Promoted to std.traits. Sep 23, 2016
@tsbockman
Copy link
Contributor Author

@JackStouffer I have removed precision and isFixedPoint from this PR, added a changelog entry, and tweaked the examples slightly to make it clearer what Promoted is for.

@JackStouffer
Copy link
Contributor

Andrei has approved the addition and the code, no one else has objected, so I'm pulling

@JackStouffer
Copy link
Contributor

Auto-merge toggled on

@JackStouffer JackStouffer merged commit d999c77 into dlang:master Oct 25, 2016
@tsbockman tsbockman deleted the more_traits branch October 25, 2016 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants