question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:17 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
levickicommented, Jan 29, 2022

@dsyme, @cartermp Thanks, and sorry again for off-topic.

1reaction
cartermpcommented, Aug 4, 2017

Yes, you will need to use F# 4.1. You can use the FSharp.Compiler.Tools package in your current projects, or move to Visual Studio 2017.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# method with default parameter value does not generate ...
Any code that I recompiled was able to compile and execute without any problems, even without the parameter like this:
Read more >
Named and Optional Arguments - C# Programming Guide
Named arguments in C# specify arguments by name, not position. Optional arguments can be omitted.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found