Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document what mixins actually do #1383

Closed
karelbilek opened this issue Feb 9, 2016 · 10 comments
Closed

Document what mixins actually do #1383

karelbilek opened this issue Feb 9, 2016 · 10 comments

Comments

@karelbilek
Copy link
Contributor

I have no idea what mixins do and how it works.

I am adding this both as a documentation issue and as a cry for help, because I really don't know what it does. It seems it can be useful to me but I am not sure.

@asolove
Copy link
Contributor

asolove commented Aug 21, 2017

I think this was primarily present to model React mixins, which are now deprecated, and I can't seem to find recent use of this. Might be worth checking if they can be removed from the codebase or if there is still code that uses it, and if so document it.

@rvion
Copy link

rvion commented Feb 19, 2018

mixins doesn't appear in the current doc website https://flow.org/en/docs/

but it was present in the doc at some point:

5c4297b

+  ## Mixins
+
+  You can declare a class which mixes in 1 or more other classes with the
+  `mixins` keyword. Mixing class `B` into class `A` copies `B`'s fields and
+  methods into `A`. Note, however, that any fields or methods that `B` inherits
+  are not copied over. Mixins are for code reuse, not for multiple inheritance.
+

mode examples in the diff

⚠️ BTW, the commit title says: "and adds a note about similarity to TypeScript and Dart." but there is no mention of TypeScript or Dart in the commit

some more infos about mixins can be found in commits:

448fcb3

interface and declare class share a lot of implementation details, but they also have a bunch of differences. their bodies are identical (for now, though it doesn't really make sense for an interface to have statics).

however, interface can have multiple extends, but no mixins; declare class can have mixins, but only one extends. interfaces' ids are types, classes' are also values.

12741b0

When processing a spec, if we see a mixins object, we resolve those in
turn (recursively, as mixins can have mixins), actually mix them in, then emit
the class type.

b72d1c6

Multiple inheritance is available via a new clause declaring 'mixins': declare class Foo extends Bar mixins Qux.

@rvion
Copy link

rvion commented Feb 19, 2018

the page about mixins have been deleted by this commit.

360d6dc

commit 360d6dc429fb76ddfee26c691d06f70c8ac5c809
Author: James Kyle <[email protected]>
Date:   Mon Mar 20 14:25:51 2017 -0700

    [PR] Website tweaks

    Summary:
    ![330f 1](https://cloud.githubusercontent.com/assets/952783/23810160/4a98f766-0585-11e7-85b0-85db9c371343.gif)
    Closes https://github.com/facebook/flow/pull/3498

    Differential Revision: D4690749

    Pulled By: mroch

    fbshipit-source-id: 06f246ae471cb86478c78cc60ef772be8a1273db

it's hard to say if other content has been deleted, as this commit rename and rewrites a lot of files from the documentation website, but I have the impression that this commit removed some other useful content too (examples, etc).

@TrySound TrySound closed this as completed Feb 4, 2019
@Mouvedia
Copy link
Contributor

@TrySound why did you close this issue? Where is it documented?

@TrySound
Copy link
Contributor

AFAIK mixins proposal was declined

@Mouvedia
Copy link
Contributor

Mouvedia commented Aug 17, 2019

0b23575

Could you reopen?

@FezVrasta
Copy link
Contributor

FezVrasta commented Dec 4, 2020

Is there any way to use mixins with interfaces? Flowgen generates class C mixins I1, I2 but Flow doesn't like it

chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this issue Apr 1, 2022
Flow rightly complains that React$ComponentType isn't a class, so it
can't be extended (…er, or used with `mixins`, whatever that is:
facebook/flow#1383 ).

A value of type React$ComponentType *might* be a class, but it might
be just a plain function.
gnprice pushed a commit to chrisbobbe/zulip-mobile that referenced this issue Apr 5, 2022
Flow rightly complains that React$ComponentType isn't a class, so it
can't be extended (…er, or used with `mixins`, whatever that is:
facebook/flow#1383 ).

A value of type React$ComponentType *might* be a class, but it might
be just a plain function.
@SamChou19815
Copy link
Contributor

Close as mixin isn't planned to be well supported

@SamChou19815 SamChou19815 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 30, 2022
@trusktr
Copy link
Contributor

trusktr commented Dec 6, 2024

New syntax should be avoided. Instead, existing plain JavaScript mixins should be made to be super easy to define types for in Flow.

This is a plain JavaScript mixin, which is very easy to implement in plain JavaScript:

export function FooMixin(Base) {
  return class Foo extends Base {
    fooMethod() {...}
  } 
 }

You can use the mixin on any other class, even if it already extends another class, meaning you can change this,

import {SomeBaseClass} from 'somewhere'

class MyClass extends SomeBaseClass {
  myMethod() {...}
}

const o = new MyClass()
o.myMethod() // ok

to this:

import {SomeBaseClass} from 'somewhere'
import {FooMixin} from 'somewhere-else'

class MyClass extends FooMixin(SomeBaseClass) {
  myMethod() {...}
}

const o = new MyClass()
o.myMethod() // ok
o.fooMethod() // new

This is incredibly straight forward in plain JavaScript, and powerful for code re-use. However it is difficult in TypeScript, and much more difficult if not impossible in Flow.

@trusktr
Copy link
Contributor

trusktr commented Dec 6, 2024

New issue for mixins:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants