|
CPallini wrote: they are coming back to the hatred rational, sensible, beloved C/C++ header/source file
FTFY
GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
|
|
|
|
|
It's for use in a partial class , so you could declare the partial method in the designer code for a WinForms app for example, and implement it in the user defined code for the same class. The difference is that the method can be called from the designer code because it will compile cleanly.
This is still the case if the implementation of the partial method is never provided: the compiler will remove the partial method and the call to it if the concrete version is not written, and you still won't get a compiler error.
Think of it as a sort-of optional private abstract method and you're about there.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Right. We use partial classes/methods all the time with EF. You extend and EF entity with extra properties and methods if needed. The entity can change in EF, but the partial class/method remains the same.
Very useful. use it all the time.
|
|
|
|
|
I use it frequently. I find it simpler to use than Interface.
__________________
Lord, grant me the serenity to accept that there are some things I just can’t keep up with, the determination to keep up with the things I must keep up with, and the wisdom to find a good RSS feed from someone who keeps up with what I’d like to, but just don’t have the damn bandwidth to handle right now.
© 2009, Rex Hammock
|
|
|
|
|
Where is YAGNI when you need it. Google trends[^] results sort of agree with your feelings, as do I.
|
|
|
|
|
Definitely YAGNI. Will definitely be asked about it in a job interview.
|
|
|
|
|
It's mostly for generated code
|
|
|
|
|
It's still pointless.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I can't say I have ever found a use for it - although that might be because I didn't know it existed until your post
|
|
|
|
|
It is most certainly NOT pointless. You just haven't found a use case for it yet.
|
|
|
|
|
The cool thing in the code below is that if you don't define LOGGING, not only does the partial method go away, but any calls to the method are also removed.
#define LOGGING
using System;
namespace PartialMethodTest
{
partial class Foo
{
partial void Log(string msg);
public void DoSomething()
{
Log("Fizbin");
}
}
#if LOGGING
partial class Foo
{
partial void Log(string msg)
{
Console.WriteLine(msg);
}
}
#endif
class Program
{
static void Main(string[] args)
{
new Foo().DoSomething();
}
}
}
Latest Article - A Concise Overview of Threads
Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny
Artificial intelligence is the only remedy for natural stupidity. - CDP1802
|
|
|
|
|
Quote: but any calls to the method are also removed. Wouldn't that be disastrous if the method is intended to produce side-effects?
|
|
|
|
|
CPallini wrote: if the method is intended to produce side-effects
If the programmer is a donkey, giving him/her a compiler is disastrous.
GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
|
|
|
|
|
Unless you are haskelling yourself too much, most methods do have side effects.
|
|
|
|
|
"Side" effects are really something that need to be reduced. Methods should have only intended effects and possibly no side effect.
If a method is to be implemented by the user of a class then it is part of the 'contract' to be respected when writing the user component - meaning it's up to the end user (programmer) to implement all the required effects and side effects.
GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
modified 16-Jan-19 10:56am.
|
|
|
|
|
Intended-side-effects were intended . That is side-effects in the functional meaning (in constrast with 'pure' functions, Haskell was the hint).
Unintended side effects are just evil.
|
|
|
|
|
Judging by the restrictions imposed on partial methods: they cannot have return value (only void ) or out parameters, they are intended to have side effects.
Whether or not it is disastrous to skip side effect is another question. In original example (logging) it is not.
|
|
|
|
|
Yes, indeed.
The original example showed a safe usage.
|
|
|
|
|
CPallini wrote: Wouldn't that be disastrous if the method is intended to produce side-effects?
Where I used to work, that was an every day occurrence, both in the code and in the meetings with the managers.
Latest Article - A Concise Overview of Threads
Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny
Artificial intelligence is the only remedy for natural stupidity. - CDP1802
|
|
|
|
|
|
I'm not sure such a code-remove by the compiler is 'cool'...
While removing unused code while optimizing is good thing, removing unimplemented code just because it signed 'partial' definitely illogical...
And to be honest - why would I define a private method and not implement it? It is definitely smells like a language feature added to support half-visual development with code generators... Outside of that context I see it very disturbing...
"The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge". Stephen Hawking, 1942- 2018
|
|
|
|
|
Yes, code generators me think.
|
|
|
|
|
The people defining and the people using may be different people - this is only applicable to partial classes, remember - so there is a very good chance that the partial definition and its implementation are in different files, just as InitializeComponent is defined in the designer.cs file of a WinForms app, and called from the .cs file.
I've not used it, but I can see advantages in a "team written" class, in that I write a "frame" which calls partial methods that are implemented by other team members in different files. As has been mentioned, it could be very useful for automated designers, allowing the user to define methods only when he needs them without having to mess around with delegates. Or tracing information only available in debug builds, which isn't even called in release giving slightly higher performance.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
That's not cool - it's obfuscatory.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
modified 16-Jan-19 14:22pm.
|
|
|
|
|
Exactly what I was going to write.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
|
|
|
|