How do you write a generic F# delegate declaration? - f#

So, how do you write a generic delegate declaration in f#?
I want to get the equivalent of this c# declaration:
public delegate Result<TOutput> Parser<TInput, TValue>(TInput input);
I haven't been able to find documentation on how this might be achieved, even though it's a quite common to have to write generic delegates.
Any ideas or pointers?

You can define a generic delegate as follows:
type Parser<'TInput, 'TOutput> = delegate of 'TInput -> Result<'TOutput>
In F#, the generic parameters are written with a single quote at the beginning (to distinguish them from normal types), so that's why you need'TInput.
Just for the record, I don't think I ever needed to define a delegate in F#. I guess they are still useful for C# interop, but even then, I would probably just define a type alias for a Func delegate, or (when you are not going to be calling this from C#, just an ordinary F# function):
// Alias for a standard .NET delegate
type Parser<'TInput, 'TOutput> = System.Func<'TInput, Result<'TOutput>>
// Alias for a normal F# function
type Parser<'TInput, 'TOutput> = 'TInput -> Result<'TOutput>

Related

How can I create a delegate within an F# type equivalent to a delegate in a C# class?

I am writing code for a game in Godot and I'm using F# as my programming language. I'm kind of pushing the boundary a bit since they are still in late alpha support for C#. It isn't always easy to find solutions to problems from converting patterns from C# to F#. I'm running into an issue declaring my delegates in a way that the Godot Editor will recognize my delegate Signals. In my eyes, this borders on the x-y problem but I'm working in the constraints of the Godot system.
I'm looking to be able to declare the F# equivalent of this class. The key here is that the [Signal] function annotation is what tells the editor that this delegate can be used as a signal in their observer pattern. Having the delegate declared within the class also locks the scope of that delegate to that class. Only that class will raise that signal.
using Godot;
public class GodotNode : Node
{
[Signal]
public delegate void MyDelegate();
public void SendSignal
{
EmitSignal(nameof(MyDelegate));
}
}
I have done by best to try to recreate this within F# but I'm really falling short on how to nest the delegate within another type. From everything I have seen this isn't exactly possible but I was wondering if there was something else I can do.
open Godot
(* This is the closest I have been able to get to. *)
[<Signal>]
type MyDelegateSignal = delegate of Unit -> Unit
type GodotNodeFs()
inherit Node
(* But I need the equivalent of this
[<Signal>]
type MyDelegateSignal = delegate of Unit -> Unit *)
member this.SendSignal =
this.EmitSignal(nameof MyDelegate)
Worst case scenario, I know I can at least get this working by pushing this back down to the C# code. At the moment in Godot, every class needs to be in C#. So all F# classes are inherited by a C# wrapper class.
public class GodotNode : GodotNodeFs
{ // ... put signals and emitters in base class
}

Why does dart not allow method overloading?

