0
        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.

On The Net Again
  • 265
  • 4
  • 16
  • 2
    See https://msdn.microsoft.com/visualfsharpdocs/conceptual/operators.int%5b%5et%5d-function-%5bfsharp%5d – glennsl Feb 09 '20 at 14:46
  • @glennsl - post as an answer so I can check it and upvote. – On The Net Again Feb 09 '20 at 14:50
  • @glennsl great find! Google doesn't seem to index that page - it didn't appear for me even when searching for the first sentence in quotes: "Converts the argument to signed 32-bit integer". Weird. – DharmaTurtle Feb 09 '20 at 15:06

2 Answers2

2

int is a function that takes a type that has a static member op_Explicit and returns an integer. string is the most common example of this:

int "108" // returns `108` as an integer

In VS Code, if you hover on int you'll see this:

enter image description here

If you call print_scene("1", "2") it should return the same output as print_scene(1, 2).

This is shadowing:

let i = 1
let i = 2

This does not mutate i. I'll refer you to this answer.

DharmaTurtle
  • 6,858
  • 6
  • 38
  • 52
  • > If you call print_scene("1", "2") it should return the same output as print_scene(1, 2). Unfortunately, you can't do both. – Asti Feb 09 '20 at 15:49
2

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
Asti
  • 12,447
  • 29
  • 38