Re: Generics instantiate problems

2020-07-07 Thread xflywind
Here are complete codes which can succeed to compile.


import strformat
import ../src/shene/mcall


# Constructor injection
type
  Animal*[T] = object
sleepImpl: proc (a: T) {.nimcall, gcsafe.}
barkImpl: proc (a: T, b: int, c: int): string {.nimcall, gcsafe.}
danceImpl: proc (a: T, b: string): string {.nimcall, gcsafe.}
  
  Phone*[T] = object
playImpl: proc (a: T) {.nimcall, gcsafe.}
  
  Cat* = object
cid: int
  
  Iphone* = object
pid: int
  
  People*[T, U] = object
id: int
pet: must(Animal, T)
phone: must(Phone, U)


proc sleep*(a: Cat) =
  discard

proc bark*(a: Cat, b: int, c: int): string =
  result = fmt"{a.cid + b + c}"

proc dance*(a: Cat, b: string): string =
  result = fmt"{b}"

proc play*(a: Phone) =
  echo "Hello, the phone is playing the music."

proc play*(a: Iphone) =
  echo "Hello, the iphone is playing the music."

proc initCat*(cid: int): must(Animal, Cat) =
  result.cid = cid
  result.sleepImpl = sleep
  result.barkImpl = bark
  result.danceImpl = dance

proc initPhone*(pid: int): must(Phone, Iphone) =
  result.pid = pid
  result.playImpl = play

proc initPeople*[T, U](pet: Must[Animal[T], T], phone: Must[Phone[U], U]): 
People[T, U] =
  result.id = 2333
  result.pet = pet
  result.phone = phone

proc prepare(p: People[Cat, Iphone]) =
  doAssert p.pet.call(barkImpl, 13, 14) == "40"
  p.pet.call(sleepImpl)
  doAssert p.pet.call(danceImpl, "Hello") == "Hello"
  doAssert p.pet.cid == 13
  p.phone.call(playImpl)
  doAssert p.phone.pid == 2333

let pet = initCat(13)
let phone = initPhone(2333)
let p = initPeople[Cat, Iphone](pet, phone)
prepare(p)


Run


Re: Generics instantiate problems

2020-07-07 Thread xflywind
Hi, I want to implement `interface` or `dependency injection pattern`. Finally 
I want to implement an extensible web framework. For example, I have a Context 
object containing a Logger attributes. I just define the interfaces that Logger 
should satisfy. Then users can pass their own Logger instance to construct 
Context object. My goal is to achieve a loosely coupled application.

However using function pointers directly makes it hard to extend object. 


type
  Animal* = object
id: int
sleepImpl: proc (a: Animal) {.nimcall, gcsafe.}
barkImpl: proc (a: Animal, b: int, c: int): string {.nimcall, gcsafe.}
danceImpl: proc (a: Animal, b: string): string {.nimcall, gcsafe.}
  
  People* = object
pet: Animal


proc sleep(a: Animal) =
  discard

proc bark(a: Animal, b: int, c: int): string =
  result = $(a.id + b + c)

proc dance(a: Animal, b: string): string =
  result = b

proc newAnimal*(id: int): Animal =
  result.id = 1314
  result.sleepImpl = sleep
  result.barkImpl = bark
  result.danceImpl = dance

let people = People(pet: newAnimal(12))
doAssert people.pet.barkImpl(people.pet, 12, 14) == "1340"


Run

If I can't use generics to implement interfaces, I maybe implement something 
like `vtables` in C++ or `fat pointer` in Rust(Using macros or so).