I tried to use method overloading in some dart code and quickly learned that overloading is not offered in dart.
My questions are: why is it not offered, and what is the recommended alternative? Is there a standard naming convention since methods that do the same thing but with different inputs must have different names?
Is it standard to use named parameters and then check that the caller has supplied enough information to complete the calculation?
Say I have a method that returns how much money someone makes in a year, called yearlyIncome.
In Java, I would create a method like this
double yearlyIncome(double hourlyRate, double hoursWorkedPerYear)
And maybe another method like this
double yearlyIncome(double monthlyRate, int monthsWorkedPerYear)
and so on. They're all used to calculate the same thing, but with different inputs. What's the best, standardized way to do this in dart?
Thanks so much in advance.
Function overloading is not supported in Dart at all.
Function overloading requires static types. Dart at its core is a dynamically typed language.
You can either use different names for the methods or optional named or unnamed parameters
// optional unnamed
void foo(int a, [String b]);
foo(5);
foo(5, 'bar');
// optional named
void foo(int a, {String b});
foo(5);
foo(5, b :'bar');
Optional parameters can also have default values. Optional named and unnamed parameters can not be used together (only one or the other for a single function)
In the case of a constructor you can use named constructors as an alternative
Dart did not support overloading originally because it was a much more dynamic language where the declared types did not have any semantic effect. That made it impossible to use static type based overload resolution.
Dart has since changed to be more statically type, and there is nothing fundamentally preventing Dart from adding overloading today, except that it would be a huge work and a huge change to the language. Or so I'd assume, because there isn't any obvious design that isn't either highly complicated or hugely breaking.
What you do instead in Dart is to use optional parameters. A method like:
String toString([int radix]);
effectively have two signatures: String Function() and String Function(int). It can act at both signatures.
There are definite limits to how far you can go with just optional parameters, because they still need to have exactly one type each, but that is the alternative that Dart currently provides. (Or use different names, but that's not overloading, you can do that in languages with overloading too).
Optional parameters is also one of the complications if we wanted to add overloading to the Dart language - would existing functions with optional parameters would count as multiple overloadings? If you declare a class like:
abstract class WithOverloading {
String toString();
String toString(int radix);
}
is that then the same signature as:
abstract class WithoutOverloading {
String toString([int radix]);
}
Probably not because you can tear off the latter and get one function with an optional parameter, and you might not be able to tear off both functions from the former and combine them into one function. Or maybe you can, that's why it's not a trivial design question how to include overloading into the existing Dart language.

F#: Implement an inline method (On Inteface) to avoid using a real type

I'm trying to see if inline can be applied to an implemented method so that the specific type coming in doesn't have to be spelled out. I've done this with one off (Not inherited/implemented) methods, but trying to also do using an interface.
type public IBookInteraction =
abstract inline CreateBook : 'a -> MethodResult<BasicBookModel>
type public BookInteraction(?userInteraction) =
interface IBookInteraction with
member inline x.CreateBook(bookModel) =
let userId = (^a : (member UserId : Int32 with get) (bookModel))
MethodResult<BasicBookModel>()
I'm guessing there's a way to do this, but it doesn't work with a generic operator(?) in the interface method signature.
I don't believe it's possible to have abstract inline methods. Even if you could, your code wouldn't work, because your interface definition promises that users can call it with any 'a, but your implementation places a static member constraint on 'a - in a hypothetical world where F# supported abstract inline methods, the declaration of the method on the interface would also need to include the constraint.
In any case, to see why it's not possible for F# to support abstract inline methods, consider what inline means: the code that you write to implement the method will be essentially copied and pasted into the call site. However, with an abstract method, you don't know the concrete type that is defining the implementation of the method, so there's no way to figure out at compile time what code you're supposed to be inlining!
I think the correct answer is interface implementations may not be inlined. I'm not sure why it's allowed in the interface definition.

Delphi - Interfaces and overload directive

I'm a little bit confused on interfaces in Delphi, so I'm asking you about this. An interface can be 'associated' with an abstract class. (It does not implement the methods declared on it.) All the methods declared on it are implemented in the class/classes which is/are implementing the interface.
So, why then is it allowed to have the overload directive on the method declaration of an interface?
type
IFoo = interface
function Test : String; overload;
end;
Compiler is quiet on this.
overloaded allows to have few the same named methods, but with different parameter sets, in a single class / interface.
Your interface has Test method. With this single method there is no need for overloaded. But you can introduce, if you need, additional Test methods with differrent parameter sets.
Probably you are thinking about override directive ...

F#: Implementation of interface that returns reference to iterator

I am implementing the Microsoft.VisualC.StlClr IVector and one of the member functions returns a reference to an iterator, like so
abstract begin :
:ContainerRandomAccessIterator<'TValue> byref -> unit
Would someone know how this interface function could be implemented?
B.
after looking at this interface in Reflector I'd rather say that it is impossible to implement it in F#. Method begin (as well as some other methods) has custom required modifier IsUdtReturn (modreq[IsUdtReturn]) and it seems it is not recognized by F# compiler. However I'll be glad to know that I'm wrong.

Resources