[IronPython] DefaultParameterValue for keyword arguments

Dino Viehland dinov at exchange.microsoft.com
Thu Apr 24 16:57:45 CEST 2008

Try using [Optional] instead.  It lets you specify that an argument isn't required but doesn't require you to provide the default value - which in this case does appear to be impossible.  Then when calling from IronPython you'll get a nullable w/o a value if the parameter isn't provided.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Korbinian Abenthum
Sent: Thursday, April 24, 2008 3:33 AM
To: Discussion of IronPython
Subject: [IronPython] DefaultParameterValue for keyword arguments


I have a problem declaring a C# method with Nullable parameters having
DefaultParameterValue attributes:

public void SomeMethod([DefaultParameterValue(1f)] float f) { } //works
public void SomeMethod([DefaultParameterValue(1f)] float? f) { } //fails
public void SomeMethod([DefaultParameterValue(null)] int? i) { } //fails
public void SomeMethod([DefaultParameterValue((byte?)1)] byte? f) { } //fails

The second and third fail with Compiler Error CS1908: "The type of the
argument to the DefaultValue attribute must match the parameter type",
the last one with  CS0182: "An attribute argument must be a constant
expression, typeof expression or array creation expression of an
attribute parameter type"

Is that a bug in the compiler that he doesn't recognize "1f" or "null"
as valid values of "float?", or am I making a mistake?

I realize that it's not exactly OT, but I guess that many IronPython
users will use DefaultParameterValue attributes to get nice pythonic
C# methods exposed to IronPython, and maybe one of you has already
encountered this problem?

Best regards,
Users mailing list
Users at lists.ironpython.com

More information about the Ironpython-users mailing list