Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Assume that I have a subfunction seen in the below. What is the difference between these two
function a=b(x,y)
.
.
.
a=output
and
function b(x,y)
......
if I write it in second form how can I define it main function and how can I see its outputs.
Another question,
I found a code from here (http://www.mathworks.com/matlabcentral/fileexchange/21443-multiple-rapidly-exploring-random-tree--rrt-) including a function like:
%% SetObstacleFilename
function SetObstacleFilename(self,value)
if isa(value,'char')
self.obstacleFilename = value;
self.GenerateObstacles();
end
end
how can I use it in my main function? Moreover what is self.GenerateObstacles() command? There is no equality on it?
I think I see how both of your questions are related to the same thing. You really should've asked something along the lines of:
I always saw MATLAB functions written in the form function a=b(x,y), however recently I came across some code which included functions in the form function b(x,y) (e.g. function SetObstacleFilename(self,value)).... so what's up with that?
In order to understand the 2nd type of functions, you need to consider object-oriented programming (OOP).
The code example you found is taken from within a MATLAB class. Class-related functions are known in OOP as "methods", and this specific code in another programming language would take the shape of a void return type function\method.
Now consider the term object that refers to an instance of a class.
Traditionally, methods are limited to a single output. For this reason, some methods are designed to operate on objects (actually pointers, AKA "passing by reference") such that returning a value is not necessary at all, because the input objects are directly manipulated. Other cases when methods don't need to return anything may include functions that have some "utility" functionality (e.g. initialize something, plot something, output something to the console etc. - just like the self.GenerateObstacles() method you mentioned).
Regarding your other questions:
The self in SetObstacleFilename(self,value) looks like an instance of the considered class.
Usually to use class methods you need to instantiate an object using a constructor (which should be a function with the same name of the class), unless these methods are static.
To conclude - above are just the most fundamental basics of OOP. I won't attempt to teach you the whole OOP Torah while standing on one leg, so I am providing some additional materials below, should you be interested to further your understanding of the topic.
Hopefully, what's going on is a bit clearer now!
Here are some resources for you:
MATLAB's OOP Manual.
MATLAB's documentation on OOP.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
While learning Scala, I found the concept of implicit difficult to rationalize. It allows one to pass values implicitly, without explicitly mentioning them.
What is its purpose for being and what is the problem that it seeks to solve?
At it's heart, implicit is a way of extending the behavior of values of a type in a way that's fully controllable on a local level in your program, and external to the original code that defines those values. It's one approach to solving the expression problem.
It lets you keep your core classes focused on their most fundamental structure and behavior, and factor out higher-level behaviors. It's used to achieve ad hoc polymorphism, where two formally unrelated data types can be seamlessly adapted to the same interface, so that they can be treated as instances of the same type.
For example, rather than your data model classes containing JSON serialization behavior, you can store that behavior elsewhere, and implicitly augment an object with the ability to serialize itself. This amounts to defining in an implicit instance, which specifies how your object can be viewed as "JSON serializable", rather than its original type, and it's done without editing real type of the object.
There are several forms of implicit, which are pretty thoroughly covered elsewhere. Use cases include enhance-my-library pattern, the typeclass pattern, implicit conversions, and dependency injection.
What's really interesting to me, in the context of this question, is how this differs from approaches in other languages.
Enhance-my-library and typeclasses
In many other languages, you accomplish this by monkey patching (typically where there is no type checking) or extension methods. These approaches have the downside of composing unpredictably and applying globally. In statically typed languages without a way of opening classes, you usually have to make explicit adapters. This has the downside of a lot of boilerplate. In both static and dynamic languages, you may also be able to use reflection, but usually with a lot of ceremony and complexity.
In Haskell, typeclasses exist as a first-class concept. They're global, though, so you don't get the local control over what typeclass is applied in a given situation. In Scala, you control what implicits are in scope locally, through the modules you import. And you can always opt out of implicit resolution entirely by passing parameters explicitly.
People advocate for global versus local resolution of typeclasses one way or the other, depending on who you ask.
Implicit conversions
A lot of other languages have no way to accomplish this. But it's become pretty frowned upon in Scala, so maybe this is for good reason.
There's a paper about type classes with older slides and discussion.
Being able implicitly to pass an object that encodes a type class simplifies the boilerplate.
Odersky just responded to a critique of implicits that
Scala would not be Scala if it did not have implicit parameters and
classes.
That suggests they solve a challenge that is central to the design of the language. In other words, supporting type classes is not an ancillary concern.
Its a deep question really It is something that is very powerful and you can use them to write abstract code eg typeclasses etc i can recommend some tutorials that you may look into and then we can haved a chat maybe sometime :)
It is all about providing sensible defaults in your code.
Also the magic of invoking apparently non existent methods on objects which just seems to work! All that good stuff is done via implicits.
But for all its power it may cause people to write some really bad code as well.
Please do watch Nick Partridge's presentation here and i am sure if you code along with him you will understand why and how to approach implicits.
Watch it here
Dick Walls excellent presentation with live coding
Watch both parts.
In C++ (at least as of a decade ago), there was speed advantage in defining the body of a class method in the header file, where the class is defined. No function call overhead was suffered because, in the compilation process, the invocation of such functions was replaced by the code in the body of the function. Subsquently, all source level optimizations (and all optimizations beyond source level) could be brought to bear.
Is there an analogous advantage to putting the body of class methods in the classdef file itself rather than in a separate m-file? I'm speaking specifically about the case where one defines a #myclass/myclass.m, with method m-files in the directory #myclass. The two options I'm considering is to have the code for the body of a method mymethod put into the classdef in #myclass/myclass.m versus being in a separate file #myclass/mymethod.m.
However, an very related auxiliary question would be how those two options compare with having everything defined in a myclass.m file, with no folder #myclass.
Please note that I have previously posted this to usenet
Summarizing the comments as the answer to this question: Using an #classFolder folder containing separate method m-files is faster than having a single m file containing the entireties of the function definitions in the classdef. This is the case even though OOP in general has sped up in 2015b.
I find this a happy answer because I see great value in separating the code implementation of a class's methods from the class definition itself. That's the whole idea of separating interface from implementation. I can look at the classdef and see only a map of the class rather than have those key information elements completely dispersed by the deluge of code that accompanies implementation.
It's just too bad that this doesn't work so well for weakly typed languages. What's listed in the classdef is just member names (properties or methods) with no specification of what class they are. So not as much information as in a strongly typed language. In fact, very little info about what the class, its properties, and its methods really are. Furthermore, there is nothing to ensure that the actual method implementation even complies with the argument list in the classdef. These kind of details helped prevent development errors in a strongly typed language, especially when one's body of classes get large.
I'm looking at a rather large and poorly written Matlab program. One of the things that makes understanding the code tricky is the variables don't show their type. In searching I only found explanations how to do this while debugging code(the whos and class commands). I'm looking for a way to view type information in the editor itself.
For example in the following code I would like to know the type of A and B:
classdef Data
properties
B;
function obj = Data(A)
obj.B = A.B;
end
Or is the type not determined until the function is called, and A could be any class with a B parameter?
As I mentioned in the comments, unfortunately there isn't any way I know of to do this in the IDE without entering the debugger because MATLAB is not statically typed. You can also trace through the function and see what is calling the methods/functions/etc. in question and the variables used.
Your ending sentence is correct. Looking at your example solely in the eyes of the IDE A could be any data type, even one where the dot notation isn't valid (and thus would throw an error). It's up to the user to add input validation for functions that are not built in.
Usually numeric variables are defined as doubles, you can ask if a variable belongs or not to a specific data type, here are some ways to do it.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm pretty much a noob with Unity. As a c++ programmer, the naming conventions in Unity bothers me a little. And having OCD ontop of that makes me go crazy ;)
The objects has say a property Transform which again has a property Position.
But these properties must be accessed by writing transform.position in the code using lower case. This is not very intuitive to me. So I wonder how I can look at it in order to more easily avoid complications. And what conventions I should use to be able to tell everything appart by taking a quick look at the variables.
The Unity convention is actually rather simple: everything is Pascal-cased, types (classes, structs, enums) and methods start with upper-case, fields and properties with lower-case. Enum values in upper-case, constants in lower-case (usually).
So ClassName, MethodName, myField, myProperty { get; set; }, MyEnum.CaseA... that's it.
As for your example, Transform is a class, whereas transform is an accessor to the instance of Transform in that particular GameObject/Component.
Also, Transform doesn't have a Position property, it has a position property (always lower-case).
This is more or less based on C#'s conventions and the standard .NET library (MS has very precise guidelines about it), except standard .NET uses UpperCase for public/protected methods AND properties, and lower-case for private (again, usually; what's private is more left to the taste of the coder I think).
As a side-note, with any codebase, in any language, the best way is ALWAYS to follow the existing convention. Every seasoned programmer will tell you this. I understand about OCD, believe me, but in this case I suggest you let it go.
There are very little objective arguments as to why a convention would be better than another (by definition a convention is arbitrary), and even if there was, the absolute worse thing you can do is mix several conventions, because then you have 0 convention at all and never know what to expect.
At least C# tries to standardize; I've worked on several C++ codebases and I fail to see a common denominator: UpperCaseClassNames, lowerCaseClassNames, underscore_separated, tClassName, ENUMS_IN_UPPER, or not... it's rarely consistent, so the less you mix the better.
This question already has answers here:
Prefer composition over inheritance?
(35 answers)
Closed 9 years ago.
I am making a program for studying chess openings, traps and maybe other related things. It has a class MoveSequence, which basically is an ordered list of objects from a class ChessPosition. I also have a class ChessOpening which has a sequence of moves and a name, an ECO-code (chess opening classification system) and probably some more stuff.
Should I implement ChessOpening as a subclass of MoveSequence, or should it just contain a MoveSequence object? The same question would apply for a class ChessTrap.
Don't think it matter so much what I choose in this simple problem. But I want to learn this stuff, so I was wondering if there is some principles, or rules of thumb, one should consider when making decision like this.
My 0.02$ worth:
If a possible subclass does not share basically all of the properties and methods of its superclass, I start to wonder.
Overriding methods to change behavior, or adding new properties and methods is normal. But when you hit that awkwardness when some of the superclass properties/methods are not applicable to the subclass, it gets creepy. (What do you do then, just ignore the invalid/inappropriate parts? Hope no one ever calls them? Stub them out and raise an error if they are used?)
If xxx is not really a yyy, maybe xxx and yyy are both zzz's. (Or if they just share common behaviors, maybe create a module to include, which is a standard Ruby/Rails practice.)
A real Computer Scientist may have a more concise and definitive answer...