Skip to content

F# late binding semantics different from C# late binding semantics and the complexity flexible types will introduce #3526

Closed

Description

@zpodlovics

Please provide a succinct description of the issue.

It seems that some newer features will introduce non-explicit type conversion / flexible types in F# eg.: #2382

However this may have some unintended consequences. It will may introduce lot's of unwanted complexity. If you think OO is easy, think again, and check Antoine Beugnard OO languages late-binding semantics results in https://web.archive.org/web/20070701151903/http://perso-info.enst-bretagne.fr/~beugnard/papiers/lb-sem.shtml and paper https://www.cis.upenn.edu/~bcpierce/FOOL/papers9/beugnard.pdf

"We observe the various interpretations of overriding and overloading in object oriented languages. With the description of an intend, represented as the following UML diagram, we have "naively" implemented in 14 different languages the same piece of code... and obtenained 10 different results."

As a simple test I have reimplemented Antoine Beugnard OO languages late-binding semantics tests in F# and C#. Try to guess the output based only the source, without executing it.

cc: @palladin
Surprisingly F# and C# have different late binding semantics. And this is without any generics introduced, and without component which implemented different languages (eg.: F# Up class + C# Top class, C# Down class + F# Top class, etc).

Update: there was a mistake in the original C# and F# upload, due unsaved changes. The source code here was also incorrect for the F# Inv part.

Repro steps

Provide the steps required to reproduce the problem

  1. Extract F# Example program from and built and execute it with .NET Core 2.0.
    fsharp-latebinding.zip
/// Author: Zoltan Podlovics
/// License: Apache License, Version 2.0, https://www.apache.org/licenses/
/// Description: Late binding semantics in F# based on Antoine Beugnard paper and archived homepage
/// https://web.archive.org/web/20070701151903/http://perso-info.enst-bretagne.fr/~beugnard/papiers/lb-sem.shtml#c#
/// https://www.cis.upenn.edu/~bcpierce/FOOL/papers9/beugnard.pdf

open System

type Top() = 
    class end
type Middle() =
    inherit Top()
type Bottom() =
    inherit Middle()

type Up() =
    member this.Cv(x: Top) =
        printfn "Up::Cv(Top)"
    member this.Inv(x: Middle) =
        printfn "Up::Inv(Middle)"
    member this.Ctv(x: Bottom) =
        printfn "Up::Ctv(Bottom)"

type Down() =
    inherit Up()
    member this.Cv(x: Middle) =
        printfn "Down::Cv(Middle)"
    member this.Inv(x: Middle) =
        printfn "Down::Inv(Middle)"
    member this.Ctv(x: Middle) =
        printfn "Down::Ctv(Middle)"

[<EntryPoint>]
let main argv =
    let t = Top()
    let m = Middle()
    let b = Bottom()
    let u = Up()
    let d = Down()
    let (ud: Up) = upcast Down()

    printfn "Up"
    u.Cv(t)
    u.Cv(m)
    u.Cv(b)
    //u.Inv(t)
    printfn "Compile error"
    u.Inv(m)
    u.Inv(b)
    //u.Ctv(t)
    printfn "Compile error"
    //u.Ctv(m)
    printfn "Compile error"
    u.Ctv(b)

    printfn ""

    printfn "Down"
    d.Cv(t)
    d.Cv(m)
    d.Cv(b)
    //d.Inv(t)
    printfn "Compile error"
    d.Inv(m)
    d.Inv(b)    
    //d.Ctv(t)
    printfn "Compile error"
    d.Ctv(m)
    d.Ctv(b)    

    printfn ""

    printfn "UpDown"
    ud.Cv(t)
    ud.Cv(m)
    ud.Cv(b)
    //ud.Inv(t)
    printfn "Compile error"
    ud.Inv(m)
    ud.Inv(b)    
    //ud.Ctv(t)
    printfn "Compile error"
    //ud.Ctv(m)
    printfn "Compile error"
    ud.Ctv(b)    

    printfn ""
    0 // return an integer exit code
  1. Extract C# Example program from and built and execute it with .NET Core 2.0.
    csharp-latebinding.zip
/// Author: Zoltan Podlovics
/// License: Apache License, Version 2.0, https://www.apache.org/licenses/
/// Description: Late binding semantics in F# based on Antoine Beugnard paper and archived homepage
/// https://web.archive.org/web/20070701151903/http://perso-info.enst-bretagne.fr/~beugnard/papiers/lb-sem.shtml#c#
/// https://www.cis.upenn.edu/~bcpierce/FOOL/papers9/beugnard.pdf

using System;

namespace latebinding
{
    public class Top {} 
    public class Middle: Top {} 
    public class Bottom: Middle{}    

    public class Up { 
        public void Cv(Top t)
        { 
            Console.WriteLine("Up::Cv(Top)"); 
        }    
        public void Inv(Middle m){ 
            Console.WriteLine("Up::Inv(Middle)"); 
        } 
        public void Ctv(Bottom b){ 
            Console.WriteLine("Up::Ctv(Bottom)"); 
        } 
    }     

    public class Down: Up { 
        public void Cv(Middle m)
        { 
            Console.WriteLine("Down::Cv(Middle)"); 
        }    
        public void Inv(Middle m){ 
            Console.WriteLine("Down::Inv(Middle)"); 
        } 
        public void Ctv(Middle m){ 
            Console.WriteLine("Down::Ctv(Middle)"); 
        } 
    }

    class Program
    {
        static void Main(string[] args)
        {
            Top t = new Top();
            Middle m = new Middle();
            Bottom b = new Bottom();
            Up u = new Up();
            Down d = new Down();
            Up ud = new Down();

            Console.WriteLine("Up");
            u.Cv(t);
            u.Cv(m);
            u.Cv(b);
            //u.Inv(t);
            Console.WriteLine("Compile error");
            u.Inv(m);
            u.Inv(b);
            //u.Ctv(t);
            Console.WriteLine("Compile error");            
            //u.Ctv(m);
            Console.WriteLine("Compile error");
            u.Ctv(b);
            Console.WriteLine("");

            Console.WriteLine("Down");
            d.Cv(t);
            d.Cv(m);
            d.Cv(b);
            //d.Inv(t);
            Console.WriteLine("Compile error");            
            d.Inv(m);
            d.Inv(b);
            //d.Ctv(t);            
            Console.WriteLine("Compile error");
            d.Ctv(m);
            d.Ctv(b);
            Console.WriteLine("");

            Console.WriteLine("UpDown");
            ud.Cv(t);
            ud.Cv(m);
            ud.Cv(b);
            //ud.Inv(t);
            Console.WriteLine("Compile error");
            ud.Inv(m);
            ud.Inv(b);
            //ud.Ctv(t);
            Console.WriteLine("Compile error");
            //ud.Ctv(m);
            Console.WriteLine("Compile error");
            ud.Ctv(b);
            Console.WriteLine("");
        }
    }
}

If possible attach a zip file with the repro case. This often makes it easier for others to reproduce.
The zip file should ideally represent the situation just before the call/step that is problematic.

Expected behavior

Same late binding semantics(?) if it's reasonable. The late binding semantics should be defined explicitly for every language how it supposed to work. And should be defined explicitly how it supposed to work with components (eg.: F# Up class + C# Top class, C# Down class + F# Top class, etc) implemented in other languages (eg.: C#, VB, etc). In my opinion automatic type conversion and flexible types should not introduced before these late binding issues solved properly.

--- /tmp/fsharp.txt	2017-08-30 23:03:53.594297191 +0200
+++ /tmp/csharp.txt	2017-08-30 22:59:57.347476061 +0200
@@ -18,7 +18,7 @@
 Down::Inv(Middle)
 Compile error
 Down::Ctv(Middle)
-Up::Ctv(Bottom)
+Down::Ctv(Middle)
 
 UpDown
 Up::Cv(Top)

Actual behavior

Different late binding semantics.

Known workarounds

No workarounds known.

Related information

Provide any related information

Activity

  1. dsyme commented on Aug 30, 2017

    @dsyme
    Contributor

    The text Up::Ctv(Middle) gets shown in the C# output but is not in the C# program?

  2. zpodlovics commented on Aug 30, 2017

    @zpodlovics
    Author

    @dsyme Sorry, my mistake. The issue source code is correct, but the uploaded file was not (unsaved content in the editor). I have re-uploaded the C# package, and updated the diff.

  3. zpodlovics commented on Aug 30, 2017

    @zpodlovics
    Author

    Well, it was almost correct, I made a mistake on the F# source code part too, now it fixed. And the difference was reduced to one case now.

  4. dsyme commented on Aug 30, 2017

    @dsyme
    Contributor

    Yep, this is by design. F# overload resolution is not an identical copy of C#, partly because F# supports a different kind of type inference and so does things differently.

    That said, thanks for the interesting cases - and yes, these things matter in some corner cases.

    BTW I don't think the above really tests late binding - just overload resolution in the presence of subtyping.

    Per CONTRIBUTING.md I'll close this - please follow up at http://github.com/fsharp/fslang-suggestions if there are specific concrete suggestions for changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions