2

I have some IronPython code that is creating a Mutex using the following constructor:

public Mutex(
    bool initiallyOwned,
    string name,
    out bool createdNew
)

Since the last parameter is an out parameter you do not pass it to the method and instead it becomes an extra return value like this:

mutex, sucess = Mutex(True, 'some_mutex')  

When this code runs it throws a TypeError saying that the Mutex object is not iterable. Since it is only returning one value this leads me to believe that IronPython is not choosing the correct overload. The Ironpython documentation says that you can control the exact overload that gets called by using the Overloads method on method objects.

The following bit of code attempts that, however, I get a ValueError stating that the index was out of range:

new_mutex = Mutex.__new__.Overloads[type(True), String, type(True)]
mutex, sucess = new_mutex(Mutex, True, 'some_mutex') 

If I try using the Overloads attribute to force using a different overload it executes correctly. Anyone know where I'm going wrong?

2
  • It doesn't sound like a good idea to make a constructor have out parameters. That would be better off as a factory method. Commented Jan 27, 2012 at 7:16
  • @JeffMercado, he's talking about System.Threading.Mutex's constructor here--he doesn't own the signature design for that constructor at all. Commented Jan 29, 2012 at 19:03

1 Answer 1

1

You could explicitly pass the out bool to the constructor like this:

success = clr.Reference[bool]()
mutex = Mutex(True, 'some_mutex', success)

# success.Value is your bool result

That lets the overload resolution stuff just do its thing.

I'm not quite sure how to extract the right method from __new__.Overloads, but there has to be a way. If I just say Mutex.__new__.Overloads, it shows me a list that includes the overload that you're looking for.

Sign up to request clarification or add additional context in comments.

1 Comment

didn't know you could pass in a clr Reference type like that, thanks for the tip!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.