Advent of Nim 2021

2021-11-30 Thread pietroppeter
as last year, I will try to blog about my solutions every day. Unlikely to keep 
it up for many days, but let's see: 


on top of that the idea would be to try and make visualization using cool nim 
libraries, but I am already hitting a problem on day 1...


TLS protocol negociation (TLS-ALPN)

2021-11-30 Thread icedquinn
PR for the low level bindings 


TLS protocol negociation (TLS-ALPN)

2021-11-30 Thread dom96
Happy to accept any patches that add this support. :)

If you want a bigger project: BearSSL support in the stdlib would be cool too :D


TLS protocol negociation (TLS-ALPN)

2021-11-30 Thread icedquinn
OpenSSL is what the standard runtime uses :shrug: I'm talking about getting 
this in stdlib so we can build http2 and grpc support on to the stdlib sockets.


Macros: why and/or when to use them?

2021-11-30 Thread kcvinu
Actually, my project was a GUI library based on win32 API. I have stopped it 
when I knew the limitations in modularity. Then after a few months, I've 
started it again with making benefit from "include" statement. In this method, 
All I need is to plan my code as a single file and properly order my types. And 
then split it to different modules. For example, one module for window class, 
one for button class etc. But that's not the way I want to do it. So I am 
silently following Nim community to see any improvements in modularity. One 
day, I saw a single file GUI library in Odin language forum. It is 2K line 
module. And it is based on SDL. I think if I follow that style in my project, I 
will end up a single file with 5K lines. I am not an expert in programming. I 
am a self learner. I found that Nim is a good language to do some nice gui 
projects. Anyways, I hope, one day everything will be better in Nim.


What is "Option[system.bool]"?

2021-11-30 Thread shirleyquirk
If you use a symbol from a module, you can generally use it unambiguously, e.g. 


import tables
var myTable:Table[int, string]

Run

But you could specify more strictly which module the type is defined in: 


var myTable: tables.Table[int, string]

Run

And sometimes people like to force all imported symbols to be explicitly 
defined, with e.g.: 


from tables import nil

Run

`bool` is a type defined in a module, like any other. You could even redefine 
it if you were particularly psychotic:

`evil.nim`


type bool = enum
  false = 3, true = 7


Run

and the two definitions could coexist in the same file, if you specified 
whether you meant `evil.bool` or `system.bool`


Macros: why and/or when to use them?

2021-11-30 Thread ElegantBeef
If we're going to talk about modularity, one could do worse than making modules 
instantiatable using type replacements. Take `strutils` presently it relies on 
`system.string`, so if someone makes their own string type it requires 
rewriting a fair bit of code, but if the module was written with generics in 
mind(using `init`/`new` that take parameters instead of `newString`) one could 
easily allow replacing all usages of `string` with `MyString` and instantiate a 
module with a given type. For a code example looking at a mathematical vector 
library: 


# vectormath.nim
type Vec2 = (float32, float32)
proc `+`*(a, b: Vec2): Vec2 = Vec2.init(a[0] + b[0], a[1] + b[1])
proc `-`*(a, b: Vec2): Vec2 = Vec2.init(a[0] - b[0], a[1] - b[1])
proc `*`*(a, b: Vec2): Vec2 = Vec2.init(a[0] * b[0], a[1] * b[1])
proc `/`*(a, b: Vec2): Vec2 = Vec2.init(a[0] / b[0], a[1] / b[1])

# myModule.nim
type Vec2Arr = array[2, float32]
proc init(_: typedesc[Vec2Arr], x, y: float32): Vec2Arr = [x, y]
import vectormath[Vec2: Vec2Arr] # Replaces all `Vec2` instances(doesnt 
declare the type in this module) with `Vec2Arr`
# vectormath has a internal concept that this needs to match,
# in this case `init`, `[]`, and operators for the fields
assert [10f32, 10f32] + [-1f32, 3f32] == [9f32, 13f32] # We can use 
operators normally.
assert [10f32, 10f32] * [-1f32, 3f32] == [-10f32, 30f32] # We can use 
operators normally.


Run

This allows a library that depends on given types, but users can easily swap 
types out, giving you more modularity.


TLS protocol negociation (TLS-ALPN)

2021-11-30 Thread shirleyquirk
Alternatively, bearssl seems to do this without mucking about with callbacks. 


Macros: why and/or when to use them?

2021-11-30 Thread Sixte
> And again, "everything depends on everything else" is the opposite of 
> "modularity", it cannot hurt to use the correct terms.

So, do you want to go for "a program is a single (albeit large) equation"

or do you want to go for orthogonalization (factoring out parts of the program 
and their equations will be evalutated seperately" ?


Macros: why and/or when to use them?

2021-11-30 Thread Araq
> But I know that I don't have any problem with modularity in other languages.

So try some F#, ML, Ocaml, Haskell...