C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Typedef

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Fri, 16 Aug 2024 13:45:15 +0200
Hi Karafiat,   while the idea to customize basic types without having to specify field names is good, the typedef syntax fell out of favour.   The typedef syntax makes type definitions like variable declarations, whereas using reverses the type and the name.       The most similar class definition would be   struct Float {     float f; }   struct Angle : public Float {     ... }   So perhaps a syntax with colon and the type name   class Angle : float { }   would be more in line, e.g. inheriting from a single basic type activates this feature?   -----Ursprüngliche Nachricht----- Von:Karafiát, László via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Fr 16.08.2024 13:33 Betreff:[std-proposals] Typedef An:std-proposals_at_[hidden]; CC:Karafiát, László <laszlo_at_[hidden]>; Dear fellows, In practice we have a good many times scalar (simple/unstructured) type of objects with pronounced domain specific behaviours. I propose a rather simple kind of type/class definition for these. 1. A typedef extension like this would suffice     // Values in radians in the range [-π,π)     typedef float Angle                                 // as before     {                                                   // NEW       float degrees()  { return  *this * 180f / π; }    // NEW       bool  isAcute()  { return abs(*this) <  /2f; }    // NEW         :         .     };                                                  // NEW     :     .     Angle α = π/6.f,                         // as before           β = π/4.f;     if (α == β) println(...);     if (α.isAcute()) println(...);            // NEW With other words,   1.a The new typedef could have a body with class-like definitions (operators, properties, constructors, *this etc).   1.b Using the object name would suffice to reference its value as with other typedefs, no extra .fieldname is necessary as it would in case of traditional classes. This brings significantly enhanced readability. The functions can be invoked like in case of classes - no syntactical news here (i.e. no confusion either). 2. It would be nice to have sg. similar for enums as well (being they scalar types too).     enum class CompassDir : short     {       None = -1,       East, NorthEast, North, NorthWest,       West, SouthWest, South, SouthEast;       // Here come the tidings ====================================       CompassDir (const Angle α)       // A constructor       { (CompassDir) (short((α.degrees()+382.5f)/45f) & 7);       }       bool       isCardinal() { return (*this & 1) == 0;          }       CompassDir opposite()   { return (CompassDir) (*this ^ 4);  }         :         .     };     :     .     Angle α = π/6.f;                   // as before     CompassDir cd = CompassDir(α);     // NEW   2.a Eventually this could also be introduced as     typedef enum CompassDir : short     {         ...   // as above     }; to stress the scalar quality of enums. When teaching the language it would be a good start to introduce these kind of type definitions before talking about classes/structs (tuples). 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-16 11:45:19