Hi, Robert.

I'm sorry. Now I got it.

But I think what you are trying to do is not possible in Go yet.
@Ian already answered about this.

Have you thought of another way to do this? Maybe reading the "nPlaces" 
value from something like a configuration file and storing it as an 
encapsulated field instead of a constant?
Even reading at runtime the value of an environment variable when creating 
the structure and storing it as a field?

Are any of these questions a possibility for you?



Em terça-feira, 15 de outubro de 2024 às 23:46:36 UTC-3, Robert Engels 
escreveu:

> I think you misunderstood the question or it wasn’t clear. The struct can 
> only contain a single int64 - so you cannot add another value to hold the 
> number of decimal places. 
>
> The idea is to track the number of decimal places via the type (or method 
> on the type) so the compiler knows it while generating the code. 
>
> It’s clear in the C++ code. 
>
> On Oct 15, 2024, at 9:17 PM, Cleberson Pedreira Pauluci <
> pauluci....@gmail.com> wrote:
>
> Hi.
>
>
>
> Maybe you can use encapsulation instead of standalone function and 
> generics.
> Encapsulation is a fundamental principle in software engineering. It 
> controls which parts of a package are visible from the outside and which 
> are not.
> It hides implementation details, allowing changes that do not affect 
> dependent code.
>
> Check it out: https://go.dev/play/p/xqwMPgB0IND
>
> I really hope I can help you.
>
> Cheers.
> Cleberson
>
>
> Em terça-feira, 15 de outubro de 2024 às 21:01:42 UTC-3, Robert Engels 
> escreveu:
>
>> Hi,
>>
>> Is there someway of doing this with Go generics? 
>> https://github.com/robaho/cpp_fixed/blob/6770b217acd84e4b723449e4c4bb42c92e7f2af1/fixed.h#L18-L23
>>
>> I could make the type and interface and add a method for ‘decimal 
>> places’, but I would like all of the constants to be resolved at compile 
>> time.
>>
>> Also, if I make it an interface, it is far less efficient than passing a 
>> single int64 with generics and letting the compiler figure it out.
>>
>> I know constexpr is not available, but I would think that if I could do 
>> it with methods the compiler would eventually inline the function calls 
>> anyway - possibly as constants.
>>
>> I read through the docs, and it doesn’t seem possible.
>>
>> I got it kind of working here https://go.dev/play/p/5ztum5NAkUp but I 
>> would really like it to be method based rather than standalone function 
>> based, and the method based I would seemingly need to duplicate a lot of 
>> code for every possible number of decimal places.
>>
>> What am I missing? Ideas?
>>
>> Thanks for the help.
>>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/fb1a8dd2-e1d6-499d-aabb-683afc8720a1n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/fb1a8dd2-e1d6-499d-aabb-683afc8720a1n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/95a32855-d970-4012-a9a8-5e75b7467a1cn%40googlegroups.com.

Reply via email to