Results 1 to 8 of 8

Thread: About Nim (Formerly Nimrod)

  1. #1
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    121
    Rep Power
    21

    About Nim (Formerly Nimrod)

    Hi all,
    I would like to share some interesting stuff about the Nim programming language.
    Nim compiles code to first C/C++/Obj-C/JS code and then use a C/C++/Obj-C/JS compiler to make executable.
    Nim's syntax is much like python. But you can taste some pascal style in Nim.
    Nim has a minimal support for object oriented programming. It uses Types for it. Here is the syntax
    type myClass = ref object of RootObj   # an object lives in stack, a ref object lives in heap. 
        aMember : int
        anotherMember : string
    
    The main work horse in Nim is "proc", a Nim version for function and sub. Another best feature i love in Nim is Uniform Function Call Syntax aka UFCS. Assume that you have a function like this.
    proc aFunction(param1 : string, param2 : int, param3 : bool) : bool
    
    Then you can call that function like this -
    param1.aFunction(param2, param3)
    
    Another plus points
    1. Nim uses Garbage Collector
    2. You can inherit a Nim class with "of" keyword.
    3. You can use pragmas. for ex: "{. pure.}" is a pragma. If you use it with an enum, then you must use name of that enum whenever you use any element of that enum.
    4. Nim uses a magical "result" variable in functions. "result" is automatically the function result. So you dont need to create and assign one.
    5. Classes created with "ref" keyword are initializing with "new" keyword.
    6. To declare a variable or a function as public, you just need to put an "*" sign after the identifier.

    Here is a sample code to understand Nim style.
    type
    	aClass  = ref object of RootObj		# A base class
    		intVar : int
    		strVar : string
    		
    	aChildClass = ref object of aClass		# A child class which inherits from aClass
    		aVar : int
    		boolVar : bool
    		
    proc NewClass(iVar : int) : aClass = 	# Constructor of aClass. 
    	result = new aClass									
    	result.intVar = iVar
    	result.strVar = "Hi I am aClass member"
    	
    proc NewChildClass() : aChildClass = 
    	result = new aChildClass 									
    	result.intVar = 500 				# Accessing base class member
    	echo "Hi I am from child class"    # Printing some text
    	
    proc childMethod(me : aChildClass) : int =
    	result = me.intVar		# Magical result variable.
    	
    #Usage of this classes.
    var aBase = NewClass(62)		# var is the dim in Nim
    var aChild = NewChildClass()
    echo aChild.childMethod() 		# prints 500. See the UFCS is interesting...
    

  2. #2
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    121
    Rep Power
    21
    Since, Nim compiles to C, we can make sure that our program will work as fast as C. Author of Nim says that Nim can create better C code than any human made C code. Some guys in Nim forum are discussing Nim's speed with when compiled to C and CPP. Well i am using only C compiling version, because thats the default compiling method.

    This UFCS is not only Nim's feature. The "D" programming language also using this.

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Hi kcvinu,

    thanks a lot for the quick introduction to the language - highly appreciated!


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4
    IUP is a nice companion library for Nim GUI projects.
    Last edited by John Spikowski; 15-10-2018 at 04:33.
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  5. #5
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    121
    Rep Power
    21
    @Petr,
    You're welcome.

    @John Spikowski,
    Is it ? I never tried it. In fact, i am making one with win api nim. There is guy named ward(forum name) made a huge library of win api wrapper. It's called "winim". With winim, its very easy to code win api functions. Here are some samples.
    import GuiLib       '# My Gui Library header file. It contains all necessary modules.
    
    '# This Application class will do the initial jobs such as getting the hInstance.
    '# You can pass a string for your window class name. It's optional.
    var app = Application("Window-Nim")
    
     
    '# Forward declarations. 
    proc OnFormCreate( e : eventArgs)
    proc OnLoad( e : eventArgs)
    proc OnNCCreate(e : eventArgs)
    proc OnButtonClick( e : eventArgs)
    proc OnButtonClick2( e : eventArgs)
    proc b1OnMouseEnter( e : eventArgs)
    proc OnClosing( e : eventArgs)
    proc OnSetFocus( e : eventArgs)
    proc OnKillFocus( e : eventArgs)
    proc OnMouseLeave_cbt(e : eventArgs)
    proc OnMouseMove_Cal(e : eventArgs)
    proc OnClick_CustomBTN(e : eventArgs)
    
    
    
    '# Creating an instance of a form class. We need to pass the Application object as parameter--
    '# -- in order to get the hInstance and other stuff.
    var Form1 = NewForm(app)
    
    '# RegisterMsg is for handling wm_create msg. I can't find a better way to do this.
    Form1.RegisterMsg(WM_CREATE, OnFormCreate)
    
    '# Now we are creating the form. If you want to set the for properties, you can--
    '# --set it like this.
    '# Form1.posx = 150
    '# Form1.posy = 200
    '# Form1.width = 800
    '# Form1.height = 600
    '# Form1.text = "My Nim Window"
    '# But i am using default values here.
    Form1.CreateForm()
    
    '# Create some control classes
    var 
        b1, b2 = Button()
        lv = ListView()
        cal = Calendar()
        ud = UpDown()
        dtp = DateTimePicker()
        trb = TrackBar()
        red = RichEdit()
        cbt = CustomButton()
    
    '# Here you can add handler functions to control events lile click, mouse move etc.
    b1.AddHandler(b1.event.click, OnButtonClick)
    b2.AddHandler(b2.event.click, OnButtonClick2)
    b2.AddHandler(b2.event.mouseLeave, b2_OnMouseLeave)
    b1.AddHandler(b1.event.mouseEnter, b1OnMouseEnter)
    Form1.AddHandler(Form1.event.mouseRightUp, OnDblClk_Form1)
    Form1.AddHandler(Form1.event.closing, OnClosing)
    Form1.AddHandler(Form1.event.load, OnLoad)
    Form1.AddHandler(Form1.event.setFocus, OnSetFocus)
    Form1.AddHandler(Form1.event.killFocus, OnKillFocus)
    cal.AddHandler(cal.event.mouseEnter, OnMouseMove_Cal)
    red.AddHandler(red.event.mouseLeave, OnMouseLeave_Red)
    cal.AddHandler(cal.event.mouseHover,OnMouseHover_Cal)
    cbt.AddHandler(cbt.event.click, OnClick_CustomBTN)
    cbt.AddHandler(cbt.event.mouseLeave, OnMouseLeave_cbt)
    
    '# Now, we can set the control properties.
    b2.posx =140
    b2.posy = 10
    b2.text = "My Button"
    b2.width = 150
    b2.font.name = "Calibri"    '# Tahoma is default
    b2.font.size = 12
    
    lv.posx = 350
    lv.posy = 10
    lv.width = 300
    
    cal.posx = 350
    cal.posy = 10
    
    ud.posx = 350
    ud.posy = 320
    
    '# See, custom button class has so many color properties. 
    cbt.posx= 350
    cbt.posy= 220
    cbt.text = "Owner Drawn BTN"
    cbt.backColor = NewColor(80, 16, 44)    '# NewColor function receives RGB values and returns a COLORREFF
    cbt.foreColor = NewColor(15, 15, 80)
    cbt.hoverBackColor = cbt.backColor      
    cbt.hoverForeColor = NewColor(238,20,49)
    cbt.width = 200
    cbt.height = 80
    
    dtp.posx = 150
    dtp.posy = 150
    
    trb.posx = 150
    trb.posy = 120
    
    red.posx = 10
    red.posy = 200
    
    '# Now we can create our controls.
    b1.CreateButton()
    b2.CreateButton()
    cal.CreateCalendar()
    ud.CreateUpDown()
    dtp.CreateDateTimePicker()
    trb.CreateTrackBar()
    red.CreateRichEdit()
    cbt.CreateCustomButton()
    
    # Let's display our form
    Form1.ShowForm()
    
    '# This is the main loop.
    app.Run()
    
    
    '# Now, these are the event handler function area.
    '# Argument "e" is a structure. It contains all necessary elements to handle the--
    '# --WndProc function
    
    proc OnCreate( e : eventArgs)  =
        echo "Window created....."
        echo e.handle   '# e.handle is the window HWND
    
    proc OnLoad( e : eventArgs) =
        echo "Window Loaded...."
        
    proc OnButtonClick( e : eventArgs) =
        b2.text = "Changed Text"    
    
    proc OnButtonClick2( e : eventArgs) =    
        msgBox("A Message")     
    
    proc b1OnMouseEnter( e : eventArgs) =
        echo "Mouse entered on button 1"
    
    proc b2_OnMouseLeave( e : eventArgs) =
        echo "Mouse leaved from Button 2"    
    
    proc OnNCCreate( e : eventArgs) =    
        echo "NC CREATE handled"  
    
    proc OnClosing( e : eventArgs) =
        echo "Form is closing..."    
    
    proc OnSetFocus( e : eventArgs) = 
        echo "Form is in focus...."
    
    proc OnKillFocus( e : eventArgs) = 
        echo "Form lost focus...."
    
    proc OnMouseLeave_cbt(e : eventArgs) =
        echo "Mouse leaved from Custom button..."
    
    proc OnMouseMove_Cal(e : eventArgs) = 
        echo "Mouse hovered over Calendar..."
    
    proc OnMouseLeave_Red(e : eventArgs) =
        echo "Mouse leave from Rich Edit..."
    
    '# End
    

  6. #6
    The ease of use and native cross platform (Windows and Gtk Linux) shared objects or static linking. It works well with interpreters via FFI and has a stepping callback loop.

    Eros created a IUP example in thinBasic a while back.
    Last edited by John Spikowski; 16-10-2018 at 06:48.
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  7. #7
    hello kcvinu
    that code looks good, easy to read and figure out.
    hope you won't mind this link to a MFC alternative I found, looks interesting to me https://www.codeproject.com/Articles...pc=Tight&fr=26

  8. #8
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    121
    Rep Power
    21
    Quote Originally Posted by jack View Post
    hello kcvinu
    that code looks good, easy to read and figure out.
    hope you won't mind this link to a MFC alternative I found, looks interesting to me https://www.codeproject.com/Articles...pc=Tight&fr=26
    Hi, Thanks for the good words. Let me check the link. May i can learn a lot from that project. Thanks for the link

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •