|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching * Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
Uniform Function Call Syntax (UFCS)Nim supports the so-called Uniform Function Call Syntax (UFCS). It's an awesome thing, though not many languages support it (D, for instance, knows it). Let's see this example: func is_even(n: int): bool = n mod 2 == 0 echo is_even(4) # true echo is_even(9) # false With UFCS, you can move the first argument to the left side and call the function as if it were a method: echo 4.is_even() # true; the compiler will call is_even(4) It works :) Although Nim is not really an object-oriented language, thanks to UFCS you can use a syntax that is common in OO languages: What if you have multiple arguments?func add(a, b: int): int = a + b echo add(3, 5) # 8 echo 3.add(5) # 8; converted to add(3, 5) The value before the dot is passed as the first argument. This is not UFCS, but closely related IMO, so I put it here: If a function has no argument, the parentheses can be omitted: echo 4.is_even # true I prefer adding the parentheses, that syntax makes it clear that we call a function. However, if you want, you can drop the parentheses. Furthermore, if a function has just one argument, then you can also omit the parentheses. These two are equivalent: discard is_even(4) discard is_even 4 I don't recommend using it, but it's also possible. Here I had to add |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |