nakkaya / ferret

Ferret is a free software lisp implementation for real time embedded control systems.

Home Page:https://ferret-lang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Elementary error: ‘str’ was not declared in this scope

0atman opened this issue · comments

Firstly, thank you so much @nakkaya, I'm so keen to get started with ferret! But I've fallen at the first hurdle: I can't get a simple hello world to work.

Why does:

(println "hello world")

work, but:

(println (str "hello" "world"))

not work? Here's the output:

$ ferret -ci cli.clj

23:18:16 info dir => ./
23:18:16 info file => cli.clj
23:18:18 info compiled => ./cli.cpp
23:18:18 info building => g++ -std=c++11 -x c++ cli.cpp
23:18:18 warning build error
23:18:18 warning cli.cpp: In function ‘void cli::main()’:
cli.cpp:2203:29: error: ‘str’ was not declared in this scope
           run(println(),run(str,obj<string>("hello",(number_t)5),obj<string>("world",(number_t)5)));

I can see the error clearly, ‘str’ was not declared in this scope, but how can that be? Thank you!

str is not yet implemented. Strings occupy too much space and memory when working with microcontrollers. So objects know how to print them selfs but not how to convert them selfs to a string yet. For now there is an alternate macro called new-string that will let you create a string in compile time. It is also useful when you wanna return just a string from a function and not want it interpreted as a FFI call.

So following would work,

(println (new-string "hello" " " "world"))

And so it does! thank you!

I note I can't reassign new-string with

(def str new-string)

perhaps that is just how the language works?

Also, the reason I thought str was available is that it's in the core language page:

(defmacro pin-mode [pin mode]
  (let [pin (if (number? pin)
              pin
              (str "number::to<number_t>(" (symbol-conversion pin) ")"))
        mode (-> mode name .toUpperCase)]
    `(cxx ~(str "::pinMode(" pin ", " mode ");"))))

here https://ferret-lang.org/#outline-container-sec-5

What does that mean?

Macros are evaluated at compile time and they are handled by real Clojure where you have access to str but on runtime there is no str. new-string is just a convenience macro not a function thats why you can not reassign same as regular Clojure.

Fascinating! Thank you for that explaination.