Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-21 Thread Aareon
Not necessarily the global scope, more like scope within a for statement, or a 
proc 


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-21 Thread mratsim
You can't exit the global scope except by exiting the program because its scope 
is the program.


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-21 Thread Aareon
Hmm? Destructors aren’t automatically called when the scope is left? Why is 
that? I’ve been looking into this for the past 3-4 days (since I started trying 
to port some simple Python scripts to Nim) with no luck, only to find this.


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-19 Thread bpr
Oh I see; I had assumed your container API was generic (in the sense of 
parametric polymorphism) but you have some kind of subtyping there.

I hope that when Nim 1.0 comes in 2037 that there's a different solution than 
the current `method`. Something more minimalistic, like Ada (>95) provides. I 
think those would address your use case.


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-19 Thread Stefan_Salewski
Maybe we should also mention that all object variants of a collection have the 
same size (occupied bytes in ram). For very heterogeneous objects like 
graphical elements (plain line vs complicated element) that may wast some 
memory.


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-19 Thread mratsim
Library users cannot change the `kind` discriminant of an object variant 
without forking the library.

For example in Arraymancer, if you have some neural network layers predefined 
from super simple (addition, matrix multiplication, mean, sum) to more complex 
(convolutions, softmax, ...). I can't hardcode the `kind` of layers as that 
would prevent users to define their own without forking the "LayerKind" enum.


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-18 Thread bpr
> For a library, when you need to build a heterogeneous collection of objects 
> with types that can be user defined.

Why don't object variants work here, instead of OOP? I don't see the need to 
introduce method for this case.

The only thing OO provides is open recursion, which I haven't needed in most of 
the code I write.


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-15 Thread mratsim
My opinion is that OOP in Nim is necessary in a very specific case:

  * For a library, when you need to build a heterogeneous collection of objects 
with types that can be user defined.



Anywhere else I actively try to avoid it.

And for naive getter and setter, you can just make the fields public. They are 
useful only if input must be validated or update is not trivial (like for the 
times module).


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-14 Thread kcvinu
The most weird OOP syntax . Thats what is Nim OOP. 


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-11 Thread LeuGim
They are called. But sometimes unexpectedly. For assignment of an object to a 
gloabal variable destructor currently is called just after assignment (if not 
changed already).


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-11 Thread LeuGim

proc `=destroy`(self: var SomeClass) = echo "destroying " & $self


Run

Destructors work not very smoothly. At least with global variables.

Types (including object types) are infered in Nim. As `newSomeClass` is 
declared above as returning `SomeClass`, you don't need to declare the 
variable's type - compiler can determine it unambiguously. But you still can to 
state it explicitly, `var Aclass: SomeClass = ...`, compiler will check it. You 
can assign variable later, then its type is required at definition. I.e., this 
all is not related to OOP, just to variables.

Very concise still exhaustive Nim's 
[manual](https://nim-lang.org/docs/manual.html) explains well both topics.


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-11 Thread leorize
Just write it like any other `proc`, although currently it is not possible for 
them to be automatically triggered when a variable gone out of scope:


type
  SomeClass* = object
m_Var1: int
m_Str1: string
m_For_Prop: int

proc destroySomeClass*(sc: var SomeClass) =
  sc.m_Var1 = 0
  sc.m_Str1 = ""


Run


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-11 Thread kcvinu
@LeuGim, Thanks for the reply. Now everything is clear to me. Great 
explanation. But one thing i forgot to add in my VB.Net class. How write a 
destructor proc in Nim ?


Re: Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-08 Thread LeuGim
E.g. so:


# module1.nim
type SomeClass* = object
  m_Var1: int
  m_Str1: string
  m_For_Prop: int
proc newSomeClass*: SomeClass =
  result.m_Var1 = 5400
  result.m_Str1 = "A String from consrtuctor"
proc PrintMembes*(self: SomeClass) =
  echo self.m_Var1
  echo self.m_Str1
  echo self.m_For_Prop
proc AProp*(self: SomeClass): int =
  self.m_For_prop
proc `AProp=`*(self: var SomeClass, value: int) =
  self.m_For_Prop = value

# module2.nim
import module1

var Aclass = newSomeClass()
Aclass.PrintMembes
Aclass.AProp = 555


Run

  * `self` is just a parameter name, you can use `this`, `obj` or whatever you 
want instead
  * `*` is to make public: that is you can access these symbols in other modules
  * the set of procedures working on a type is not fixed, you can add such 
"methods of SomeClass" in other modules; and you can call them like 
`PrintMembes(Aclass)` too; they are just procedures




Confused with Nim OOP tutorials. Please clarify straight forward examples

2018-09-08 Thread kcvinu
Hi, i cant digest the example in OOP section. This is a class example is from 
VB.Net. Please somebody translate this into Nim, so that i can easily 
understand what Nim offers.

  1. Public Class SomeClass
  2. Private m_Var1 As Integer '// An Integer member variable
  3. Private m_Str1 As String '// A String memeber var
  4. Private m_For_Prop As Integer '// An integer for property
  5. Public Sub New() '// A constructor without any params
  6. Me.m_Var1 = 5400 '// initialized member var with value 5400
  7. Me.m_Str1 = "A String from constructor" '// Initialized member string
  8. End Sub



9.

  1. Public Sub PrintMembes() '// A method to print the member values
  2. Console.WriteLine(Me.m_Var1)
  3. Console.WriteLine(Me.m_Str1)
  4. End Sub
  5. Public Property AProp() As Integer '// A public property which we can read 
and write
  6. Get
  7. Return Me.m_For_Prop
  8. End Get
  9. Set(ByVal Value As Integer)
  10. Me.m_For_Prop= value
  11. End Set
  12. End Property
  13. End Class