Labeled parameters - Syntax question

Hi there,

out of curiostiy, is it possible in Oxygene to do calls to the “File” method with paramters like this?

Example in C#

var log = new LoggerConfiguration()
    .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
    .CreateLogger();

Oxidizer converts it to this:

var log := new LoggerConfiguration()
    .WriteTo.File("log.txt") rollingInterval(RollingInterval.Day)
    .CreateLogger();

This doesen’t work, compiler throws the following error:

Parameter labels do not match. Parameter 2 is labeled “rollingInterval” but should be unlabeled in call to “class method File(path: String; restrictedToMinimumLevel: LogEventLevel; outputTemplate: String; formatProvider: IFormatProvider; fileSizeLimitBytes: Int64; levelSwitch: LoggingLevelSwitch; buffered: Boolean; shared: Boolean; flushToDiskInterval: nullable TimeSpan; rollingInterval: RollingInterval; rollOnFileSizeLimit: Boolean; retainedFileCountLimit: nullable Int32; encoding: System.Text.Encoding; hooks: Serilog.Sinks.File.FileLifecycleHooks; retainedFileTimeLimit: nullable TimeSpan): LoggerConfiguration”

The “File” method “looks” like this:

  class method File(path: String; restrictedToMinimumLevel: LogEventLevel; outputTemplate: String; formatProvider: IFormatProvider; fileSizeLimitBytes: Int64; levelSwitch: LoggingLevelSwitch; buffered: Boolean; shared: Boolean; flushToDiskInterval: nullable TimeSpan; rollingInterval: RollingInterval; rollOnFileSizeLimit: Boolean; retainedFileCountLimit: nullable Int32; encoding: System.Text.Encoding; hooks: Serilog.Sinks.File.FileLifecycleHooks; retainedFileTimeLimit: nullable TimeSpan): LoggerConfiguration

Thanks in advance.

Update:
Adding some braces and a comma seem to help…

var log := new LoggerConfiguration()
      .WriteTo.File("log.txt", rollingInterval(RollingInterval.Day))
      .CreateLogger();

This seem to work - a warning is thrown regarding the case of “rollingInterval”. If i change it, THIS example works now.

That’s not the same thing: the C# code gives the name of the parameter and the Oxygene code makes a cast!

Yes, it does. But somehow the value is assigned to the correct value doing the call. :confused:

(This means, rollingInterval is changed to “Day” in the called method in this example. Maybe the compiler is able to assign it to the correct parameter if it is unambiguous.)

This code is also working, without the unneeded cast.

Hmm,. that is not a c# syntax I was even aware of. you are right, this SHOULD be callable ad

so that seems like a buf. I will log.

Out of curiosity: in C#, can you call this with and without thew name, or does it require the name? how is this method declared on the c# side?

Logged as bugs://E25741.

Providing a name allows to give only needed parameters to the method, all other values are set to their default values in the method declaration. If i omit the name, i must follow the order, i think. But C# is not mine.

This is one of the called methods…

public static class FileLoggerConfigurationExtensions
{
    public static LoggerConfiguration File(
        this LoggerSinkConfiguration sinkConfiguration,
        string path,
        LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
        string outputTemplate = DefaultOutputTemplate,
        IFormatProvider? formatProvider = null,
        long? fileSizeLimitBytes = DefaultFileSizeLimitBytes,
        LoggingLevelSwitch? levelSwitch = null,
        bool buffered = false,
        bool shared = false,
        TimeSpan? flushToDiskInterval = null,
        RollingInterval rollingInterval = RollingInterval.Infinite,
        bool rollOnFileSizeLimit = false,
        int? retainedFileCountLimit = DefaultRetainedFileCountLimit,
        Encoding? encoding = null,
        FileLifecycleHooks? hooks = null,
        TimeSpan? retainedFileTimeLimit = null)
    {
		// -- 8< -- snipped -- 8< --
    }
}

Examples of valid calls:

var log = new LoggerConfiguration()
  .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
  .CreateLogger();

var log = new LoggerConfiguration()
  .WriteTo.File("log.txt", shared: true)    
  .CreateLogger();

var log = new LoggerConfiguration()
  .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day, retainedFileCountLimit: null)
  .CreateLogger();

Examples from here; GitHub - serilog/serilog-sinks-file: Write Serilog events to files in text and JSON formats, optionally rolling on time or size

To test it you need at least nuget-package “Serilog” and “Serilog.Sinks.File”.

1 Like

Named and Optional Arguments - C# Programming Guide | Microsoft Docs

2 Likes

Ah ok. this is not supported int Oxygene then, as rthisis distinct from named partial method names, which Oxygene and RemObjects C# support, ie:

void foo(string a = "y", int b)

vs

void bar(string a) andAnIntegerToo(int b)

would be allowed to be called

foo("x", 5);
foo(b = 5);
bar("x") andAnIntegerToo(5);

but the middle syntax has no representation in Oxygene. however, ion Oxygene you can omit parameters that have defaults, even if they ar ion the middle of the list, provided there is no ambiguity, so

foo(5);

would work in Oxygene (but explicitly and by design not in C#), because it is clear that “5” only qualifies for the b parameter.

bugs://E25741 was closed as won’t fix.