www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - I need my struct to convertable to 'wchar'

reply Zoda <zoda vuhuv.com> writes:
Hi, I need my struct to convertable to 'wchar' my struct looks 
like this
```d
struct ColoredChar {
     int[3] rgb;
     wchar ch;
}

// Existing functions looks like this
void doSomething(wchar ch) {
     ...
}
```
i don't want my existing functions to break so i need my struct 
to be convertable to a 'wchar', how can achieve this?

Thanks, Zoda.
Jun 17
next sibling parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Tuesday, 17 June 2025 at 19:15:33 UTC, Zoda wrote:
 Hi, I need my struct to convertable to 'wchar' my struct looks 
 like this
 ```d
 struct ColoredChar {
     int[3] rgb;
     wchar ch;
 }

 // Existing functions looks like this
 void doSomething(wchar ch) {
     ...
 }
 ```
 i don't want my existing functions to break so i need my struct 
 to be convertable to a 'wchar', how can achieve this?

 Thanks, Zoda.
``` import std; struct ColoredChar { int[3] rgb; wchar ch(){ return 'a'; } alias ch this; } // Existing functions looks like this void doSomething(wchar ch) { ch.writeln; } unittest{ ColoredChar().doSomething; } ```
Jun 17
parent Zoda <zoda vuhuv.com> writes:
On Tuesday, 17 June 2025 at 19:29:16 UTC, monkyyy wrote:
 On Tuesday, 17 June 2025 at 19:15:33 UTC, Zoda wrote:
 Hi, I need my struct to convertable to 'wchar' my struct looks 
 like this
 ```d
 struct ColoredChar {
     int[3] rgb;
     wchar ch;
 }

 // Existing functions looks like this
 void doSomething(wchar ch) {
     ...
 }
 ```
 i don't want my existing functions to break so i need my 
 struct to be convertable to a 'wchar', how can achieve this?

 Thanks, Zoda.
``` import std; struct ColoredChar { int[3] rgb; wchar ch(){ return 'a'; } alias ch this; } // Existing functions looks like this void doSomething(wchar ch) { ch.writeln; } unittest{ ColoredChar().doSomething; } ```
Thanks, i works but can i want to do something like ColoredChar([255,0,0], '█') can its possible? Thanks again, Zoda.
Jun 17
prev sibling parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Tue, Jun 17, 2025 at 07:15:33PM +0000, Zoda via Digitalmars-d wrote:
 Hi, I need my struct to convertable to 'wchar' my struct looks like this
 ```d
 struct ColoredChar {
     int[3] rgb;
     wchar ch;
 }
 
 // Existing functions looks like this
 void doSomething(wchar ch) {
     ...
 }
 ```
 i don't want my existing functions to break so i need my struct to be
 convertable to a 'wchar', how can achieve this?
[...] ```d struct ColoredChar { int[3] rgb; wchar ch; alias ch this; } ``` --T
Jun 17
parent reply Zoda <zoda vuhuv.com> writes:
On Tuesday, 17 June 2025 at 19:31:30 UTC, H. S. Teoh wrote:
 On Tue, Jun 17, 2025 at 07:15:33PM +0000, Zoda via 
 Digitalmars-d wrote:
 Hi, I need my struct to convertable to 'wchar' my struct looks 
 like this
 ```d
 struct ColoredChar {
     int[3] rgb;
     wchar ch;
 }
 
 // Existing functions looks like this
 void doSomething(wchar ch) {
     ...
 }
 ```
 i don't want my existing functions to break so i need my 
 struct to be
 convertable to a 'wchar', how can achieve this?
[...] ```d struct ColoredChar { int[3] rgb; wchar ch; alias ch this; } ``` --T
Oh thanks thats works just fine now. Thanks for you answer.
Jun 17
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Tue, Jun 17, 2025 at 07:36:27PM +0000, Zoda via Digitalmars-d wrote:
 On Tuesday, 17 June 2025 at 19:31:30 UTC, H. S. Teoh wrote:
[...]
 ```d
 struct ColoredChar {
 	int[3] rgb;
 	wchar ch;
 	alias ch this;
 }
 ```
[...]
 Oh thanks thats works just fine now. Thanks for you answer.
Glad it works. But you should know that `alias this` is generally regarded as an anti-pattern; it's great for a quick hack to get things done, but in the long term, you should probably refactor your code to avoid it. T -- If you want to solve a problem, you need to address its root cause, not just its symptoms. Otherwise it's like treating cancer with Tylenol...
Jun 17
parent Zoda <zoda vuhuv.com> writes:
On Tuesday, 17 June 2025 at 21:39:59 UTC, H. S. Teoh wrote:
 On Tue, Jun 17, 2025 at 07:36:27PM +0000, Zoda via 
 Digitalmars-d wrote:
 On Tuesday, 17 June 2025 at 19:31:30 UTC, H. S. Teoh wrote:
[...]
 ```d
 struct ColoredChar {
 	int[3] rgb;
 	wchar ch;
 	alias ch this;
 }
 ```
[...]
 Oh thanks thats works just fine now. Thanks for you answer.
Glad it works. But you should know that `alias this` is generally regarded as an anti-pattern; it's great for a quick hack to get things done, but in the long term, you should probably refactor your code to avoid it. T
I'm currently working on refactoring my code. Thanks again, Zoda.
Jun 18