Re: How to return a seq[string] from proc

2019-07-20 Thread leorize
Remove `{.discardable.}`. Don't use that pragma if you don't understand how Nim 
works.

> When trying to compile without {.Discardable.} I get an error - van2.nim(49, 
> 24) Error: expression 'splitstring(new_string, split_by, accu)' is of type 
> 'seq[string ]' and has to be discarded; start of expression here: 
> van2.nim(39, 8)

Return values of a function can't be implicitly ignored in Nim without 
`{.discardable.}`

> How do I return a copy of the resulting sequence of string?

Nim's sequences use value semantics, so they are always copied, for example 
([run on playground](https://play.nim-lang.org/#ix=1P24)):


var
  a = @[1, 2, 3, 4]
  b = a

b.add 5
echo a # @[1, 2, 3, 4]
echo b # @[1, 2, 3, 4, 5]


Run

> It seems like the function is returning too early - I'm assuming because 
> result is being used in the if block?

The function is not returning too early. The problem is that you dropped the 
return value of your recursion calls (which you didn't notice thanks to 
`{.discardable.}`), so they disappeared.

It's worth noting that `result` is just like any ordinary variable and will 
**not** cause any control-flow changes.

Here's my simplification of the function you presented: 
[https://play.nim-lang.org/#ix=1P25](https://play.nim-lang.org/#ix=1P25). But 
please just use `strutils.split` instead.


Re: wNim - Nim's Windows GUI Framework

2019-07-20 Thread oyster
offtopic, in the table, there are very long list like "a":a ("wAquamarine": 
wAquamarine). Is there an concise way to write it?


Re: How to return a seq[string] from proc

2019-07-20 Thread ravigupta001
Thank you very Mratsim! That worked. It seems like the function is returning 
too early - I'm assuming because result is being used in the if block? I 
modified the proc to make it a bit more obvious what is happening, and when. 
I'm expecting two values in the seq to be returned not one. A second question 
that I have is, I'm expecting that when proc is being called in that first if 
block - both with the if, and else that the rest of the proc is being foregone. 
Am I thinking about this the right way?


import sequtils

let file = readfile("testing")


proc splitstring(string_to_split: string, split_by: string, acc: 
seq[string]): seq[string] {.discardable.} =
let search_len = split_by.len
let found = string_to_split.find(split_by)
let end_position = found + search_len + -1
echo(end_position)
if found != -1:
echo("string was found")
let new_string_start = end_position + 1
let new_string = string_to_split[new_string_start..^1]
if len(acc) == 0:
result.add(string_to_split[0..end_position])
splitstring(new_string, split_by, result)
else:
result = acc
result.add(string_to_split[0..end_position])
splitstring(new_string,split_by, result)
else:
echo("the final result of splitting was ",acc)
result = acc

#start of main
var indi_files =  splitstring(file,"",@[])
echo("the final result returned to indi_files ",indi_files)


Run

Output


17
string was found
25
string was found
4
the final result of splitting was @["asdfasdfasdf", 
"<  


   /OFX>"]
the final result returned to indi_files @["asdfasdfasdf"]



Re: How to return a seq[string] from proc

2019-07-20 Thread ravigupta001
Is returning in an else branch disallowed in Nim? I'm use to thinking about 
this in a functional manner, hence I was trying out for recursion here. 


Re: How to return a seq[string] from proc

2019-07-20 Thread mratsim
seq have value semantics, meaning they are copied.

You have 2 issues:

  * You don't return or assign to the implicit result value in your branch of 
`if found != -1:`
  * Your are shadowing the implicit result with `let result` in your other 
branch. `result` is implicit, already declared.




Re: How to return a seq[string] from proc

2019-07-20 Thread ravigupta001
Dawkot, thank you for the reply. Would you be able to expand on passing by 
value vs by reference? My understanding is that passing by reference is using a 
pointer to the original data. In my case I'm trying to make a copy of accu, and 
not just use it as a pointer. I looked at the docs sequtils and didn't see 
something for a deep copy. Appreciate the help!


Re: get a type via a string?

2019-07-20 Thread jasper
You can get the ssn typedef with this: 


macro ssnTypeImpl(T: typedesc): untyped =
  let ssnField = T.getType[1].getType[2][1]
  echo ssnField.getTypeInst.getImpl.treeRepr

ssnTypeImpl(Person)


Run


Re: Nim VS The World (CoffeeScript/Boo/PureBasic/C#/ES2018/Python)

2019-07-20 Thread Neil_H
PureBasic lacks many features (some of which I have mentioned here) that 
considering how long its been going should be included, it maybe okay for small 
apps but how many big applications have been produced with it? It kinda reminds 
me of a AutoIt but with a GUI designer...but unlike AutoIt and many other 
programming languages including Nim... its not free. 


Re: How to return a seq[string] from proc

2019-07-20 Thread dawkot
I think you didn't notice that seq is passed by value and not by reference.


Re: Vim 8 code completion plugin?

2019-07-20 Thread akavel
Thanks. I am trying to make it work, but after some debugging, it seems to me 
it may just got broken by Nim 0.20.2's change in handling stdout on Windows (by 
making nimlsp emit CR-CR-LFs when compiled with 0.20.2, instead of CR-LFs as 
required in the LSP protocol). I didn't have enough time to confirm this 100% 
without doubt, but I let myself report 
[nim#11801](https://github.com/nim-lang/Nim/issues/11801) anyway for 
clarification, as I am confused if it's possible to emit a raw LF on Windows 
with 0.20.2 at all. (I had trouble making choosenim work too, so I couldn't 
easily check if it works OK with 0.20.0 yet :/)


Re: wNim - Nim's Windows GUI Framework

2019-07-20 Thread Neil_H
That's cool, yep add it to the examples I think actually seeing the colors 
visually (why I wrote it) will always be better than just seeing the color as 
text.


Re: get a type via a string?

2019-07-20 Thread mratsim
Here you go


import macros, options

type
  ssnType = Option[int]

type
  Person = object
age: Option[int]
ssn: ssnType

proc isOptionInt(x: NimNode): bool =
  sameType(x, getType(Option[int]))

macro checkOptionInt(x: typed): untyped =
  result = newLit(x.isOptionInt)

echo checkOptionInt(typeof(Person().ssn))


Run

Note that you can turn a typedesc into a NimNode with `getType`, but there is 
no way to turn a NimNode representing a type back to a typedesc, and the 
corresponding feature request was closed 
([https://github.com/nim-lang/Nim/issues/6785)](https://github.com/nim-lang/Nim/issues/6785\)),
 so keep your typedescs around as long as possible.


Re: Wrap C library that needs CMake

2019-07-20 Thread hugogranstrom
Tank you :-) Awesome work from you too ;-) will test it out!


Re: Bind not working in Generic Procs.

2019-07-20 Thread solo989
Here's a better example of what I'm talking about.

Imagine you have two files foreignFile.nim and myFile.nim.

foreignFile.nim 


template p[T:int|float](a : T) =
  echo "Number:" & $a

template p(a : string) =
  echo "String:" & a

proc printVar*[T:int|float|string](a : T) =
  p(a)


Run

All the templates printVar should call are already defined but p isn't bound so 
it can potentially use new templates/procs/etc defined in the future.

myFile.nim 


import foreignFile

var x = 0

template p(a : int) =
  x += a
  echo x

p(7)

printVar(5)
printVar(5.0)
printVar("5.0")


Run

In this example you've inadvertently overwritten p from myFile when aProc is 
passed an int. It will echo 12 instead of Number:5. You only wanted to call 
your new p definition directly. In this case the generic printVar proc should 
probably be three seperate printVar definitions instead of


proc printVar*[T:int|float|string](a : T)


Run


Re: get a type via a string?

2019-07-20 Thread kaushalmodi
This is very related: 
[https://forum.nim-lang.org/t/5027#31537](https://forum.nim-lang.org/t/5027#31537).
 Does that help? 


Re: wNim - Nim's Windows GUI Framework

2019-07-20 Thread Ward
I like your idea. I modified it by using table to store the name and color. 
[https://gist.github.com/khchen/381c32741de901279532f3e2e0e8a6d8](https://gist.github.com/khchen/381c32741de901279532f3e2e0e8a6d8)

I am considering to add this code to wNim's examples :-)