www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - struct opCall - use cases

reply Brother Bill <brotherbill mail.com> writes:
What are some use cases where struct opCall would be useful?
As far as I know, this capability is unique to D.  Do any other 
languages support this?
Aug 08
next sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Fri, Aug 08, 2025 at 02:11:48PM +0000, Brother Bill via Digitalmars-d-learn
wrote:
 What are some use cases where struct opCall would be useful?
When the struct is intended to be a function object. I.e., a type that abstracts a function.
 As far as I know, this capability is unique to D.  Do any other
 languages support this?
C++. Not that that's a good example of what a language should do. :-D T -- What did the epileptic have for lunch? Seizure salad.
Aug 08
prev sibling next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Friday, 8 August 2025 at 14:11:48 UTC, Brother Bill wrote:
 What are some use cases where struct opCall would be useful?
 As far as I know, this capability is unique to D.  Do any other 
 languages support this?
Glue code, someone was fairly good about trying to hide all implementation details, contrary to popular belief, syntax sugar has syntactic meaning. 95% of the base cases of the fundamental syntax are "scary hidden control flow" escape hatches. In the most exterme cases you can nest structs with opDispatch, opCall is fairly mild mannered bit of meta programming hacking.
Aug 08
prev sibling parent Bradley Chatha <sealabjaster gmail.com> writes:
On Friday, 8 August 2025 at 14:11:48 UTC, Brother Bill wrote:
 What are some use cases where struct opCall would be useful?
 As far as I know, this capability is unique to D.  Do any other 
 languages support this?
I've used it my Lua wrapper to give it a slightly more natural interface when calling Lua functions from D: https://github.com/BradleyChatha/lumars/blob/master/source/lumars/function_.d#L138
Aug 08