Ada: Renaming an Attribute with a Function? - function

In a recent question I asked about the ' operation, and learned it is used for getting at language-defined "attributes" of certain types. From what I can gather, there is no way to create your own attributes for your types.
I came across this line of code that I don't understand:
function Image(C: Ada.Containers.Count_Type) return String renames
Ada.Containers.Count_Type'Image;
What is this doing?

Certain attributes, like 'Read, 'Write, 'Input and 'Output, can be overridden by user-defined subprograms, like so:
procedure My_Write
(Stream : not null access Ada.Streams.Root_Stream_Type'Class;
Item : in My_Type);
for My_Type'Write Use My_Write;
The 'Image attribute can't be overridden.
The function definition in you example is a renaming of the attribute, allowing you to call the attribute as if it were a normal subprogram:
Image(My_Count);
instead of
Ada.Containers.Count_Type'Image(My_Count);

Related

In Lua, what is the difference between functions that use ":" and functions that do not? [duplicate]

This question already has answers here:
Difference between . and : in Lua
(3 answers)
Closed 7 years ago.
Let's say we have two function declarations:
function MyData:New
end
and
function New(MyData)
end
What is the difference between them? Does using : have any special purpose when it comes to inheritance and OOP? Can I only call functions declared with : by using :?
I'm coming from using only C# -- so if there's any comparison to be made, what would it be?
Adapted from the manual, end of ยง3.4.10:
The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement
function t:f (params) body end
is syntactic sugar for
t.f = function (self, params) body end
You should search SO as there are many questions about this but you have a set of questions so I can't say this is a duplicate.
Q. What is the difference between them?
A. The one with colon causes a method to be added to the MyData table, and the Lua interpreter to automatically insert a "self" before the first parameter when called, with this "self" pointing to the MyData instance that the "method" is being called upon. It is the same as writing MyData.New = function(self) end. The second signature has a parameter called MyData and is a global function. It is unrelated to the MyData table (or class).
Q. Does using ":" have any special purpose when it comes to inheritance and OOP?
A. No; it is merely syntactic sugar so that when you call MyData.New you can just write MyData:New() instead of the clunky looking MyData.New(MyData). Note that a "new" function is typically to create instances of a class, so you wouldn't have such a function be a method, rather just a function in the MyData table. For inheritence and OOP, you use metatables, and this does not interact with : in any special way.
Q. Can I only call functions declared with ":" by using ":"?
A. No, as mentioned, it just syntactic sugar, you can define one way and call a different way.
Q. I'm coming from using only C# -- so if there's any comparison to be made, what would it be?
A. For functions, the : is like the . in C#, whether used in a call or definition. The "." in Lua is more like "attribute", there is no equivalent in C# for functions.
MyData = {} -- a table
function MyData.func(self)
print('hello')
end
MyData:func()
MyData.func(MyData) -- same as previous
function MyData:func2() -- self is implicit
print('hello')
end
MyData:func2()
MyData.func2(MyData) -- same as previous
Note that MyData as defined above is not a class, because you cannot create "instances" of it (without doing extra work not shown there). Definitely read the Programming in Lua online book on the Lua.org website, lots of useful discussions of these notions.
Lua doesn't have functions declarations per se; It has function definition expressions. The syntax you have used is shorthand for a function definition expression and assignment.
The only difference in your examples is when the first statement is executed a new function is created and assigned to the field New in the table referenced by the variable MyData, whereas the second is an assignment to a non-field variable (local, if previously declared, otherwise global).
Keep in mind that these are only the first references to the created function values. Like any other value, you can assign references to functions to any variable and pass them as parameters.
If you add formal parameter usage to the bodies then there is another difference: The first has an implicit first parameter named self.
If you add function calling to the scenarios, the ":" syntax is used with an expression on the left. It should reference a table. The identifier to the right should be a field in that table and it should reference a function. The value of the left expression is passed as the first actual argument to the function with any additional arguments following it.
A function definition with a ":" is called a method. A function call with a ":" is called a method call. You can construct a function call to a function value that is a field in a table with the first argument being a reference to the table using any function call syntax you wish. The Lua method definition and method call syntax makes it easier, as if the function was an instance method. In this way, a Lua method is like a C# Extension Method.

