Skip to content

[Numbers\sum] should this accept :objects too? #2188

@RickBarretto

Description

@RickBarretto

Summary

Summing :objects that implements the add magic method would improve the expressiveness of the language.

Current Behavior

define :Vec2 [

    init: method [x y][
        this\x: x
        this\y: y
    ]

    add: method [other :Vec2][
        to :Vec2 #[
            x: this\x + other\x
            y: this\y + other\y
        ]
    ]
]

inspect a: <= to :Vec2 [1 2]!
; [ :Vec2
;         x  :        1 :integer
;         y  :        2 :integer
; ]

inspect b: <= to :Vec2 [3 4]!
; [ :Vec2
;         x  :        3 :integer
;         y  :        4 :integer
; ]

inspect c: <= add a b
; [ :Vec2
;         x  :        4 :integer
;         y  :        6 :integer
; ]

inspect sum @[a b c]
; ══╡ Type Error ╞═════════════════════════════════════════════════════════════════════════════ <script> ══
;
;   Erroneous type found
;
;   Invalid operation add
;   Between: :integer
;       and: :object
;
;   ┃ File: c:\Users\rickd\Documents\Projects\unitt\sum.art
;   ┃ Line: 20
;   ┃
;   ┃   18 ║  inspect c: <= add a b
;   ┃   19 ║
;   ┃   20 ║► inspect sum @[a b c]
;   ┃   21 ║

Expected Behavior

define :Vec2 [

    init: method [x y][
        this\x: x
        this\y: y
    ]

    add: method [other :Vec2][
        to :Vec2 #[
            x: this\x + other\x
            y: this\y + other\y
        ]
    ]
]

inspect a: <= to :Vec2 [1 2]!
; [ :Vec2
;         x  :        1 :integer
;         y  :        2 :integer
; ]

inspect b: <= to :Vec2 [3 4]!
; [ :Vec2
;         x  :        3 :integer
;         y  :        4 :integer
; ]

inspect c: <= add a b
; [ :Vec2
;         x  :        4 :integer
;         y  :        6 :integer
; ]

inspect sum @[a b c]
; [ :Vec2
;         x  :        8 :integer
;         y  :        12 :integer
; ]

Steps To Reproduce

No response

OS

Windows 11

Version

arturo 0.10.0 Arizona Bark (amd64/windows)

Anything else?

Issue reported from official Arturo's VS Code extension.

  • VS Code: Visual Studio Code 1.109.3 / Extension: v0.9.1

Is there an existing issue for this?

  • I have searched the existing issues

Metadata

Metadata

Labels

:objectbugSomething isn't workingoopType definitions & object-oriented aspects of Arturoopen discussionOpen discussion about features/ideas/suggestions→ Numbers

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions