Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting rid of function's argument
    primarykey
    data
    text
    <p>It is better to explain my intention with code. So right now I have the following code:</p> <pre><code>class A, IInterfaceUsedByB { } class B { void func(A someObject) { func2(someObject, 1); func3(someObject, "string"); func4(someObject, new MyObject()); var anotherObject = someObject.NewInstance; func2(anotherObject, 1); ....................... } func2(A someObject, int val); func3(A someObject, string val); func4(A someObject, C val); } </code></pre> <p>Where <code>func2</code>, <code>func3</code>, <code>func4</code> do need references to someObject. I want to change this to</p> <pre><code>void func(A someObject) { with(someObject, () =&gt; { func2(1); func3("string"); func4(new MyObject()); } ); var anotherObject = someObject.NewInstance; with(anotherObject, () =&gt; { func2(1); ....................... } ); } </code></pre> <p>Or even better to</p> <pre><code>void func(A someObject) { func2(1); func3("string"); func4(new MyObject()); } </code></pre> <p>So that I don't have to drag this someObject around, but I should still be able to use it inside func2,3,4. In other words it's like setting some context object for a particular code block. I can use any of the three languages (C#, F# or IronPython) for this.</p> <p><strong>UPDATE</strong> In the ideal solution class B would be independent of A. <code>func*</code> functions only depend on a small interface of A consisting of 2 methods.</p> <p><strong>UPDATE2</strong> The someObject can be different for different function calls.</p> <p><strong>UPDATE3</strong> I don't think that there's a way to do this only with language primitives like classes, interfaces or extension methods. I think that the solution might be possible with more hackish tools, like attributes, reflection or F#'s computation workflow feature. Or maybe some dynamic features of IronPython. Essentially what I'm trying to do is to create a small embedded DSL.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload