User Tools

Site Tools


with

Table of Contents

with

with - a compound statement for short-cut access to fields of a user-defined type

syntax

with name do
  [statement]
  [statement]
end
  • name is a variable that is declared as a (nested) user-defined type instance

See also records

details

A with statement is a compound or block statement that can be used to shorten access notation of user-defined type fields, whereby the user-defined type instance specified as the name of the with statement is replaced by a dot.

With statements can be nested up to three levels, whereby each level is represented by a dot, counted from the innermost with statement. This means that the innermost with statement is referenced by one dot and the next outer with statement by two dots, etc.

The name of a with statement can be any user-defined type instance, including nested instances, starting with and/or separated by dots.

The with statement is purely a cosmetic statement, i.e. jumps can be made freely to and from with statements without affecting the field references. It also means that with statements do not have an explicit scope.

example

' SharpBASIC 'with' programming example
' -------------------------------------
incl "lib/sys.sbi";

record A is
  x: int;
  y: int;
end

record B is
  z: int;
  a: A;
end

dim a: A;
dim b: B;

main do

  a.x = 5;
  a.y = 4;
  b.z = 10;
  
  jump @calculate;        ' this is ok
  
  print(b.z);             ' not printed

  with b do               ' block 1 (...)
    with b.a do           ' block 2 (..)
      with a do           ' block 3 (.)
        @calculate;
        ..x = .x * ...z;
        ..y = .y * ...z;
      end
    end
  end

  with b do
    with .a do
      print(.x);
      print(.y);
    end
    print(.z);
  end

end
Output:

50

40

10

with.txt · Last modified: 2023/07/12 17:52 by admin