问题
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.
回答1:
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
回答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:
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.
来源:https://stackoverflow.com/questions/60137632/is-this-shadowing-a-variable-in-f