The jQuery framework has a lot of functions which will either retrieve or mutate values depending on the parameters passed:
$(this).html(); // get the html
$(this).html('blah'); // set the html
Is there a standard name for functions which behave like this?
AFAIK, there is no "standard" name for a single function that acts both as a getter and as a setter. It's an idiom not very commonly used, but it's possible in some languages, and as long as it's well documented, there's no harm in using it.
Some call it a property.
Is there a standard name for functions which behave like this?
Getters and Setters.
EDIT:
I just realized that you want a single function to do this. The technical term for this is a bad idea. Unless your language provides properties, trying to rig up a single function to do this is very unreadable.
Related
When using Vue.js, when should you use a method or a computed property setter? There seems to be little distinction in the documentation, or numerous articles. Usually articles present computed property setters as little more than a footnote.
Given that both methods and setters accept parameters, is there a particular reason you'd use one or the other? As far as I can see methods would be all you need.
Edit:
This is literally not a repost because the linked SO answer contains the word setter once and only in vague passing:
computed properties are converted into a property of the Vue with a getter and sometimes a setter.
Great, so how does this elaborate on the subject of this post, when to use a SETTER vs a method?
Computed properties are cached so they can benefit you when it comes to performance. They do not work like methods, as they do not accept arguments.
I use them mostly to modify existing data or make it easier to access nested data.
The part about caching is something that can end up being a hassle. They will always cache unless their direct dependencies change. Properties within computed properties that are within controls blocks will usually not update the computed property(not seen as a direct dependency).
This is something you need to be aware of.
When using things like large v-for lists you will want to to take advantage of the caching ability of computed properties since unlike with a method you won't have to perform the logic inside of it over and over, unless the direct dependencies of the computed property change.
Computed properties should be used to display data relative to existing data. While methods should be used to do an action and/or change data.
I was wondering, what is real benifit, to have label together with function parameter. As far as I know, the following way
Not recommended
-(void) insertObject:(id)anObject:(unsigned int)index
Recommended
-(void) insertObject:(id)anObject atIndex:(unsigned int)index
Besides :
Enable function overloading, what else?
This doesn't enable function overloading. Obj-C doesn't have function overloading. What it does is name the method correctly. Your first method is named -insertObject:: and the second is named -insertObject:atIndex:. While both technically work, the former is considered extremely bad form, especially if you add even more parameters, e.g. -doSomething::::.
One of the oft-cited benefits of Obj-C is the method naming allows you to understand what code is doing by reading it without having to look at documentation. For example, if I have
[self initWithName:#"foo" andAge:13]
it's immediately obvious what the parameters are, but if I had
[self init:#"foo" :13]
then it's not obvious at all what these parameters are supposed to represent.
It also helps when you have similarly-named methods. For example, NSKeyValueCoding defines both -setValue:forKey: and -setValue:forUndefinedKey:. Quite obviously, if it had been named -setValue::, then there would be a collision here.
Because using the parameter labels makes the function call read somewhat more like an English sentence rather than a magic incantation.
It is fairly easy to create ABAP program objects using the built-in statements. But what about dictionary objects? There are a lot of function groups related to DDIC, but which ones to use? Or classes perhaps?
Check the function modules RPY_* - they are rather complete and reliable (and RFC-enabled :-)).
One class you can use is CL_REBF_DDIC_TABL. Use the static method PUT_COMPLETE. For a function module have a look at DD_CREATE_TABLE but I've never used this before.
You might want to check the specific DDIC elements you want to create and have a look at the Plugins of SAPlink http://code.google.com/p/saplink/ or ZAPlink http://code.google.com/p/zaplink/.
Both support a variety of elements and show the usage of functions to create them.
What I would like to do is be able to take a Dictionary of key value pairs and make the key the name of a variable and the value the value.
From searching the net seems to be very vague on whether this is possible.
The equivalent in PHP would be:
foreach($array as $key=>$val)
{
$$key = $val;
}
Thanks.
You have to declare variables in .Net CLR languages (not to be confused with .Net dynamic runtime languages) at compile time. More than that, it's generally better if you know the types of the variables as well. .Net programmers generally believe that this a good thing (the link is for C#, but the contents still apply).
What do you want to do with these variables? Tell us, and I'll bet we can give you a better way to accomplish the same thing.
This is doable but it is a slow operation. You'd have to use reflection to access the variables by their string representation.
If at all possible it will be much faster to use Generics to store the objects themselves as the keys. There's an example of that in VB.NET with a Dictionary here. Doing it this way means that there will be no casting or reflection needed at run-time. Plus it allows intellisense to work on the collection directly which is yet another awesome thing about Generics.
In C#4.0 we're going to get dynamic types, or objects whose "static type is dynamic", according to Anders. This will allow any method invocation resolution to happen at runtime rather than compile time. But will there be facility to bind the dynamic object to some sort of contract (and thereby also get full intellisense for it back), rather than allowing any call on it even if you know that is not likely to be valid.
I.e. instead of just
dynamic foo = GetSomeDynamicObject();
have the ability to cast or transform it to constrain it to a known contract, such as
IFoo foo2 = foo.To<IFoo>;
or even just
IFoo foo2 = foo as IFoo;
Can't find anything like that in the existing materials for C#4.0, but it seems like a logical extension of the dynamic paradigm. Anyone with more info?
I'm not aware of anything really resembling duck typing, I'm afraid. I've blogged about the idea, but I don't expect any support. It probably wouldn't be too hard to use Reflection.Emit to make a class which will generate an implementation of any given interface, taking a dynamic object in the constructor and just proxying each call through to it. Not ideal, but it might be a stopgap.
That's a cool idea.
If I understand you, you're describing/proposing a capability of the CLR, whereby, when you try and cast a dynamic object to an interface, it should look at what methods/properties the dynamic object supports and see if it has ones that effectively implement that interface. Then the CLR would take care of 'implementing IFoo' on the object, so you can then cast the dynamic object to an IFoo.
Almost certain that that will not be supported, but it's a interesting idea.
Tobias Hertkorn answered my question here with a link to his blogpost showing an example of how to use the Convert() method on MetaObject to return a dynamic proxy. It looks very promising.