You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Trying to use a custom function in Output I am getting the following exception:
System.Exception: Cannot convert output type.
at Fli.CE.ICommandContext`1.outputTypeMapping[a,a](ICommandContext`1 _, a output)
at Fli.CE.ICommandContext`1.Output[a,a](ICommandContext`1 this, ICommandContext`1 context, a output)
Ok, I can see what you've done here. It's "generic". Why not make it type-safe?
EDIT: Sorry for my initial harsh response. It was late... I think I've got confused by the docs stating:
Provided Fli.Outputs:
File of string a string with an absolute path of the output file.
StringBuilder of StringBuilder a StringBuilder which will be filled with the output text.
Custom of Func<string, unit> a custom function (string -> unit) that will be called with the output string (logging, printing etc.).
So I used Output (Custom(Func<string,unit>(fun s -> ... which is not supported by outputTypeMapping.
Rather than doing runtime casts - couldn't be multiple output types solved via separate overloads of the Output custom operation in the CE?
@CaptnCodr I was thinking of something along the lines of the below. Essentially you can define Output multiple times with different types. That way you avoid casting on runtime and also the user can't get it wrong because the compiler will prevent it. Also the user doesn't need to use DU but can still conveniently just pass a value of an actual type. Another also, it would be a non-breaking API change too as any not supported types would be already throwing an exception.
namespaceFli[<AutoOpen>]moduleCE =openSystem.TextopenDomaintypeICommandContext<'a>withmemberthis.Yield(_)= this
typeStartingContext={ config:Config option }memberthis.CurrentConfig= this.config |> Option.defaultValue Defaults
interface ICommandContext<StartingContext>withmemberthis.Context= this
letcli={ config = None }/// Extensions for Shell context.typeICommandContext<'a>with/// `Shell` opening keyword, which `Fli.Shell` shall be used.[<CustomOperation("Shell")>]member_.Shell(context:ICommandContext<StartingContext>,shell)=
Cli.shell shell context.Context.CurrentConfig
/// Which `Command` should be executed in the `Shell`.[<CustomOperation("Command")>]member_.Command(context:ICommandContext<ShellContext>,command)= Cli.command command context.Context
/// `Input` string(s) that can be used to interact with the shell.[<CustomOperation("Input")>]member_.Input(context:ICommandContext<ShellContext>,input)= Cli.input input context.Context
/// Extra `Output` that is being executed immediately after getting output from execution.[<CustomOperation("Output")>]memberthis.Output(context:ICommandContext<ShellContext>,filePath:string)=
Cli.output (File filePath) context.Context
[<CustomOperation("Output")>]memberthis.Output(context:ICommandContext<ShellContext>,func:string ->unit)=
Cli.output (Custom func) context.Context
[<CustomOperation("Output")>]memberthis.Output(context:ICommandContext<ShellContext>,builder:StringBuilder)=
Cli.output (StringBuilder builder) context.Context
/// Current executing `working directory`.[<CustomOperation("WorkingDirectory")>]member_.WorkingDirectory(context:ICommandContext<ShellContext>,workingDirectory)=
Cli.workingDirectory workingDirectory context.Context
/// One tupled `EnvironmentVariable`.[<CustomOperation("EnvironmentVariable")>]member_.EnvironmentVariable(context:ICommandContext<ShellContext>,environmentVariable)=
Cli.environmentVariables [ environmentVariable ] context.Context
/// A list of tupled `EnvironmentVariables`.[<CustomOperation("EnvironmentVariables")>]member_.EnvironmentVariables(context:ICommandContext<ShellContext>,environmentVariables)=
Cli.environmentVariables environmentVariables context.Context
/// `Encoding` that'll be used for `Input`, 'Output.Text' and `Output.Error`.[<CustomOperation("Encoding")>]member_.Encoding(context:ICommandContext<ShellContext>,encoding)= Cli.encoding encoding context.Context
/// Extensions for Exec context.typeICommandContext<'a>with/// `Exec` opening keyword, which binary/executable shall be started.[<CustomOperation("Exec")>]member_.Exec(context:ICommandContext<StartingContext>,program)=
Program.program program context.Context.CurrentConfig
/// `Arguments` that will be passed into the executable.[<CustomOperation("Arguments")>]member_.Arguments(context:ICommandContext<ExecContext>,arguments)=letmatchArguments arguments =match box (arguments)with|:? string as s -> s
|:? seq<string>as s -> s |> Seq.map string |> String.concat ""|_-> failwith "Cannot convert arguments to a string!"
Program.arguments (matchArguments arguments) context.Context
/// `Input` string(s) that can be used to interact with the executable.[<CustomOperation("Input")>]member_.Input(context:ICommandContext<ExecContext>,input)= Program.input input context.Context
/// Extra `Output` that is being executed immediately after getting output from execution.[<CustomOperation("Output")>]memberthis.Output(context:ICommandContext<ExecContext>,filePath:string)=
Program.output (File filePath) context.Context
[<CustomOperation("Output")>]memberthis.Output(context:ICommandContext<ExecContext>,func:string ->unit)=
Program.output (Custom func) context.Context
[<CustomOperation("Output")>]memberthis.Output(context:ICommandContext<ExecContext>,builder:StringBuilder)=
Program.output (StringBuilder builder) context.Context
/// Current executing `working directory`.[<CustomOperation("WorkingDirectory")>]member_.WorkingDirectory(context:ICommandContext<ExecContext>,workingDirectory)=
Program.workingDirectory workingDirectory context.Context
/// `Verb` keyword that can be used to start the executable.[<CustomOperation("Verb")>]member_.Verb(context:ICommandContext<ExecContext>,verb)= Program.verb verb context.Context
/// Start the executable with another `Username`.[<CustomOperation("Username")>]member_.UserName(context:ICommandContext<ExecContext>,userName)=
Program.userName userName context.Context
/// Start the executable with other `Credentials`./// Hint: `Domain` and `Password` are available for Windows systems only.[<CustomOperation("Credentials")>]member_.Credentials(context:ICommandContext<ExecContext>,credentials)=let(domain,user,pw)= credentials in Program.credentials (Credentials(domain, user, pw)) context.Context
/// One tupled `EnvironmentVariable`.[<CustomOperation("EnvironmentVariable")>]member_.EnvironmentVariable(context:ICommandContext<ExecContext>,environmentVariable)=
Program.environmentVariables [ environmentVariable ] context.Context
/// A list of tupled `EnvironmentVariables`.[<CustomOperation("EnvironmentVariables")>]member_.EnvironmentVariables(context:ICommandContext<ExecContext>,environmentVariables)=
Program.environmentVariables environmentVariables context.Context
/// `Encoding` that'll be used for `Input`, 'Output.Text' and `Output.Error`.[<CustomOperation("Encoding")>]member_.Encoding(context:ICommandContext<ExecContext>,encoding)=
Program.encoding encoding context.Context
@queil Thanks for sharing. I was pretty sure that CustomOperations can't share the same name, but I was wrong.
This change comes in the next version + I amended some docs about this.
Activity
queil commentedon May 26, 2023
Ok, I can see what you've done here. It's "generic". Why not make it type-safe?EDIT: Sorry for my initial harsh response. It was late... I think I've got confused by the docs stating:
So I used
Output (Custom(Func<string,unit>(fun s -> ...which is not supported byoutputTypeMapping.Rather than doing runtime casts - couldn't be multiple output types solved via separate overloads of the Output custom operation in the CE?
CaptnCodr commentedon May 27, 2023
Hi @queil,
I wanted to dig into your issue and saw your recent edit. Good that you found the correct overload now.
Do you have something in mind or a good example of what exactly you mean? To force the user to use the DU as you thought in your opening comment?
queil commentedon May 27, 2023
I'll try to put an example together in the evening.
queil commentedon May 27, 2023
@CaptnCodr I was thinking of something along the lines of the below. Essentially you can define Output multiple times with different types. That way you avoid casting on runtime and also the user can't get it wrong because the compiler will prevent it. Also the user doesn't need to use DU but can still conveniently just pass a value of an actual type. Another also, it would be a non-breaking API change too as any not supported types would be already throwing an exception.
CaptnCodr commentedon May 27, 2023
@queil Thanks for sharing. I was pretty sure that CustomOperations can't share the same name, but I was wrong.
This change comes in the next version + I amended some docs about this.
queil commentedon May 27, 2023
I think it was implemented in dotnet/fsharp#9400
queil commentedon May 27, 2023
Thanks!