Pythons None in Nim

2021-12-22 Thread Hlaaftana
That is the optimization.

Pythons None in Nim

2021-12-21 Thread juancarlospaco
I think is best to optimize Option implementations than using `-1` or something like that, is not worth the bugs...

Pythons None in Nim

2021-12-20 Thread ElegantBeef
Options are a type which wrap your type and add a bool, so when you do `some(100)` it makes an `Option[typeof(100)]` with the bool flag set to `true` to showcase how it works here is our own explanative not to be used implementation below: type Option*[T] = distinct (bool, T)

Pythons None in Nim

2021-12-20 Thread oyster
what does `some` mean? I have read but I am still get lost thanks

Pythons None in Nim

2021-12-20 Thread xigoi
I was once thinking of having a `DirtyOption` type that would use various “weird” values for None. dirtyNone(int) == dirtySome(int.low) dirtyNone(float) == dirtySome(NaN) dirtyNone(char) == dirtySome('\0') dirtyNone(string) == dirtySome("some invalid UTF-8 maybe?") di

Pythons None in Nim

2021-12-20 Thread SolitudeSF
kinda almost works import options type Option*[T: range] = object value: int func contains*[T: range](r: typedesc[T], i: int): bool = i >= T.low and i <= T.high func some*[T: range](r: T): Option[T] = result.value = r func

Pythons None in Nim

2021-12-20 Thread awr1
`int.low` is an inferior method, as it becomes a magic value unlike the option which forces you to check. However options as they currently are strike me as limited in the optimization department because internally an `Option[int]` will be represented as a `(has: bool; value: int)` in memory whe

Pythons None in Nim

2021-12-19 Thread kobi
I think it maps to either nil or Option none type. works better with option. also, u can type: some val (without parentheses) or val.some I personally think the options module should be in the Nim's prelude, receiving the blessing :-)

Pythons None in Nim

2021-12-19 Thread Araq
Fwiw I find `int.low` so useless (as there is no positive equivalent) that using it as `None` is perfectly fine.

Pythons None in Nim

2021-12-19 Thread Jocker
Yea, that's what I did. Looks like it is the best solution

Pythons None in Nim

2021-12-19 Thread demotomohiro
You can use type class to pass int or nil. Unlike `std/options`, which type of value you pass must be determined at compile time. Please read this for more details: proc foo(x: int or typeof(nil); y: int or typeof(nil))

Pythons None in Nim

2021-12-19 Thread pietroppeter
and since I am just running into this example while writing Python code, another case where a None in Python can be replaced by something else in Nim, is the case where a None stands for a value that has a default in terms of previous parameters. For example: def constant(size=5, s

Pythons None in Nim

2021-12-19 Thread Jocker
Will probably go with that, always having to write some(value) Run is kinda annoying

Pythons None in Nim

2021-12-19 Thread Jocker
yea, I already did that for other procs but it doesn't really make sense in my case right now

Pythons None in Nim

2021-12-18 Thread pietroppeter
Not your use case but in case you have a single optional input (it becomes impractical when optional inputs increase) another option (haha) is to have separate overloads (which usually can share the logic): proc foo(x: int) = echo “no y” proc foo(x: int, y: int) = echo “y: “, y

Pythons None in Nim

2021-12-18 Thread evoalg
For someone new to Nim and coming from python, I like your suggestion of using int.low... proc foo(x: int, y=int.low) = if y == int.low: echo "y wasn't given" else: echo "y = ", y foo(1, 2) # y = 2 foo(1) # y wasn't given Run int.l

Pythons None in Nim

2021-12-18 Thread ElegantBeef
Nim's `options` gives this abillity, alternatively you could use a specific value like `-1` or `int.low` but that's up to you. import std/options proc foo(x, y = none(int)) = if y.isNone: echo "y = None" else: echo y.get foo(some(1), some(2))

Pythons None in Nim

2021-12-18 Thread Jocker
I'm trying to rewrite some Python code in Nim but I am currently struggling at this codeblock. I have multiple optional Inputs (ints) and need to check whether they are set or not. def foo(x, y=None): if y is None: print("y=None") else: print(y)