Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support default value for structs, such as default(CancellationToken) #817

Open
bnuzhouwei opened this issue May 25, 2022 · 3 comments
Open

Comments

@bnuzhouwei
Copy link

bnuzhouwei commented May 25, 2022

Runtime: NET6.0
IronPython: 2.7.11

In mongodb, there are many methods with default value as default(CancellationToken)

long CountDocuments(FilterDefinition<TDocument> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken));

but in IronPython, when we call the method:

CountDocuments[BsonDocument]("collection")

got

IronPython.Runtime.Exceptions.TypeErrorException: expected CancellationToken, got NoneType

I seems that IronPython does't support default value for structs, such as default(CancellationToken)

@bnuzhouwei bnuzhouwei changed the title Default value for default(CancellationToken) Support default value for structs, such as default(CancellationToken) May 25, 2022
@slozier
Copy link
Contributor

slozier commented May 27, 2022

Thanks for the report, I managed to reproduce this with something like:

public void Test(CancellationToken token = default);

A potential fix would be to add some sort of check to DefaultArgBuilder.ToExpression in the DLR (don't think this is correct but something similar may work):

if (parameterType.IsValueType) {
    // default(T)
    return AstUtils.Constant(Activator.CreateInstance(parameterType));
}

@bnuzhouwei
Copy link
Author

Thanks, can we do this without modified the IronPython Souce Code?

DefaultArgBuilder.ToExpression

@slozier
Copy link
Contributor

slozier commented May 28, 2022

Thanks, can we do this without modified the IronPython Souce Code?

I don't have any workaround, I think you will have to explicitly pass in the argument CountDocuments[BsonDocument]("collection", cancellationToken=CancellationToken.None).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants