How to declare tick class as friend of another class.
Tick library here
https://github.com/pfultz2/Tick
example
`TICK_TRAIT(My_Trait)
{
template<class T_SomeType>
auto require(T&& x) -> valid<
decltype(T_SomeType::SomeFunc())
>;
};`
class SomeClass{
private:
friend My_Trait<SomeClass>;
SomeFunc(){}
}
Thanks
You need to add the class keyword to your friend declaration:
friend class My_Trait<SomeClass>;
Related
I'm able to do something like the following in TypeScript
class Foo {
private constructor () {}
}
so this constructor is accessible only from inside the class itself.
How to achieve the same functionality in Dart?
Just create a named constructor that starts with _
class Foo {
Foo._() {}
}
then the constructor Foo._() will be accessible only from its class (and library).
A method without any code must be something like this
class Foo {
Foo._();
}
Yes, It is possible, wanna add more information around it.
A constructor can be made private by using (_) underscore operator which means private in dart.
So a class can be declared as
class Foo {
Foo._() {}
}
so now, The class Foo doesn't have a default constructor
Foo foo = Foo(); // It will give compile time error
The same theory applied while extending class also, It's also impossible to call the private constructor if it declares in a separate file.
class FooBar extends Foo {
FooBar() : super._(); // This will give compile time error.
}
But both above functionality works if we use them in the same class or file respectively.
Foo foo = Foo._(); // It will work as calling from the same class
and
class FooBar extends Foo {
FooBar() : super._(); // This will work as both Foo and FooBar are declared in same file.
}
you can create following class in order to get a singleton instance
class Sample{
factory Sample() => _this ??= Sample._();
Sample._(); // you can add your custom code here
static Sample _this;
}
Now in the main function you can call the sample constructor
void main(){
/// this will return the _this instace from sample class
Sample sample = Sample();
}
just use abstract class.
Because you can't instantiate abstract class
So I'm trying to create a simple little program where I utilize mixins. I want to represent a bookstore and have two products (books, bags)..but I want the abstract class up top (Com) to define methods that can be applied to all products (objects) without changing the individual classes. However, I have no idea how to implement this. The method can be as simple as tracking if a certain book is in the bookstore.
Here is my current code:
abstract class Com {
not sure not sure
}
class Product extends Object with Com {
String name;
double price;
Product(this.name, this.price);
}
class Bag extends Product {
String typeofb;
Bag(name, price, this.typeofb) :super(name, price);
}
class Book extends Product {
String author;
String title;
Book(name, price, this.author, this.title):super(name, price);
}
void main() {
var b = new Book('Best Book Ever', 29.99,'Ed Baller & Eleanor Bigwig','Best
Book Ever');
}
A Dart mixin is currently just a bag of members that you can copy on the top of another class definitions.
It's similar to implementation inheritance (extends) except that you extend the superclass, but extend with the mixin. Since you can only have one super-class, mixins allows you a different (and much more restricted) way to share implementation that doesn't require the super-class to know about your methods.
What you describe here sounds like something the can just as well be handled using a common superclass. Just put the methods on Product and let Bag and Book both extend that class. If you don't have any subclass of Product which doesn't need the mixin methods, there is no reason to not include them in the Product class to begin with.
If you do want to use a mixin, you can write something like:
abstract class PriceMixin {
String get sku;
int get price => backend.lookupPriceBySku(sku);
}
abstract class Product {
final String sku;
Product(this.sku);
}
class Book extends Product with PriceMixin {
final String title;
Product(String sku, this.title) : super(sku);
}
class Bag extends Product with PriceMixin {
final String brand;
Product(String sku, this.brand) : super(sku);
}
class Brochure extends Product { // No PriceMixin since brochures are free.
final String name;
Brochure(String sku, this.name) : super(sku);
}
In Java I can create a static initializer like:
static { ... }
In Swift I can have:
class MyClass {
class var myVar:Int?
}
Is it possible to create some kind of class/static var initializer in Swift?
If you need a computed property accessible from the class type and you want it to be like a constant value, the best option is static keyword.
Type Property Syntax
“For computed type properties for class types, you can use the class keyword instead to allow subclasses to override the superclass’s implementation.” Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/pt/jEUH0.l
With class keyword a subclass can override the computed value.
Best solution:
class MyClass {
static var myVar: Int {
return 0
}
}
I begin with Dart and I would like to extend RectElement class to create a MyRectElement class which is able to move rectangle in SVG area :
import 'dart:html';
import 'dart:svg';
class MyRectElement extends RectElement{
int xOrigin;
int yOrigin;
factory MyRectElement() {
}
}
void main() {
var rect = new MyRectElement();
var container = querySelector("#container");
container.append(rect);
}
But RectElement has a factory constructor.
I must admit that I don't understand factory constructor even if I read lots of posts about it...
What should I put in MyRectElement factory contructor ?
Extending just the class is not supported.
You can build a Polymer element that extends a DOM element or if you don't want to use Polymer this question should provide some information Is it possible to create a Polymer element without Html?
i have the following domain class
class Session{
static hasMany=[lessons:Lesson]
}
class BasicSession extends Session{
}
class AdvancedSession extends Session{
}
know that Lesson is also a domain class:
class Lesson {
static belongsTo=[session:Session]
}
What's the Criteria that retrieves all lessons that belongs to Session subclass (BasicSession or AdvancedSession)
if i want to explain what i mean , i can write :
// lessons belong only to AdvancedSession
Lesson.createCriteria().list{
session{
eq('class','slm.abdennour.AdvancedSession') // !!!
}
}
After consulting this Issue, the solution is as what i said in question but , instead of String type , use Class type .
That it means :
eq('class',slm.abdennour.AdvancedSession)
and not
eq('class','slm.abdennour.AdvancedSession')