Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to distribute the code of a class to several files?

Is it possible to distribute the code of a class to several files?

like image 630
sid_com Avatar asked May 17 '21 05:05

sid_com


People also ask

Can you have multiple classes in a Python file?

In Python there is rule of one module=one file. In Python if you restrict yourself to one class per file (which in Python is not prohibited) you may end up with large number of small files – not easy to keep track. So depending on the scenario and convenience one can have one or more classes per file in Python.

How do you split a class in Java?

Use aggregation: Move parts of your class to a separate class and establish a relationship to the second class using a reference.

Should all classes be in the same file?

Thus, if you have a few very small classes that are highly interconnected, they should be bundled into the same file. You should split out a class if it's either not tightly connected to another class, or if its too complex to be included with another class. That said, the one-class-per-file rule is usually a good heuristic.

When to split a class into multiple files?

You should split out a class if it's either not tightly connected to another class, or if its too complex to be included with another class. That said, the one-class-per-file rule is usually a good heuristic.

Can We import a class from another class in Java?

As the classes are declared public by default in each .class file, you can import the class successfully. Some variables and methods may be available after subclassing and some are available directly (public variables and methods). 24) What are the uses of a Java Package?

Can you extend the abstract classes imported from a package?

C) You can extend the abstract classes imported from the package. 23) Accessing the variables, constants, or methods of a class, imported from a package is subjective to access modifiers like PUBLIC, PRIVATE, PROTECTED and default. State TRUE or FALSE.


Video Answer


2 Answers

Honestly, I think the best route is to break it up into different roles that you compose into your class.

After all, how are you planning on breaking up your class?
Are you going to group methods and attributes according to similarity?

At that point you've just about come up with a role, so you might as well make it a role.

If you look at the source for Rakudo you see things like this:

class Perl6::Metamodel::ClassHOW
    does Perl6::Metamodel::Naming
    does Perl6::Metamodel::Documenting
    does Perl6::Metamodel::LanguageRevision
    does Perl6::Metamodel::Stashing
    does Perl6::Metamodel::AttributeContainer
    does Perl6::Metamodel::MethodContainer
    does Perl6::Metamodel::PrivateMethodContainer
    does Perl6::Metamodel::MultiMethodContainer
    does Perl6::Metamodel::MetaMethodContainer
    does Perl6::Metamodel::RoleContainer
    does Perl6::Metamodel::MultipleInheritance
    does Perl6::Metamodel::DefaultParent
    does Perl6::Metamodel::C3MRO
    does Perl6::Metamodel::MROBasedMethodDispatch
    does Perl6::Metamodel::MROBasedTypeChecking
    does Perl6::Metamodel::Trusting
    does Perl6::Metamodel::BUILDPLAN
    does Perl6::Metamodel::Mixins
    does Perl6::Metamodel::ArrayType
    does Perl6::Metamodel::BoolificationProtocol
    does Perl6::Metamodel::REPRComposeProtocol
    does Perl6::Metamodel::InvocationProtocol
    does Perl6::Metamodel::ContainerSpecProtocol
    does Perl6::Metamodel::Finalization
    does Perl6::Metamodel::Concretization
    does Perl6::Metamodel::ConcretizationCache
{
    … # only 300 lines of code
}

If you do a good job of splitting up your roles, you should be able to use them in many classes.
How many classes in the Rakudo code base do you think compose in the Perl6::Metamodel::Naming role?

That role only provides a few things, and is only 45 lines long.

Here is an abbreviated version. (All of the code in the methods has been deleted here for brevity.)

role Perl6::Metamodel::Naming {
    has $!name;
    has $!shortname;

    method name($obj) {
        …
    }
    method set_name($obj, $name) {
        …
    }
    method shortname($obj) {
        …
    }
    method set_shortname($obj, $shortname) {
        …
    }
}
like image 156
Brad Gilbert Avatar answered Oct 17 '22 02:10

Brad Gilbert


Yes, there always is. But there us no standard supported way (yet anyway).

  1. You can take the approach that Raku takes itself when creating the core settings: concatenate the files into a single file, and compile that. When you're building Rakudo from scratch, that's what's happening when you see the line:

    +++ Generating gen/moar/foo
    

    The generated files can be inspected in the gen/moar directory. At one point, I brought this up in a problem solving issue, but that never went anywhere. Perhaps that should be revisited.

  2. You can use augment class. But that currently only makes sense inside a single file, because with multiple files it is creating multiple versions of the same "module" in pre-compilation. And the system is not able to determine what to resolve to what. This is when you realize that pre-compilation of Raku modules currently is creating libraries that need to be statically linkable at runtime.

like image 24
Elizabeth Mattijsen Avatar answered Oct 17 '22 02:10

Elizabeth Mattijsen