Experiment Code: 
[https://github.com/xflywind/shene](https://github.com/xflywind/shene)


Re: Generics instantiate problems

2020-07-07 Thread spip
What do you want to code? Can you express it in English? Perhaps using generics 
is not the best way to express it.

Or do you want explicitly to use and learn generics?


Re: File not found error EVERY TIME

2020-07-07 Thread spip
When you type `nim -v` under Bash, what do you get? Just to be sure that your 
`file not found` message is about not finding the Nim compiler or your source 
file...


Re: File not found error EVERY TIME

2020-07-07 Thread jayv
I think we'll need a little more information to be able to properly help you. 
Depending on the exact error this could mean one of several things:

  * The compiler cannot find the input file `filename.nim`
  * Your program is importing another nim file that cannot be found.
  * Your program is trying to open another file that cannot be found.



Please copy and paste the command you are running and the exact output. It may 
also be helpful to show your nim file. If your nim file is more than a few 
lines, please try to create a small file, ideally less than 10 lines, that 
causes the same error to occur and post that as well.


Re: Generic function resolution

2020-07-07 Thread solo989
It may be a bug or an implementation detail.

Either way I would report the issue on github.


Re: Generic function resolution

2020-07-07 Thread solo989
It's not actually calling 


proc g[T](d: string)


Run

without B.

What it's doing is

instantiate g1 withB

instantiate g2 withB

instantiate g1 withC

instantiate g2 withC

call g1 withB

call g2 withC

It only comes up when you explicitly pass in generic parameters. All functions 
that match the generic parameters are instantiated.


Re: How to set up/start a Project?

2020-07-07 Thread sschwarzer
Just to be sure, are you using the command `Nim` (uppercase N) or `nim` 
(lowercase n)? The command is `nim` (lowercase n).


Re: How to set up/start a Project?

2020-07-07 Thread XxDiCaprioxX
And there is my next problem. If i try to run anything with Nim c -r 
filename.nim it gives a file not found error. even if i use f6 in VS Code


Re: How to set up/start a Project?

2020-07-07 Thread XxDiCaprioxX
Thank you for the links it was hard for me to find anything


Re: How to set up/start a Project?

2020-07-07 Thread XxDiCaprioxX
Thank you, I'll look into it


File not found error EVERY TIME

2020-07-07 Thread XxDiCaprioxX
Every time I try to run a nim file in BASH (Nim c -r filename.nim) or compile 
and run in VS Code it gives me a file not found error. Is there a way to fix 
this?


Re: How to set up/start a Project?

2020-07-07 Thread treeform
This is project structure I use: 
[https://github.com/treeform/nimtemplate](https://github.com/treeform/nimtemplate)


Generics instantiate problems

2020-07-07 Thread xflywind
Hello, I have some problems with generics.

I have four similar situations in which only one can succeed.

Failed case one: 
[https://play.nim-lang.org/#ix=2r7Q](https://play.nim-lang.org/#ix=2r7Q)


type
  Must*[U: object; T: object | ref object] = object
impl*: U
data*: T
  
  Animal[T] = object
  Cat = object

template must(a, b: typed): untyped =
  Must[a[b], b]


var a: must(Animal, Cat)

# failed
proc initZ[T](must: Must[Animal[T], T]) =
  discard


Run

Failed case two: 
[https://play.nim-lang.org/#ix=2r7R](https://play.nim-lang.org/#ix=2r7R)


type
  Must*[U: object; T: object | ref object] = object
impl*: U
data*: T
  
  Animal[T] = object
  Cat = object

template must(a, b: typed): untyped =
  Must[a[b], b]


var a: must(Animal, Cat)

# failed
proc init[T](must: must(Animal, T)) =
  discard


Run

Failed case three: 
[https://play.nim-lang.org/#ix=2r7S](https://play.nim-lang.org/#ix=2r7S)


type
  Must*[U; T: object | ref object] = object
impl*: U
data*: T
  
  Animal[T] = object
  Cat = object

template must(a, b: typed): untyped =
  Must[a[b], b]


var a: must(Animal, Cat)

# failed
proc init[T](must: must(Animal, T)) =
  discard


Run

Successive Case: 
[https://play.nim-lang.org/#ix=2r7U](https://play.nim-lang.org/#ix=2r7U)


type
  Must*[U; T: object | ref object] = object
impl*: U
data*: T
  
  Animal[T] = object
  Cat = object

template must(a, b: typed): untyped =
  Must[a[b], b]


var a: must(Animal, Cat)

# succeded
proc initZ[T](must: Must[Animal[T], T]) =
  discard

initZ[Cat](must = Must[Animal[Cat], Cat]())


Run


Generic instantiate inproblems

2020-07-07 Thread xflywind
Hello, I have some problems with generics.

I have four similar situations in which only one can succeed.

Failed case one: 
[https://play.nim-lang.org/#ix=2r7Q](https://play.nim-lang.org/#ix=2r7Q)


type
  Must*[U: object; T: object | ref object] = object
impl*: U
data*: T
  
  Animal[T] = object
  Cat = object

template must(a, b: typed): untyped =
  Must[a[b], b]


var a: must(Animal, Cat)

# failed
proc initZ[T](must: Must[Animal[T], T]) =
  discard


Run

Failed case two: 
[https://play.nim-lang.org/#ix=2r7R](https://play.nim-lang.org/#ix=2r7R)


type
  Must*[U: object; T: object | ref object] = object
impl*: U
data*: T
  
  Animal[T] = object
  Cat = object

template must(a, b: typed): untyped =
  Must[a[b], b]


var a: must(Animal, Cat)

# failed
proc init[T](must: must(Animal, T)) =
  discard


Run

Failed case three: 
[https://play.nim-lang.org/#ix=2r7S](https://play.nim-lang.org/#ix=2r7S)


type
  Must*[U; T: object | ref object] = object
impl*: U
data*: T
  
  Animal[T] = object
  Cat = object

template must(a, b: typed): untyped =
  Must[a[b], b]


var a: must(Animal, Cat)

# failed
proc init[T](must: must(Animal, T)) =
  discard


Run

Successive Case: 
[https://play.nim-lang.org/#ix=2r7U](https://play.nim-lang.org/#ix=2r7U)


type
  Must*[U; T: object | ref object] = object
impl*: U
data*: T
  
  Animal[T] = object
  Cat = object

template must(a, b: typed): untyped =
  Must[a[b], b]


var a: must(Animal, Cat)

# succeded
proc initZ[T](must: Must[Animal[T], T]) =
  discard

initZ[Cat](must = Must[Animal[Cat], Cat]())


Run


Re: Why Seq search is faster than Table search

2020-07-07 Thread erikenglund
Perhaps try the new --asm command option?


Re: How to set up/start a Project?

2020-07-07 Thread sschwarzer
There's the Nim package manager [Nimble](https://github.com/nim-lang/nimble) 
and its README has a section on [project 
structure](https://github.com/nim-lang/nimble#project-structure).

Also, if you use `nimble init` on the command line, it will create the 
structure for you to have an easy start. :-) The example files also have 
imports so you see how a file references others.


Re: How to set up/start a Project?

2020-07-07 Thread Stefan_Salewski
Have you tried reading some tutorials?

[https://nim-lang.org/learn.html](https://nim-lang.org/learn.html)

There are also videos at youtube.

Generally in Nim we do not but all objects in its own source file as it is 
common in Java. So generally You just create one single text file for your 
code, and you import modules like strutils, sequitils, tables or whatever you 
need. Many stuff is already available without import from system module. For 
libraries not directly available in Nim we have external packages and the 
nimble or nimph package manager. Of course when your own code grows it may make 
sense to divide it in multiple modules. For a chess game you may have your 
module engine for the chess algorithm, and your module board for the display. 
Maybe one more for saving games to disk.

The official tutorial has section about modules:

[https://nim-lang.org/docs/tut1.html#modules](https://nim-lang.org/docs/tut1.html#modules)

I have also written something about modules:

[http://ssalewski.de/nimprogramming.html#_modules](http://ssalewski.de/nimprogramming.html#_modules)

The other tutorials may contain more details.