Truly declarative language?

后端 未结 19 2191
梦毁少年i
梦毁少年i 2021-02-04 01:26

Does anyone know of a truly declarative language? The behavior I\'m looking for is kind of what Excel does, where I can define variables and formulas, and have the formula\'s re

19条回答
  •  孤城傲影
    2021-02-04 02:15

    It looks like you just want to make Z store a function instead of a value. In C#:

    var X = 10;    // define and assign two variables
    var Y = 20;
    
    Func Z = () => X + Y;  // declare a formula that uses these two variables
    
    Console.WriteLine(Z());
    
    X = 50;     // change one of the input variables
    
    Console.WriteLine(Z());
    

    So the equivalent of your ?-prefix syntax is a ()-suffix, but otherwise it's identical. A lambda is a "formula" in your terminology.

    Behind the scenes, the C# compiler builds almost exactly what you presented in your C# conceptual example: it makes X into a field in a compiler-generated class, and allocates an instance of that class when the code block is entered. So congratulations, you have re-discovered lambdas! :)

提交回复
热议问题