arturo icon indicating copy to clipboard operation
arturo copied to clipboard

should it raise error?

Open retsyo opened this issue 1 year ago • 1 comments

I am using latest clone-and-bulit arturo on windows 10 64 bits

$> b: 2022

$> b.int
=> 2022

$> b.print
=> 2022

till now, I think this is a valid way to call function. Then

$> b.sin
=> 2022

$> b.sin 2*pi/4
=> 1.570796326794897

$> 2022.sin 2*pi/4
=> 1.0

$> sin 2*pi/4
=> 1.0

$> 2022.(1.0)
=> 1.0

$> 2022.1
=> 2022.1

so I am totally confuzed.

retsyo avatar Aug 15 '22 11:08 retsyo

@retsyo Hi! 😄

The only* valid, canonical way to call a function is:

function-name function-parameter-1, function-parameter-2, etc

In a few words: to call a function, it's just the function followed by its arguments.

The .something syntax in Arturo is solely for attributes or attributeLabels.

*To call a function, there is also the... call function and the long-awaited | pipe syntax, but for now I don't think it would be wise to overcomplicate things. :)


Now, to answer your question: why is that "working" in your example?

The explanation is hopefully simple:

Arturo - internally - is stack-based machine.

When you do F x y and F is a function, then the evaluator consumes the needed number of arguments/values from the right (that's the "syntactical" part) and then - during the execution phase - each function pops its arguments from the stack.

In the REPL (running just arturo from the command-line), this whole thing is a tiny-bit more... lax.

What does b.sin do?

Basically, it pushes one item onto the stack (b) and also sets an attribute (.sin) -- attributes are not passed to the main stack, but to a distinct one: the attribute stack.

And the REPL simply prints the top-most element of the stack; that is: b.

Note: Arturo's high syntax flexibility obviously comes with its own drawbacks. In this particular example, you may have had the intention of actually pushing a - not-so-useful - attribute onto the stack. In that case, Arturo cannot know and won't bother you at all. Obviously, this means that it won't bother you even when this wasn't your intention at all...

How to fix it?

Your code would normally work like this:

b: 2022

print b ; to print the value

print sin b ; to print the *sin* of the value

; or...
print sin 2*pi/4 

In Arturo's official website, you can find loads of examples where you can get even more familiar with the syntax. An even better idea would probably be to have a look into the In a nutshell section, which is pretty much the whole language, by example, in an ultra-fast example read.

In any case, feel free to ask away anything else you might need; I'm all ears! 😉

drkameleon avatar Aug 15 '22 12:08 drkameleon

Reopened by mistake.

drkameleon avatar Aug 24 '22 09:08 drkameleon