[nim-dev] Re: Nim vs Scala (not holywar). Compare functions for strings

  • From: Krux02 <arne.doering@xxxxxxx>
  • To: nim-dev@xxxxxxxxxxxxx
  • Date: Sat, 16 Jul 2016 13:44:35 +0000

Yes I really do like scala, and I think scala has a lot in the functional 
programming domain, that I would like to have in Nim, too.

Here is a pattern from scala that I like to use to avoid the creation of 
intermediate variables in object scope, when in practice only the result is 
interesting. It also tells the reader of the code, that the variables are also 
really only used in that context, and never accessed from outside. It is also 
very useful, when I want to use statements (Unit methods) in expressions.

```scala

def foo: (Int, Float) = {
  val a = 17
  val b = 4.0f
  (a,b)
}

// foo: (Int, Float)

val (a1,b1) = foo

// a1: Int = 17
// b1: Float = 4.0

val (a2,b2) = {
  val a = 17
  val b = 4.0f
  (a,b)
}

// a2: Int = 17
// b2: Float = 4.0

```

I tried to do the same in Nim, and this is how far I came with it. 

```nim
proc foo(): tuple[a:int, b:float] =
  let a = 17
  let b = 4.0
  (a, b)

var (a1,b1) = foo()

echo a1, b1

var (a2, b2) = (block:
  let a = 17
  let b = 4.0
  (a,b)
)
```

first of all, it is weired, that I need to use the block keyword here, and the 
() pair, otherwise the nim compiler would not accept that construct. It would 
really be nice, if that would not be necessary. But even then it still doesn't 
work, this is an example where the nim compiler generates non compileable C 
code.

Just for the curious people, even gnu c has an extension to do this: 
`<https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html#Statement-Exprs>`_


Other related posts: