linhr opened a new issue, #11100:
URL: https://github.com/apache/datafusion/issues/11100

   ### Is your feature request related to a problem or challenge?
   
   I'm trying to implement a helper function like this in my own project, but 
it cannot compile.
   
   ```rust
   fn expr_to_func_impl<T: ScalarUDFImpl + 'static>(expr: &Expr) -> Option<&T> {
       match expr {
           Expr::ScalarFunction(ScalarFunction { func, .. }) => {
               func.inner().as_any().downcast_ref::<T>()
           }
           _ => None,
       }
   }
   ```
   
   The compiler gives the following error.
   
   ```text
   |             func.inner().as_any().downcast_ref::<T>()
   |             ------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |             |
   |             returns a value referencing data owned by the current function
   |             temporary value created here
   ```
   
   The issue is that `inner()` is defined to return a clone of `Arc` whose 
lifetime is not associated with `&self`. Therefore, it's inconvenient if 
someone has a reference to an outer `struct` and would like to cast the inner 
`Arc<dyn MyTrait>` object to `&MyTraitImpl` where `MyTraitImpl` implements 
`MyTrait`.
   
   ```rust
   impl ScalarUDF {
       pub fn inner(&self) -> Arc<dyn ScalarUDFImpl> {
           self.inner.clone()
       }
   }
   ```
   
   ### Describe the solution you'd like
   
   We should return `&Arc` to inner trait objects for `&self` methods. Many 
data structures in DataFusion already follow this pattern, and we could simply 
fix the few remaining places, such as the `ScalarUDF::inner()` method discussed 
above.
   
   ### Describe alternatives you've considered
   
   N/A
   
   ### Additional context
   
   N/A


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to