Questions tagged [default-implementation]
30 questions
91
votes
4 answers
Can an interface method have a body?
I know that an interface is like a 100% pure abstract class. So, it can't have method implementation in it. But, I saw a strange code. Can anyone explain it?
Code Snippet:
interface Whoa {
public static void doStuff() {
…
user3034861
51
votes
2 answers
What does an ampersand after this assignment operator mean?
I was reading through this nice answer regarding the "Rule-of-five" and I've noticed something that I don't recall seeing before:
class C {
...
C& operator=(const C&) & = default;
C& operator=(C&&) & = default;
...
};
What is the purpose of…

Mihai Todor
- 8,014
- 9
- 49
- 86
27
votes
3 answers
Can you extend the default JsonConverter used in JSON.NET for collections?
I'm trying to write a custom JsonConverter for cases where a person subclasses a list or collection, but then adds extra properties to the subclass (see here). The current implementation of JSON.NET just changes the list into an array of child…

Mark A. Donohoe
- 28,442
- 25
- 137
- 286
24
votes
6 answers
How do I provide a default implementation for an Objective-C protocol?
I'd like to specify an Objective-C protocol with an optional routine. When the routine is not implemented by a class conforming to the protocol I'd like to use a default implementation in its place. Is there a place in the protocol itself where I…

fbrereto
- 35,429
- 19
- 126
- 178
9
votes
1 answer
C# 8.0 - Can't use default interface implementations
I recently read about C# 8.0 having interface default implementations, so i went into my project and tried it out, but i was met with an error instead. Target runtime doesn't support default interface implementation. Is there any way to fix this…

AndrewToasterr
- 459
- 1
- 5
- 16
9
votes
3 answers
Is it safe to place definition of specialization of template member function (withOUT default body) in source file?
Here's what I mean:
// test.h
class cls
{
public:
template< typename T >
void f( T t );
};
-
// test.cpp
template<>
void cls::f( const char* )
{
}
-
// main.cpp
int main()
{
cls c;
double x = .0;
c.f( x ); // gives EXPECTED…

Kiril Kirov
- 37,467
- 22
- 115
- 187
7
votes
2 answers
Rust calling default implementation of function in specialized version
I have a trait in Rust that offers a few default implementations for its functions.
trait MyTrait {
fn do_something(&self);
fn say_hello(&self) {
println!("Hello I am default");
}
}
Some implementors extend this trait and use…

Rüdiger Ludwig
- 115
- 8
7
votes
2 answers
Open Closed Principle Vs Default Implementation
Java 8 introduced the concept of default implementation for interfaces? Isn't this violating Open Closed Principle, since based on the example on https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html, it seems like you can always…

P. Sharma
- 71
- 5
4
votes
1 answer
How can you provide default implementations for UIPageViewControllerDataSource?
I assume that the answer to this question will address issues with Objective-C protocols in general, but this is the first problem of this type that I've come across.
I expect for these methods to be used, when implementing…
user652038
3
votes
2 answers
Calling protocol default implementation from regular method, when protocol has associated type
I have a protocol that has a static method with a default parameter. I want to change the default value in a class that implements the protocol. Essentially doing what is easily done with classes and super.
I only have a solution when the Protocol…

ThinkChaos
- 1,803
- 2
- 13
- 21
2
votes
1 answer
Why does the compiler not see the default code in a protocol?
Edit: I have restated and hopefully clarified this question over here. Now I've added the solution.
I've defined a function (see foo() in attached example) as a default function for structs adopting my protocol. It applies the + operator defined in…

Tchelyzt
- 161
- 1
- 10
2
votes
1 answer
Implementation of `first` in Arrow library
I don't understand the implementation of first in the library.
first seems to be defined recursively with *** -- I don't see when the recursion shall end!?
first :: a b c -> a (b,d) (c,d)
first = (*** id)
and
(***) :: a b c -> a b' c' -> a (b,b')…

user3680029
- 179
- 8
2
votes
0 answers
What does 'impl MyTrait' do without 'for MyStruct' in Rust?
Traits in Rust allow default implementation for trait methods: you can write some implementation right inside trait MyTrait {...} and it will be used in impl MyTrait for MyStruct later.
However, you can also write plain impl MyTrait, which does not…

yeputons
- 8,478
- 34
- 67
2
votes
1 answer
Why were default methods included interfaces in Java 8 instead of adding additional interfaces to the Collection Framework?
Normally, an interface would be frozen once released into production.
Hence, if you need added functionality, your option in Java would be to extend an existing interface into a new interface, which describes the added functionality.
This ensures…

Anders Clausen
- 21
- 2
1
vote
3 answers
Generic class to create concrete class automatically
Is there a way to take an interface, say:
///
/// Interface containing operators which operate on T
///
public interface IScalarOperators
{
// Adds two T objects
IOperateScalar OperatorAdd { get; }
// Subtracts…

MPavlak
- 2,133
- 1
- 23
- 38