funq: syntax that doesn't scare off C++ developers
Samuel Gaist
samuel.gaist at edeltech.ch
Fri Jul 25 22:16:55 UTC 2014
On 25 juil. 2014, at 10:53, Aaron J. Seigo <aseigo at kde.org> wrote:
> Hi ...
>
> So .. first things first: the git url I included only works for people who have
> a KDE commit account. Sorry about that :) Here is the public url that should
> work for everyone:
>
> git clone git://anongit.kde.org/scratch/aseigo/funq
>
> OK.. so the first thing that I'd really appreciate feedback on is the general
> feel of the funq syntax. While it intends to be a pragmatically functional
> language[1], I have tried to make it *look* more familiar. One developer I
> showed it to without telling them what it was asked if it was Java :)
>
> The intention there is to make it easier for developers mostly familiar with
> the Algol family of languages (C, C++, Java, Python ...) to be able to
> understand and write the code without too much rewiring of their brain.
>
> The other primary goal with the syntax is to keep it simple through clarity,
> consistency and trying to always provide "just one way to do things".
>
> So here is the basic anatomy of a funq function:
>
> tags functionName(parameter list) : preconditions
> {
> code block
> }
>
> Only the function name and code block are required and they do what one would
> expect.
>
> We can leave function tags for later discussion, but they offer hints to the
> compiler as to how the function is intended to be used (allowing additional
> compiler checks and some useful introspection tricks).
>
> The parameter list supports pattern matching. There is an example of a (not
> very efficient) fibonacci number function in the file:
>
> fibonacci(1) { 0 }
> fibonacci(2) { 1 }
> fibonacci(x) : std::isType(x, std::Int) && x > 2
> {
> fibonacci(x - 1) + fibonacci(x - 2)
> }
>
> These are actually the same function, and which version of them is called
> relies on the value of the parameter passed. fibonacci(1) calls the first
> version; fibonacci(2) calls the second; calling it with >2 calls the third
> version. any non-int or integer less than 1 will result in an error due to no
> matching function.
>
> Preconditions optionally follow the the parameter list and allow various
> boolean checks to be made on the parameters. The third fibonacci function
> version checks that x is an integer and greater than 2.
>
> A function version is only called if both the parameter list and preconditions
> match.
>
> There are no return types; functions are free to return different types.
>
> Variables are single-assignment (you can't assign a different value to a
> variable once it is set) and typing is inferred (but not dynamic, due in part
> to single-assignment). Due to single-assignment, there is no need for a
> comparison operator (==). The assignment operator does both assignment and
> checking. So:
>
> x = 1; // x is now 1.. obvious :)
> x = 2; // false!
> 1 = x; // true!
> x = "An awesome string"; // false!
> y = x; // true! y now also equals 1
> z = 3;
> y = z; // false!
> y = x; // true!
>
> In expressions expecting booleans such as the preconditions clause, true/false
> is the result of '='. In a function body '=' generates no error or an error
> corresponding to true / false. On error a function stops processing and
> returns an error at that point which, if not caught, will result in the
> process it is running in to crash. Sort of like an assert.
>
> That should hopefully give you enough information to be able to get a general
> feel for the code in examples/syntax_samples.fqt
>
> I would love to hear your thoughts on:
>
> * what you found not clear enough / easy enough to read
>
> * any inconsistencies you spot
>
> * any ideas for improving readability and ease of writing new code
I was wondering if using std as the name of funq's base library is a good choice. C++ developers might think that it's a wrapper of the STL library to use in funq.
Samuel
More information about the Funq-devel
mailing list