Date: Wed, 12 Mar 2025 18:08:07 -0500
> It would looks like a mess:
Not really. You're free to format your code as a mess or not, simply
putting enum constants and member functions together separated by a newline
would be plenty clean.
> Or just allow to assign a function to a value.
> Second solution for me looks better.
Except it would be very confusing. You want to use the same syntax as enum
constants, but not for an enum constant.
Cheers,
Jeremy
On Wed, Mar 12, 2025 at 5:46 PM Dmitrii Shabalin via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> Hi Tom.
>
> enum class E {
> A, B, C
> std::string_view to_string(E value) { … }
> }
>
> It would looks like a mess:
>
> enum class E {
> A = 1, B, C = 3
> std::string_view to_string(E value) { … }
> D = A, E = calcE("E"), F
> E from_string(std::string value) { … }
> G, H, max = H
> bool IsValueExists(E Value)
> }
>
> So, you need a clear keyword to separate enumeration from functions. Or
> just allow to assign a function to a value.
> Second solution for me looks better.
>
> Kind regards,
> Dmitrii shabalin
>
>
>
> чт, 13 мар. 2025 г. в 06:04, Tom Honermann via Std-Proposals <
> std-proposals_at_[hidden]>:
>
>> On 3/11/25 10:23 PM, Jeremy Rifkin via Std-Proposals wrote:
>>
>> Hi,
>>
>> > Sorry, Jeremy, but there is no straight way to add a normal function
>> into enum.
>> The problem is: enum is enumeration. If you stop enumeration for a
>> function, it would break the logic of the enum.
>>
>> An implementation developer can weight in about challenge with adding
>> this to existing implementations but there is no reason C++ cannot support
>> this. To elaborate on what I mean, imagine syntax along the following lines:
>>
>> enum class E {
>> A, B, C
>> std::string_view to_string(E value) { … }
>> }
>>
>> E e = E::A;
>> e.to_string();
>>
>> I would use deducing this to support member function call syntax.
>>
>> enum class E {
>> A, B, C;
>> std::string_view to_string(this E value) { … }
>> };
>> E e = E::A;
>> auto name = e.to_string();
>>
>> Tom.
>>
>>
>>
>> Cheers,
>> Jeremy
>>
>>
>> On Tue, Mar 11, 2025 at 20:23 Dmitrii Shabalin via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>> Hi.
>>> Sorry, Jeremy, but there is no straight way to add a normal function
>>> into enum.
>>> The problem is: enum is enumeration. If you stop enumeration for a
>>> function, it would break the logic of the enum.
>>>
>>> For me, the only way is accepting a value as a function.
>>>
>>> Actually, it might work too:
>>>
>>> std::string ToString( Test test) { if (value == testValue) return
>>> "testValue"; else return ""; }
>>>
>>> enum Test{
>>> testValue = 0,
>>> toString = ToString,
>>> max = testValue
>>> }
>>>
>>> std::cout << "Test value" << Test::toString(Test::testValue);
>>>
>>> but as you can see, it causes having a function in the different
>>> namespace.
>>>
>>> If you have any idea how to have a function inside in the enum, please,
>>> share your idea with an example.
>>>
>>> Kind regards,
>>> Dmitrii shabalin
>>>
>>>
>>> ср, 12 мар. 2025 г. в 12:32, Jeremy Rifkin via Std-Proposals <
>>> std-proposals_at_[hidden]>:
>>>
>>>> Hi,
>>>>
>>>> It seems weird to add a special-case to just allow lambdas in enums
>>>> like this. The syntax is confusing too, it resembles enum constant syntax
>>>> and this isn't syntax that exists for other similar language constructs.
>>>>
>>>> I would much rather see enums get normal member functions like most
>>>> other languages have.
>>>>
>>>> Cheers,
>>>> Jeremy
>>>>
>>>>
>>>> On Tue, Mar 11, 2025 at 6:20 PM Dmitrii Shabalin via Std-Proposals <
>>>> std-proposals_at_[hidden]> wrote:
>>>>
>>>>> Hi.
>>>>> Since C++11 we have lambdas.
>>>>> I believe, everyone, who works with enums, write some functions for
>>>>> this particular enum only.
>>>>> However, there is no way to associate this function with the enum's
>>>>> namespace.
>>>>> My idea: we can set a value for any of enum fields. It should be const
>>>>> and could be constexpr.
>>>>> May be, we can set a lambda for a field too?
>>>>> something like:
>>>>> enum Test{
>>>>> testValue = 0,
>>>>> toString =[](Test value) -> std::string { if (value == testValue)
>>>>> return "testValue"; else return ""; },
>>>>> max = testValue
>>>>> }
>>>>>
>>>>> And usage will be something like:
>>>>> std::cout << "Test value" << Test::toString(Test::testValue);
>>>>> --
>>>>> Std-Proposals mailing list
>>>>> Std-Proposals_at_[hidden]
>>>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>>>
>>>> --
>>>> Std-Proposals mailing list
>>>> Std-Proposals_at_[hidden]
>>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>>
>>> --
>>> Std-Proposals mailing list
>>> Std-Proposals_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Not really. You're free to format your code as a mess or not, simply
putting enum constants and member functions together separated by a newline
would be plenty clean.
> Or just allow to assign a function to a value.
> Second solution for me looks better.
Except it would be very confusing. You want to use the same syntax as enum
constants, but not for an enum constant.
Cheers,
Jeremy
On Wed, Mar 12, 2025 at 5:46 PM Dmitrii Shabalin via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> Hi Tom.
>
> enum class E {
> A, B, C
> std::string_view to_string(E value) { … }
> }
>
> It would looks like a mess:
>
> enum class E {
> A = 1, B, C = 3
> std::string_view to_string(E value) { … }
> D = A, E = calcE("E"), F
> E from_string(std::string value) { … }
> G, H, max = H
> bool IsValueExists(E Value)
> }
>
> So, you need a clear keyword to separate enumeration from functions. Or
> just allow to assign a function to a value.
> Second solution for me looks better.
>
> Kind regards,
> Dmitrii shabalin
>
>
>
> чт, 13 мар. 2025 г. в 06:04, Tom Honermann via Std-Proposals <
> std-proposals_at_[hidden]>:
>
>> On 3/11/25 10:23 PM, Jeremy Rifkin via Std-Proposals wrote:
>>
>> Hi,
>>
>> > Sorry, Jeremy, but there is no straight way to add a normal function
>> into enum.
>> The problem is: enum is enumeration. If you stop enumeration for a
>> function, it would break the logic of the enum.
>>
>> An implementation developer can weight in about challenge with adding
>> this to existing implementations but there is no reason C++ cannot support
>> this. To elaborate on what I mean, imagine syntax along the following lines:
>>
>> enum class E {
>> A, B, C
>> std::string_view to_string(E value) { … }
>> }
>>
>> E e = E::A;
>> e.to_string();
>>
>> I would use deducing this to support member function call syntax.
>>
>> enum class E {
>> A, B, C;
>> std::string_view to_string(this E value) { … }
>> };
>> E e = E::A;
>> auto name = e.to_string();
>>
>> Tom.
>>
>>
>>
>> Cheers,
>> Jeremy
>>
>>
>> On Tue, Mar 11, 2025 at 20:23 Dmitrii Shabalin via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>> Hi.
>>> Sorry, Jeremy, but there is no straight way to add a normal function
>>> into enum.
>>> The problem is: enum is enumeration. If you stop enumeration for a
>>> function, it would break the logic of the enum.
>>>
>>> For me, the only way is accepting a value as a function.
>>>
>>> Actually, it might work too:
>>>
>>> std::string ToString( Test test) { if (value == testValue) return
>>> "testValue"; else return ""; }
>>>
>>> enum Test{
>>> testValue = 0,
>>> toString = ToString,
>>> max = testValue
>>> }
>>>
>>> std::cout << "Test value" << Test::toString(Test::testValue);
>>>
>>> but as you can see, it causes having a function in the different
>>> namespace.
>>>
>>> If you have any idea how to have a function inside in the enum, please,
>>> share your idea with an example.
>>>
>>> Kind regards,
>>> Dmitrii shabalin
>>>
>>>
>>> ср, 12 мар. 2025 г. в 12:32, Jeremy Rifkin via Std-Proposals <
>>> std-proposals_at_[hidden]>:
>>>
>>>> Hi,
>>>>
>>>> It seems weird to add a special-case to just allow lambdas in enums
>>>> like this. The syntax is confusing too, it resembles enum constant syntax
>>>> and this isn't syntax that exists for other similar language constructs.
>>>>
>>>> I would much rather see enums get normal member functions like most
>>>> other languages have.
>>>>
>>>> Cheers,
>>>> Jeremy
>>>>
>>>>
>>>> On Tue, Mar 11, 2025 at 6:20 PM Dmitrii Shabalin via Std-Proposals <
>>>> std-proposals_at_[hidden]> wrote:
>>>>
>>>>> Hi.
>>>>> Since C++11 we have lambdas.
>>>>> I believe, everyone, who works with enums, write some functions for
>>>>> this particular enum only.
>>>>> However, there is no way to associate this function with the enum's
>>>>> namespace.
>>>>> My idea: we can set a value for any of enum fields. It should be const
>>>>> and could be constexpr.
>>>>> May be, we can set a lambda for a field too?
>>>>> something like:
>>>>> enum Test{
>>>>> testValue = 0,
>>>>> toString =[](Test value) -> std::string { if (value == testValue)
>>>>> return "testValue"; else return ""; },
>>>>> max = testValue
>>>>> }
>>>>>
>>>>> And usage will be something like:
>>>>> std::cout << "Test value" << Test::toString(Test::testValue);
>>>>> --
>>>>> Std-Proposals mailing list
>>>>> Std-Proposals_at_[hidden]
>>>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>>>
>>>> --
>>>> Std-Proposals mailing list
>>>> Std-Proposals_at_[hidden]
>>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>>
>>> --
>>> Std-Proposals mailing list
>>> Std-Proposals_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Received on 2025-03-12 23:08:22