MEMBERS
Method
5 min
actions do not exist in shift better said the type action is an alias to a method that returns void type action\<typearray> = method\<typearray, void>; typearray is a dependent typed array of types it's definition is as follows type typearray\<s\ int> = array\<type,s>(); see dependent typing for more information void is a keyword alias to type void type void = void; void being a keyword is always available and guarenteed to be the same type this is the same as c# and it's keyword type alias int vs int32 void itself is a type alias to a type dependent empty array type void = array<0>; bringing these 2 concepts together gives us action which is a type alias to a method with a type argument for input, and a fixed output of type void which is just itself array<0> philosophically this helps unify methods with output and no output there is always input and there is always output emptyset might idealogically be the value of either of those two dimensions action and method both has keyword supplied type aliases for scoping convenience type action = action;//always in scope, and takes first precendence in type resolution type method = method;//always in scope, and takes first precendence in type resolution methods taking the above information, all method definitions in constructors are syntactic sugar to a method field method is a record type and is immutable some example methods in their common format you'd experience in c# and then the field syntax shift int plustwo(int foo) { return foo + 2; } //identical behavior/mechanics as above, just a different syntax for writing //long hand lambda syntax for block definition method\<int,int> plustwo = (int foo) { return foo + 2; }; //short hand lambda syntax is supported for trivial blocks method\<int,int> plustwo = x => x + 2; //type inference on both the input and output of the method method plustwo int x => x + 2; //separate example just to demostrate multiple parameters //type inference infers method to be method<\[int,int],int> method addtogether (int a, int b) => a + b; c# developers will find the initial syntax most common and for complex methods the most commonly used some examples of void return types from c# applied to shift shift //example 1 void somesideeffectmethod(complextype foo) { var json = jsonconvert serialize(foo); file writealltext(" /saveme json", foo); } //example 2 action\<complextype> somesideeffectmethod => (somesideeffectmethod foo) { var json = jsonconvert serialize(foo); file writealltext(" /saveme json", foo); } //example 3 action\<complextype> somesideeffectmethod => (somesideeffectmethod foo) { var json = jsonconvert serialize(foo); file writealltext(" /saveme json", foo); return; } //example 4 action\<complextype> somesideeffectmethod => (somesideeffectmethod foo) { var json = jsonconvert serialize(foo); file writealltext(" /saveme json", foo); return new void(); } all 4 examples are the same, just using different syntax and amounts of inference the first form is the most likely form you'd see such as thing in a service very similiar to c# unpacking what's going on just like before with the method examples; example 1 is syntactic sugar for defining an action field hence the only diffference between 1 and 2 being the syntax it's common in c# when having a void return type to use no return statements this is shown in example 1 and 2 but in shift everything has an output, which means there is always a returned value the compiler infers that when a void return type is specified that the final line of the block will return a new instance of void furthermore the compiler understands that a return statement without a value, is being asked to return a new void; array's are record types in shift this means they have equality checking based on their contents, not their instance identifier shift var output = somesideeffectmethod(foo); var emptyset = new array<0>(); array<0> bar;//showing off type default values var areequivalant = output == emptyset && output = bar ; var sametype = output@ == emptyset@ && output@ == bar@ ; console writeline(areequivalant);//outputs true variable bar was thrown in to also demostrate the equivalence of type aliases void and array<0> are the same type void just an alias this means anywhere you could use the alias you can use what the alias is defined as the core take away and thing to remember is a type alias is no inherietence void does not inherit from array<0>, it is array<0> with a different name for type resolution purposes some more examples hammering home the type alias versus the definition are interchangable shift //example 5 action\<complextype> somesideeffectmethod => (somesideeffectmethod foo) { var json = jsonconvert serialize(foo); file writealltext(" /saveme json", foo); return new array<0>(); } //example 6 action\<complextype> somesideeffectmethod => (somesideeffectmethod foo) { var json = jsonconvert serialize(foo); file writealltext(" /saveme json", foo); var emptysettoreturn = new array<0>(); return emptysettoreturn; } //example 6 action\<complextype> somesideeffectmethod => (somesideeffectmethod foo) { var json = jsonconvert serialize(foo); file writealltext(" /saveme json", foo); array<0> emptysettoreturn; return emptysettoreturn; }
