C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Scalar Inheritance and other Joys

From: Jeremy Rifkin <rifkin.jer_at_[hidden]>
Date: Wed, 28 Aug 2024 09:47:41 -0500
Would class Angle : float be implicitly convertible to a float or float&?

Jeremy

On Wed, Aug 28, 2024 at 09:02 Karafiát, László via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Dear Fellows,
>
> I admit, my first draft was not clear enough.
>
>
> 1. Now I give it another try.
>
> Motivation
> Out on the brown fields (of practice) we often need objects with simple,
> un_struct_ured (i.e. scalar) values but domain specific behaviour and/or
> constraints.
>
> Proposal
> It would be theoretically as well as practically a useful round-up if
> one could define such, rather simple classes like in the example below:
>
> Definition
> class Angle final : float
> { // Values in radians in the range [-π,π)
> public:
> float degrees() { return *this * 180.f / π; }
> bool isAcute() { return abs(*this) < π/2.f; }
> Angle complementary() { return π/2.f - *this; }
> Angle supplementary() { return π - *this; }
> :
> .
> };
>
>
> :
> .
>
> Usage
> Angle α = π/6.f,
> β = π/4.f;
> if (α == β) println(...);
>
> if (α.isAcute()) println(...);
>
> The syntax is like for any struct-ed class except that
> . We can name now a built-in scalar type as base/super class
> . The objects would inherit all the operations and casts
> (but those could be hidden as private if necessary).
> . No field definitions are allowed (that would be a struct).
> . When using objects of these classes the object name refers
> to the (only) value i.e. no dot notation is needed.
>
> #####################################################
>
> 2.Other remarks/ideas on readability
>
> 2.a Reading the τ (tau) discussion, eventually a new set of math
> constants could be defined using UTF characters. This would allow a new
> kind of style on source texts for those who mind (using e.g. ⅇ, π, ∂, ⅈ,
> Γ, etc.).
>
> 2.b Similarly, some of the basic mathematical types could be defined like
>
> ℕ 2115 natural (unsigned int)
> ℤ 2124 int
> ℚ 211A rational (someday, maybe)
> ℂ 2102 complex :
> ℝ 211D float and double
>
> Not just shorter but more readable too as we have seen these letters in
> school already. (And what is that 'unsigned int' botch anyhow?)
> One could use an index for the size: ℕ₂ for unsigned short, ℤ₈ for long
> long int, ℝ₄ for float and ℝ₈ for double etc. Letters without index
> refer to the implementation default. (The IEEE float and double
> representations are not reals having limited value ranges and precision
> of course, but we don't want to open that discussion here, do we).
>
> So the example in the 1st proposal would start with
>
> class Angle final : ℝ₄
> {
>
> One could define these with typedefs of course, but typedefs are pariahs
> now. And anyhow, if the standard provides it: Small leap for a
> programming language...
>
>
> Best regards
> László Karafiát
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-08-28 14:47:54