X++ container to CSV string

I was wondering whether a container with values such as ["abc", 50, myDate, myRealNumber] can be converted to "abc","50","1/1/1900","-50.34" using a single function.
The con2Str global function fails if the input type is anything other than str.
I tried creating my own version of con2str function to use an "anyType" instead of str, but it fails because anyType cannot be assigned a different type after the first assignment.
If such a function exists (it does not), it would have to deal with strings containing quotes etc.
This is all handled in class CommaIo method writeExp.
But it writes to a file of cause.
Regarding your problem with anytype you could use the class SysAnyType which wraps your value in another object so that multiple assignments are possible.

using variable in argument - JSON target

I would like to use a variable (string) as part of my JSON target. Instead of simply coding for each section, like this:
$.each(data.portfolioitems.section1, function (k,v){...}
$.each(data.portfolioitems.section2, function (k,v){...}
$.each(data.portfolioitems.section3, function (k,v){...}
I would like to have a variable "varsection" that indicates which section should be called, like this:
$.each(data.portfolioitems.varsection, function (k,v){...}
As this exists, it seems that I am attempting to target the section "varsection", which of course doesn't exist.
I have found other topics where it was discussed how to use a variable as part of a JSON target, but it seems that none of the solutions I found are acceptable for this scenario where the target is an argument.
data.portfolioitems[varsection]

How do I make PhpStorm suggest a list of predefined input variables?

How do I make PhpStorm suggest input variables ?
In Java/C# and the like (with VS/Eclipse), when I have a function that receives enum-input-variable, like:
void func(SomeEnum var);
Whenever I type func(, the editor kindly suggests all the available enums (of type SomeEnum). I look for this same functionality in PHP, when I have a predefined set of input options (like DB's tables).
Maybe with the use of Intellilang or these # tags (PHPDocs ?) I can predefine such list ?
Currently you cannot limit the possible options for code completion in this regard: PHP simply has no such "thing" similar to Enum. The only limitation you can do is limit by expected parameter type (e.g. int variable will not be offered when parameter expects an array).
I think this will be the correct ticket to look for: http://youtrack.jetbrains.com/issue/WI-854 and maybe http://youtrack.jetbrains.com/issue/WI-3623
Kind of related (for array indexes completion): http://youtrack.jetbrains.com/issue/WI-3423

Stata: Check if a local macro is undefined

I am writing a Stata do file and I would like to provide default values if the user does not supply some parameters. To do so, I woud like to check if a macro is undefined.
I have come up with a hacky way to do this:
*** For a local macro with the name value:
if `value'1 != 1 {
...do stuff
}
But I would like to know if there is a idiomatic way to do this.
If it's undefined, the contents of the macro would be empty. You can do this:
if missing(`"`mymacroname'"') {
display "Macro is undefined"
}
The quotes aren't really needed if the macro will contain a number. The missing(x) function can handle strings and numbers. It is kind of like testing (x=="" | x==.) Placing `' around "`mymacroname'" allows the macro to contain quotes, as in local mymacroname `"foo"' `"bar"'.
The question asked for an idiomatic way to do this and across Stata programmers
if "`macroname'" != ""
is far by the most commonly used test of whether a macro is defined. Macros contain strings when they are defined, and this is the general usage; use of numeric characters is just a special case.
OP asked if there was a way to test if a local is undefined. The concept of defined/undefined doesn't really exist in Stata (which is confusing to users new to Stata but not new to programming).
A more Stata like way to think about this is if the local is missing or not missing. If a local has not been defined it is considered missing. But if a local has been defined to a missing value, like the empty string "", it is also considered missing.
So, there is no way in Stata to differentiate between a string local being undefined or being defined but contains a missing value, i.e. the empty string "". Both the answers already given does not capture the difference between localA (defined but as the empty string) and localB (undefined) in the example below:
*initiating localA to the empty string
local localA ""
*Both these conditions will evaluated identically
if missing("`localA'")
if missing("`localB'")
*Both these conditions will evaluated identically
if "`localA'" != ""
if "`localB'" != ""