GithubHelp home page GithubHelp logo

with's Introduction

with is fun, but as its own project it should have a splash image like "Inspired by a forbidden(TM) JavaScript feature!" -jrfondren

with is a simple macro to replace the deprecated {.with.} pragma in Nim. This macro allow you to access fields of an object or tuple without having to repeatedly type its name. It works by creating tiny templates with the same name as the field that expand to access to the field in the object.

Example:

type Foo = ref object
  first: int
  second: string
  third: float

var foo = Foo(first: 1, second: "two", third: 3.0)

with foo:
  echo first
  if true:
    third = float(first)
  echo second

with also works with named tuples:

var foo = (first: 1, second: "two")
with foo:
  first = 3
  second = "six"

Fields from the object can be shadowed by new declarations in the scope where the new declaration is made. The object field can still be accessed by using its full dot-expression notation:

var foo = (first: 1, second: "two")
with foo:
  assert first == 1
  if true:
    let first = "dragons"
    assert first == "dragons" 
    assert foo.first == 1
  assert first == 1
  assert foo.first == 1

For the brave, with blocks can be nested, or multiple objects can be passed in a tuple constructor. Make sure that field names are unique in the nested objects:

var foo = (first: 1, second: "two")
var bar = (alpha: 42)

with foo:
  with bar:
    first = alpha

with (foo,bar):
  first = alpha

with's People

Contributors

pmunch avatar recloser avatar zevv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

with's Issues

Doesn't work with inheritence

The following code won't compile:

import with

type
  Base = object of RootObj
    a: int

  Inherit = object of Base

var i = Inherit()
with i:
  echo a

Introducing with for Object members (property) cannot be assigned to when used with with

Before

proc vmAdd(self: Computer) =
  self.s.processor.memory3 = self.s.processor.memory1 + self.s.processor.memory2
  self.s.processor.programCounter = self.s.processor.programCounter + 4

after

proc vmAdd(self: Computer) =
  with self.s.processor:
    memory3 = memory1 + memory2
    programCounter = programCounter + 4

In the after code the s of the self in the with statement is underlined issue reported is template/generic instantiation of with from here

memory3 and ProgramCounter are underlined with
'memory3' cannot be assigned to
'programCounter' cannot be assigned to

Please note that I in in the very early days of learning nim so its entirely possible I've made mistakes. The Advent of code problem to which the above code belongs compiles and gives the correct answers.

related code where with seems to work fine

type
  OutputResponse* = enum
    HaltOnOutput = 1,
    ContinueOnOutput = 2,

type
  Properties = ref object
    actionOnOutput: OutputResponse
    runHasCompleted: bool
    input: seq[int64]
    output: seq[int64]

type
  State = ref object
    newOutputReady: bool
    processor: IntComputerProgramCounterFrame

type
  Computer* = ref object
    p: Properties
    s: State


proc initProperties(): Properties =
  result = new Properties
  with result:
    actionOnOutput = OutputResponse.HaltOnOutput
    runHasCompleted = false
    input = @[]
    output = @[]

proc initState(): State =
  result = new State
  with result:
    newOutputReady = false
    processor = newIntComputerProgramCounterFrame()

proc initComputer*(): Computer =
  result = new Computer
  with result:
    p = initProperties()
    s = initState()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.