Skip to content

[<DefaultParameterValue>] is ignored in F#-authored code #96

@KevinRansom

Description

@KevinRansom

Originally opened at CodePlex latkin

Reported by Tomas P via fsbugs.

The F# compiler ignores the [<DefaultParameterValue>] attribute. For example, the following always prints null when called by C# code (calling from F# without specifying message is not supported):

open System
open System.Runtime.InteropServices
type C = 
  static member Foo([<Optional; DefaultParameterValue("Hello world")>] message) =
         printfn "%s" message

Analysis from Don:

This is correct, in F# 3.0 and F# 3.1 DefaultParameterValue is ignored for F#-authored code, both by F# consumers and .NET consumers of F# code.

The relevant code is infos.fs GetParamAttribs, where we look for OptionalArgumentAttribute and not DefaultParameterValueAttribute:

        | FSMeth(g,_,vref,_) -> 
            vref 
            |> ArgInfosOfMember g 
            |> List.mapSquared (fun (ty,argInfo) -> 
                let isParamArrayArg = HasFSharpAttribute g g.attrib_ParamArrayAttribute argInfo.Attribs
                let isOutArg = HasFSharpAttribute g g.attrib_OutAttribute argInfo.Attribs && isByrefTy g ty
                let isOptArg = HasFSharpAttribute g g.attrib_OptionalArgumentAttribute argInfo.Attribs
                // Note: can't specify caller-side default arguments in F#, by design (default is specified on the callee-side) 
                let optArgInfo = if isOptArg then CalleeSide else NotOptional
                (isParamArrayArg,isOutArg,optArgInfo))

Likewise in ilxgen.fs, we never generate a “Default” value in the Abstract IL metadata for an F# assembly. There is even a “TODO” marker there...

       let param = 
            { Name=nmOpt;
              Type= ilArgTy;  
              Default=None; (* REVIEW: support "default" attributes *)   
              Marshal=Marshal; 
              IsIn=inFlag;    
              IsOut=outFlag;  
              IsOptional=optionalFlag; 
              CustomAttrs= mkILCustomAttrs (GenAttrs cenv eenv attribs) }

We should fix this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions