Search code examples
variablesf#shadowing

Is this shadowing a variable in F#?


        let print_scene (y, v) =
            do Console.Clear()

            let y, v = int y, int v    (* This is the code in question *)

            for j = 10 downto 0 do
                for i = 0 to 30 do
                    if (y + 1) = j && i = 15 then
                        Console.Write("b")
                    elif j = 0 || i = 0 || j = 10 || i = 30 then
                        Console.Write("*")
                    else
                        Console.Write(" ")
                Console.Write("\n")
            ignore(Console.ReadKey())

I don't understand what the int is doing in this code, what it is, or why it's there.


Solution

  • Indeed, this is parameter shadowing.

    let foo bar =
        let bar = bar * bar
        bar
    

    This is absolutely fine in F#. A function parameter is being shadowed by a binding. Nothing is being changed - it just makes the original binding inaccessible.

    The deeper problem lies in the int. Because int converts a type to an Int32 you'd expect the function to take in anything that can be converted to int; either numbers or strings. But -

      let print_scene (y, v) =
             let y, v = int y, int v 
             ()
    
        print_scene (1.0, "2.0")
        print_scene (1.0,  2.0) //this won't compile
    

    The function parameters will be constrained from its first usage. Here, its type becomes:

    float * string -> unit
    

    This is because F# doesn't have higher-kinded polymorphism. You are probably better off being explicit about the type of parameters you want to accept, or inlining it, if being generic is important to you.

    let inline print_scene (y, v) =
         let y, v = int y, int v 
         ()
    
    print_scene (1.0, "2.0") 
    print_scene (1.0, 2.0) //this works