Skip to content

Commit

Permalink
[REVIEW] Update for review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
haxscramper committed Oct 31, 2021
1 parent 3c1a592 commit 82e3c15
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ modification, copying.
"""

block simple_object:
## Object can be defined in a `type` section, using `TypeName = object`
## and then listing all fields
## Object can be defined in a `type` section, using `TypeName = object`
## and then listing all fields
type
Simple = object
field1: int
field1: int
field2: int

## Object can be constructed using `TypeName(field: value, ...)` syntax.
## Fields that were not explicitly listed are set to their default values.
## To assert that particular field is explicitly initialized in constructor
## you can annotate it with `{.requiresinit.}` (for example of this see
## you can annotate it with `{.requiresinit.}` (for example of this see
## `t02_plain_object_requiresinit.nim`)
let it = Simple(field1: 12)

Expand All @@ -38,7 +38,7 @@ block simple_object:

block copying_defaulting:
## It is possible to create an object variable without explicitly initializing
## it. In that case all fields would be initialized to default state (just like
## it. In that case all fields would be initialized to default state (just like
## explicit initialization where omitted fields were defaulted)
type
Object = object
Expand All @@ -63,9 +63,9 @@ block copying_defaulting:


block no_fields:
## It is possible to define object with no fields.
## It is possible to define object with no fields.
type
NoFields = object
NoFields = object

let it = NoFields()

Expand All @@ -75,10 +75,10 @@ block using_complex_types_in_fields:
OtherPre = object
field1: int
field2: int

Object = object
## It is possible to put any type as a field value *except* for the same
## objet type *unless* it was defined as `ref object`. For example of this
## object type *unless* it was defined as `ref object`. For example of this
## see `t02_plain_object_illegal_recursion.nim`
intField: int

Expand All @@ -93,7 +93,7 @@ block using_complex_types_in_fields:
field1: int
field2: int

block editing_object:
block mutating_instances:
## After construction object's field can be modified
type
Object = object
Expand Down Expand Up @@ -121,16 +121,16 @@ block ref_object:
doAssert it.field1 == 12
doAssert it.field2 == 0

## When ref object variable is constructed without explicit initalization
## When ref object variable is constructed without explicit initalization
## it follows `ref` semantics - that is, it would be initialized to nil
## and modifying copy of an object would result in original being modified
## as well
var defaulted: Object

## Defaults to nil
doAssert isNil(defaulted)
doAssert isNil(defaulted)

## To create new object use `new` proc. Constructed object will have all
## To create new object use `new` proc. Constructed object will have all
## fields initialied to default values
new(defaulted)

Expand All @@ -143,4 +143,3 @@ block ref_object:

doAssert copy.field1 == 12
doAssert defaulted.field1 == 12

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
discard """
description: '''
By default object's fields are initialized to default values. To requires explicit
By default object's fields are initialized to default values. To requires explicit
initialization of a particular field it can be annotated with `{.requiresinit.}`
'''
Expand All @@ -22,7 +22,7 @@ block no_requires_field:
doAssert it.field2 == 0
doAssert it.field3 == 0

block has_requires_field:
block has_required_field:
type
Object = object
field1: int
Expand All @@ -43,8 +43,7 @@ block has_requires_field:
field: Object


## Compilation also fails due to
## Compilation also fails due to
## `Error: The Wrapper type requires the following fields to be initialized: field.`
## even though there was no explicit annotation for the field itself
let wrap = Wrapper()

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
discard """
description: '''
It is possible for objects to inherit fields from each other. Each
It is possible for objects to inherit fields from another. Each
object has at most one parent type.
'''
"""
Expand All @@ -14,7 +14,7 @@ block derive_from_root_obj:
Derived = object of Base
field3: int

## `Derived` type has all newly declared fields as well as all types
## `Derived` type has all newly declared fields as well as all types
## declared in parent objects.
discard Derived(field3: 3, field2: 2, field1: 1)

Expand Down Expand Up @@ -43,20 +43,19 @@ block derive_from_ref_root_obj:
## Or storing multuple derived objects in the same sequence
let baseSeq: seq[Base] = @[Base(), Derived()]

## To check whether based object is a subtype you can use built-in `of`
## To check whether based object is a subtype you can use built-in `of`
## operator
doAssert baseSeq[1] of Derived
doAssert not(baseSeq[0] of Derived)

## To convert base type back to derived you can use object conversion
doAssert Derived(base).f2 == 12

## If expressions cannot be converted to the derived type it will
## If expressions cannot be converted to the derived type it will
## result in `ObjectConversionDefect` begin raised
try:
discard Derived(Base())
doAssert false, "Had to raise defect"

except ObjectConversionDefect as def:
discard

0 comments on commit 82e3c15

Please sign in to comment.