zlacker

[parent] [thread] 11 comments
1. sevens+(OP)[view] [source] 2025-12-05 13:24:12
I have been burned by sentinel values every time. Give me sum types instead. And while I’m piling on, this example makes no sense to me:

    proc fib[T: Fibable](a: T): T =
      if a <= 2:
        result = 1
      else:
        result = fib(a-1) + fib(a-2)
Integer is the only possible type for T in this implementation, so what was the point of defining Fibable?
replies(3): >>Hendri+b5 >>treefo+2D >>jibal+Ad1
2. Hendri+b5[view] [source] 2025-12-05 13:52:09
>>sevens+(OP)
I agree about sentinel values. Just return an error value.

I think the fib example is actually cool though. Integers are not the only possible domain. Everything that supports <=, +, and - is. Could be int, float, a vector/matrix, or even some weird custom type (providing that Nim has operator overloading, which it seems to).

May not make much sense to use anything other than int in this case, but it is just a toy example. I like the idea in general.

replies(1): >>sevens+Ze
◧◩
3. sevens+Ze[view] [source] [discussion] 2025-12-05 14:40:00
>>Hendri+b5
Well, I agree about Fibable, it’s fine. It’s the actual fib function that doesn’t work for me. T can only be integer, because the base case returns 1 and the function returns T. Therefore it doesn’t work for all Fibables, just for integers.
replies(2): >>cb321+No >>Hendri+oq
◧◩◪
4. cb321+No[view] [source] [discussion] 2025-12-05 15:23:49
>>sevens+Ze
In this case, it compiles & runs fine with floats (if you just delete the type constraint "Fibable") because the string "1" can be implicitly converted into float(1) { or 1.0 or 1f64 or float64(1) or 1'f64 or ..? }. You can think of the "1" and "2" as having an implicit "T(1)", "T(2)" -- which would also resolve your "doesn't work for me" if you prefer the explicitness. You don't have to trust me, either. You can try it with `echo fib(7.0)`.

Nim is Choice in many dimensions that other PLang's are insistently monosyllabic/stylistic about - gc or not or what kind, many kinds of spelling, new operator vs. overloaded old one, etc., etc., etc. Some people actually dislike choice because it allows others to choose differently and the ensuing entropy creates cognitive dissonance. Code formatters are maybe a good example of this? They may not phrase opposition as being "against choice" as explicitly as I am framing it, but I think the "My choices only, please!" sentiment is in there if they are self-aware.

replies(1): >>sevens+ZI
◧◩◪
5. Hendri+oq[view] [source] [discussion] 2025-12-05 15:30:42
>>sevens+Ze
I see, I misunderstood your complaint then.

However, the base case being 1 does not preclude other types than integers, as cb321 pointed out.

6. treefo+2D[view] [source] 2025-12-05 16:18:06
>>sevens+(OP)
There can be a lot of different integers, int16, int32 ... and unsigned variants. Even huge BigNum integers of any lengths.
◧◩◪◨
7. sevens+ZI[view] [source] [discussion] 2025-12-05 16:43:19
>>cb321+No
But given the definition of Fibable, it could be anything that supports + and - operators. That could be broader than numbers. You could define it for sets for example. How do you add the number 1 to the set of strings containing (“dog”, “cat”, and “bear”)? So I suppose I do have a complaint about Fibable, which is that it’s underconstrained.

Granted, I don’t know nim. Maybe you can’t define + and - operators for non numbers?

replies(1): >>cb321+gO
◧◩◪◨⬒
8. cb321+gO[view] [source] [discussion] 2025-12-05 17:05:50
>>sevens+ZI
Araq was probably trying to keep `Fibable` short for the point he was trying to make. So, your qualm might more be with his example than anything else.

You could add a `SomeNumber` predicate to the `concept` to address that concern. `SomeNumber` is a built-in typeclass (well, in `system.nim` anyway, but there are ways to use the Nim compiler without that or do a `from system import nil` or etc.).

Unmentioned in the article is a very rare compiler/PLang superpower (available at least in Nim 1, Nim 2) - `compiles`. So, the below will print out two lines - "2\n1\n":

    when compiles SomeNumber "hi": echo 1 else: echo 2
    when compiles SomeNumber 1.0: echo 1 else: echo 2
Last I knew "concept refinement" for new-style concepts was still a work in progress. Anyway, I'm not sure what is the most elegant way to incorporate this extra constraint, but I think it's a mistake to think it is unincorporatable.

To address your question about '+', you can define it for non-SomeNumber, but you can also define many new operators like `.+.` or `>>>` or whatever. So, it's up to your choice/judgement if the situation calls for `+` vs something else.

replies(1): >>sevens+mo1
9. jibal+Ad1[view] [source] 2025-12-05 18:56:32
>>sevens+(OP)
You're completely missing the point of this casual example in a blog post ... as evidenced by the fact that you omitted the type definition that preceded it, that is the whole point of the example. That it's not the best possible example is irrelevant. What is relevant is that the compiler can type check the code at the point of definition, not just at the point of instantiation.

And FWIW there are many possible types for T, as small integer constants are compatible with many types. And because of the "proc `<=`(a, b: Self): bool" in the concept definition of Fibable, the compiler knows that "2" is a constant of type T ... so any type that has a conversion proc for literals (remember that Nim has extensive compile-time metaprogramming features) can produce a value of its type given "2".

◧◩◪◨⬒⬓
10. sevens+mo1[view] [source] [discussion] 2025-12-05 19:43:55
>>cb321+gO
That’s fair. Sounds like the example was composed in haste and may not do the language justice.
replies(1): >>cb321+pw1
◧◩◪◨⬒⬓⬔
11. cb321+pw1[view] [source] [discussion] 2025-12-05 20:26:58
>>sevens+mo1
I think the example was chosen only for familiarity and is otherwise not great. Though it was the familiarity itself that probably helped you to so easily criticize it. So, what do I know? :-)

FWIW, the "catenation operator" in the Nim stdlib is ampersand `&`, not `+` which actually makes it better than most PLangs at visually disambiguating things like string (or other dynamic array, `seq[T]` in Nim) concatenation from arithmetic. So, `a&b` means `b` concatenated onto the end of `a` while `a+b` is the more usual commutative operation (i.e. same as `b+a`). Commutativity is not enforced by the basic dispatch on `+`, though such might be add-able as a compiler plugin.

Mostly, it's just a very flexible compiler / system.. like a static Lisp with a standard surface syntax closer to Python with a lot of parentheses made optional (but I think much more flexible and fluid than Python). Nim is far from perfect, but it makes programming feel like so much less boilerplate ceremony than most alternatives and also responds very well to speed/memory optimization effort.

replies(1): >>sevens+uO1
◧◩◪◨⬒⬓⬔⧯
12. sevens+uO1[view] [source] [discussion] 2025-12-05 21:57:35
>>cb321+pw1
Thanks for the discussion! I know a lot more about nim than I did this morning.
[go to top]