I am trying to create a type which is basically an Array with additional information. I want to use only [int | float] for the Array.
type
MyArray* = ref object
length*: int
index: int
arr: seq[int|float]
proc createMyArray(length: int, tp: string): MyArray =
if tp == "int":
let ttype = int
elif tp == "float":
let ttype = float
result = MyArray(
length: length,
index: 0,
arr: newSeq(ttype)[length]
)
var ma = createMyArray(int(rows), "float")
The code is probably naive as I am still learning. The MyArray type compiles if everything below is commented out. As soon as I uncomment, it gives me this error.
/home/jimmie/Dev/Nim/ntplayground.nim(125, 19) Error: invalid type: 'int or float' in this context: 'proc (length: int, tp: string): MyArray' for proc
Any help, wisdom or understanding greatly appreciated.
I'm not too sure of your end goal but Nim is a static language so types must be known at runtime, you can't pass them a a string.
Also your type is generic over int or float so you need to use the generic syntax:
type
MyArray*[T: int|float] = ref object
length*: int
index: int
arr: seq[T]
proc createMyArray[T: int|float](length: int, tp: typedesc[T]): MyArray[T] =
result = MyArray[T](
length: length,
index: 0,
arr: newSeq[T](length)
)
var ma = createMyArray(1000, float)
Why are you trying to create such a thing?
I am sure you could make such thing in nim (you can do anything in nim). Buy why?
I think the reason its hard to do such a thing is that nim is trying to say don't do it this way.
If you know the type of array at compile time just use:
var ma = newSeq[float](rows)
If you don't, use a case object:
type
OddArrayKind = enum
OddInt, OddFloat
OddArray* = ref object
case kind: OddArrayKind
of OddInt:
intSeq: seq[int]
of OddFloat:
floatSeq: seq[float]
proc createMyArray(length: int, strSind: string): OddArray =
if strSind == "int":
OddArray(kind: OddInt, intSeq: newSeq[int](length))
else:
OddArray(kind: OddFloat, floatSeq: newSeq[float](length))
var rows = 23
var ma = createMyArray(rows, "float")
I would strongly suggest creating the intSeq or the floatSeq and passing that around instead of doing case objects and these odd string types.
Thanks. That works and got me going.
I wasn't trying to pass types as a string so much as knowledge about the type.
I am still working on understanding generics. I have not found much documentation regarding generics. I have the book and I have read the Tutorial II.
Most documentation is regarding defining a proc not a type.
Is there documentation I am overlooking? Or is there a good source of source code which would have good examples that are comprehensible by mortals. :)
Again thanks.
My apologies for not being more clear. I could easily simply create and pass around the plain seqs. But that defeats the purpose. The purpose is to maintain state and knowledge of the array or seq that a smarter object can do.
For example in time series data that I collect as time goes on or on historical data that I operate on as if I am in the moment.
It is far less expensive to maintain state of a seq than to perform operations on it. Especially if those operations are being done over and over.
I tried for a minimal example that would teach me what I needed to know to fix my understanding and solve my problem. However it is not a complete example.
An easy example is if I had a seq of which I wanted to know the sum, average, max and min.
All these things are easily achieved in a seq or array and computed when desired. They can also become expensive. What is the cost of sum, max and min over a arbitrarily large array? Verses the cost of maintaining a sum, max and min. And then you multiply that cost by every object which has a reference to that seq.
That is what I am trying to achieve. Nim is amazing. Its performance in those computation is outstanding. However, if I can avoid them in the first place. I can do better.
Creating such an object even in a dynamic language makes knowing the sum, max and min instant even over a multi-million valued array.
Then the question might be why have the array not simply maintain state. The array is part of the state. And I do not know that there are no other requirements which can only be met by the array itself.
Hope this helps.
Thanks for engaging in the conversation.