MichaelRFairhurst / wake-compiler

C++ compiler for the programming language Wake: a fast, expressive, typesafe language built with testability from the ground up.
https://cdn.rawgit.com/MichaelRFairhurst/wake-compiler/docs/index.html
MIT License
56 stars 7 forks source link

Proposal: var expressions #96

Open MichaelRFairhurst opened 9 years ago

MichaelRFairhurst commented 9 years ago

Dangerous idea to be sure, but, just like we can say

foreach(getEntities()) { save(Entity); }

and the compiler knows that that returns Entity[] to make the var Entity automatically, we should be able to write

var getEntities();
Entity[].map(save);

which creates a var named Entity[] automatically.

Gets pretty dangerous/cool, things like

var 5;
var "Hello World!";

Syntax is an issue, though, as the token stream

|var|Num|...

could end as

var Num = 5;

or

var Num + 5;

Probably needs to be a different keyword, like

def getEntities();
def "Hello World!";
def 5;
return Entity[].getSize() + Text.getSize() + Num;

also need to consider how this would interact with the proposed immutable variable declarations

MichaelRFairhurst commented 9 years ago

I think mutable/immutable was planned on 'var' and 'def', or 'var' and 'the'. Why not solve this more syntactically than a new keyword?

var(getEntities()); // mutable var Entity[] def("Hello World"); // immutable var Text

this just leaves the question of how useful this is vs how dangerous it is.

MichaelRFairhurst commented 9 years ago

It did occur to me that this would make for some nice DSLs.

def wockito();
def projectRoot(".")
def default(Project)Builder();
def default(File)Structure();

Build.my(Files);

Might be interesting to let loose!