On the proc and method in Nim

Source: Internet
Author: User


In Nim ,proc is the keyword that defines the process, and method is the keyword that defines it. The fundamental difference between them is that the process defined by proc is a static binding, and method is defined by dynamic binding. When it comes to static bindings, dynamic bindings, and things like overloading, rewriting, and polymorphism, if you don't understand some of these concepts, look at my last blog post.


Overloading of procedures:

Proc print (): void =   echo ("This is Empty") proc print (a:int): void =   echo ("int a =", a) proc print (a:float): VO id =   echo ("float a =", a) print () print (1) print (1.0)
Out:this is emptyint a = 1float A = 1.0

Proc defines a procedure overload that can be in a class or not in a class. methods defined by method can only be overloaded within the class. I think this should be a static binding with Proc, and method is about dynamic binding.


Overloads of the method:

Type  Thing = ref object of rootobj  Unit = ref object of Thing    X:intmethod Collide (A, b:thing) {. inline.} =
   quit "to override!" Method Collide (a:thing, b:unit) {. inline.} =  echo "1" Method Collide (A:unit, b:thing) {. inline.} =  echo "2" meth Od Collide (a:unit, b:unit) {. inline.} =  echo "3" var A, B:unitvar c:thingnew anew bnew ccollide (A, c) # Output:2co Llide (A, b) # Output:3


class, if proc defines a procedure overload, the procedure call, because proc is a static binding, is strictly called by the procedure parameter requirements , If the parameter type does not match compile, an error occurs. If The method is defined by means that because method is dynamic bound, the runtime matches the parameter type from left to right to determine which method to call. As in the above example, if the fourth collide method is commented out, the call to collide (A, b) outputs 2.


Overrides and polymorphism:

#Module: Tprocmethod.nimtype animal* = ref object of Rootobj  name*: string  age*: int  proc selfintroduce* ( This:animal): void = Echo ("My name is Animal") method vocalize* (this:animal): string = "..." Method agehumanyrs* (This:an iMAL): int = This.agetype dog* = ref object of Animalproc selfintroduce* (this:dog): void = Echo ("My name is", this.name) Method Vocalize* (This:dog): string = "Woof" Method agehumanyrs* (this:dog): int = this.age * 7type cat* = ref object of an Imalproc selfintroduce* (this:cat): void = Echo ("My name is", THIS.name) method vocalize* (this:cat): string = "Meow"

#Module: testprocmethod.nimimport Tprocmethodvar animals:animalanimals = Dog (name: "Sparky", Age:10) echo Animals.nameecho Animals.ageecho animals.vocalize ()                 #method方法调用, is referenced by an instance object. Method Name Echo animals.agehumanyrs () Tprocmethod.selfintroduce (Animals)      #这里调用proc过程是用 module name. The name of the procedure, all the letters of the file name (module name) are considered lowercase when you run the file with the command line under Windows,                                         # When calling a procedure here, be aware of the case of the module name letter. The case is case-sensitive under Linux. Animals = Cat (name: "Mitten", Age:10) echo animals.vocalize () echo animals.agehumanyrs () Tprocmethod.selfintroduce ( Animals

Out:sparky10woof70my name is animalneow10my name is animal

Summarize:

1, the process is static binding, method is dynamic binding

2, procedures and methods can be overloaded, but the method can only be inside the class.

3, the process is like a static method in Java, can not be rewritten, there is no polymorphism.

4, in order to achieve polymorphism, the operation in the class is the method defined methods.

5, the process is through the module name. The procedure name is called by reference to the instance object . Method Name Call.







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

On the proc and method in Nim

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.