summary refs log tree commit diff stats
path: root/lib/std/with.nim
blob: c7338b4e46e55cc19258608f2e17f918b64532ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#
#
#            Nim's Runtime Library
#        (c) Copyright 2020 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module implements the `with` macro for easy
## function chaining. See https://github.com/nim-lang/RFCs/issues/193
## and https://github.com/nim-lang/RFCs/issues/192 for details leading to this
## particular design.
##
## **Since:** version 1.2.

import macros, private / underscored_calls

macro with*(arg: typed; calls: varargs[untyped]): untyped =
  ## This macro provides `chaining`:idx: of function calls.
  ## It does so by patching every call in `calls` to
  ## use `arg` as the first argument.
  ##
  ## .. caution:: This evaluates `arg` multiple times!
  runnableExamples:
    var x = "yay"
    with x:
      add "abc"
      add "efg"
    doAssert x == "yayabcefg"

    var a = 44
    with a:
      += 4
      -= 5
    doAssert a == 43

  result = newNimNode(nnkStmtList, arg)
  underscoredCalls(result, calls, arg)
"> ## ARM64 based processor vm, ## Some Virtual machine: Nim's VM or JavaScript avr ## AVR based processor msp430 ## TI MSP430 microcontroller OsPlatform* {.pure.} = enum ## the OS this program will run on. none, dos, windows, os2, linux, morphos, skyos, solaris, irix, netbsd, freebsd, openbsd, aix, palmos, qnx, amiga, atari, netware, macos, macosx, haiku, js, nimVM, standalone const targetOS* = when defined(windows): OsPlatform.windows elif defined(dos): OsPlatform.dos elif defined(os2): OsPlatform.os2 elif defined(linux): OsPlatform.linux elif defined(morphos): OsPlatform.morphos elif defined(skyos): OsPlatform.skyos elif defined(solaris): OsPlatform.solaris elif defined(irix): OsPlatform.irix elif defined(netbsd): OsPlatform.netbsd elif defined(freebsd): OsPlatform.freebsd elif defined(openbsd): OsPlatform.openbsd elif defined(aix): OsPlatform.aix elif defined(palmos): OsPlatform.palmos elif defined(qnx): OsPlatform.qnx elif defined(amiga): OsPlatform.amiga elif defined(atari): OsPlatform.atari elif defined(netware): OsPlatform.netware elif defined(macosx): OsPlatform.macosx elif defined(macos): OsPlatform.macos elif defined(haiku): OsPlatform.haiku elif defined(js): OsPlatform.js elif defined(nimrodVM): OsPlatform.nimVM elif defined(standalone): OsPlatform.standalone else: OsPlatform.none ## the OS this program will run on. targetCPU* = when defined(i386): CpuPlatform.i386 elif defined(m68k): CpuPlatform.m68k elif defined(alpha): CpuPlatform.alpha elif defined(powerpc): CpuPlatform.powerpc elif defined(powerpc64): CpuPlatform.powerpc64 elif defined(powerpc64el): CpuPlatform.powerpc64el elif defined(sparc): CpuPlatform.sparc elif defined(ia64): CpuPlatform.ia64 elif defined(amd64): CpuPlatform.amd64 elif defined(mips): CpuPlatform.mips elif defined(mipsel): CpuPlatform.mipsel elif defined(arm): CpuPlatform.arm elif defined(arm64): CpuPlatform.arm64 elif defined(vm): CpuPlatform.vm elif defined(avr): CpuPlatform.avr elif defined(msp430): CpuPlatform.msp430 else: CpuPlatform.none ## the CPU this program will run on.