Anzeige
The Ring programming language version 1.9 book - Part 97 of 210
The Ring programming language version 1.9 book - Part 97 of 210
The Ring programming language version 1.9 book - Part 97 of 210
The Ring programming language version 1.9 book - Part 97 of 210
Anzeige
The Ring programming language version 1.9 book - Part 97 of 210
The Ring programming language version 1.9 book - Part 97 of 210
The Ring programming language version 1.9 book - Part 97 of 210
The Ring programming language version 1.9 book - Part 97 of 210
The Ring programming language version 1.9 book - Part 97 of 210
Anzeige
The Ring programming language version 1.9 book - Part 97 of 210
Nächste SlideShare
The Ring programming language version 1.5.1 book - Part 173 of 180 The Ring programming language version 1.5.1 book - Part 173 of 180
Wird geladen in ... 3
1 von 10
Anzeige

Más contenido relacionado

Similar a The Ring programming language version 1.9 book - Part 97 of 210(20)

Anzeige

Más de Mahmoud Samir Fayed(20)

Anzeige

The Ring programming language version 1.9 book - Part 97 of 210

  1. CHAPTER EIGHTYFOUR FREQUENTLY ASKED QUESTIONS (FAQ) 84.1 Why do we need Yet Another Programming Language (YAPL)? The language comes with better support for natural language programming and declarative programming. The inno- vation comes in supporting these paradigms with new practical techniques on the top of object-oriented programming and functional programming. Ring provides the programmers with the tools required to build a natural language like Supernova or a declarative language like REBOL and QML without the need to know anything about (compilers and parsing). You get the language constructs ready for use to create domain-specific languages in a fraction of time. Take a look at the Supernova programming language, in this language you can type: (I want window and the window title is hello world.) and it will create a GUI window with “Hello, World!” as the window title. When I created Supernova language in 2010, i discovered that using the natural code can be (similar to English and without limits and we can use the power of human language in programming) but to implement that you need a new language that has: 1. General Purpose 2. Practical 3. Can create natural languages very quickly. So we can get a system that can increase ease of use and productivity to the maximum level. So I created Ring because it was the best way to achieve this goal. Supernova was just a test of the idea, it helped getting a better view of the advantages and the disadvantages of the idea. And After testing the new ideas you are provided with something practical. So now we have Ring after Supernova. A story that is maybe similar to having Python after ABC.Where Python avoids the problems of ABC, but keeps the advantages of ABC. Also, Ring learns from Ruby and ROR’s story. The language power could appear in frameworks better than the direct usage as a general purpose language. Also Ring comes with a clear goal/motivation; (Creating a new version of the PWCT Software) something that was learned from the design the C language in a certain way to create the Unix Operating System. In other words, you have a goal that directs you in each design decision. You will understand the value of our decisions once you start trying to solve the problem that we will use Ring to solve. The questions is: could you enable any one in the world without knowledge about computer programming concepts to create very powerful software? Scientifically the answer is (visual Programming) and (natural Programming). In practice we are still away from switching to these paradigms without introducing other problems. Ring is designed to solve this problem. It is designed to provide natural programming in a practical way. And to create a powerful visual programming tool. Ring is designed to be a new world of programming after 10 years of research in visual programming and natural languages. The Ring Programming Language (Compiler+VM) is developed 100% using visual programming without writing a single line of code. I used my tool (Programming Without Coding Technology) to design everything and get the C code generated for me. Advantages ? 1. Faster 929
  2. Ring Documentation, Release 1.9 2. No Syntax Errors 3. Easy to understand and manage the code because the abstraction level is higher 4. No critical disadvantages because you can control everything as writing your code. Using my experience in using visual programming for 10 years and natural programming for 5 years, I designed Ring to move the knowledge to mainstream programmers by providing a practical language that supports these ideas. I agree that each programmer/developer has the freedom to form his opinions about any software including program- ming languages. Ring is not an exception but you may miss the idea behind the language. It is innovative and may help you to think differently about how to solve your problems. Maybe this is not clear to many programmers because It is a practical language and includes many features known to programmers and when a programmer looks at the language they maight think that nothing new because it’s familiar. I created Ring to solve problems in a different way. Where I will start programming just by describing the software using new natural interfaces that I will implement later when I move from the design stage to the implementation stage. (I don’t determine the time to switch between stages, You are free to use Agile methods). Since Ring is a new language you have 3 options: 1. To not care at all for now. 2. Think of the future of the language and help us if you understand the idea and want to contribute. 3. Wait and come back again in the future to use it. Summary: • Ring is designed based on a need to develop a new version of the PWCT software. Once we finish PWCT 2.0 we will have good and large software developed using Ring. • We will push declarative and natural paradigms many steps forward. Also in next versions we have a plan to present a new paradigm for network programming and concurrency. We tested this new paradigm through simple prototypes during the last years and we will integrate it with Ring in future releases. 84.2 Why is Ring weakly typed? Because it’s faster and more natural, and this is important for the language’s goals. One of the rules is: the data type at the beginning affects the final result. For example, when you type “Print : ” + 5 , The String comes first, so 5 will be converted to a String. While when you type 5 + “10” The number comes first so “10” will be converted to 10. This helps a lot to quickly convert between numbers and strings using the same operator. If you want to prevent conversion (Write code that prevent conversion) In these cases you will notice that what you are writing is less code (And can be removed). Weakly typed = automatic conversion and automatic is good thing and is better than manual if you know how to use it correctly. 84.3 What are the advantages to using Ring over Lisp or Smalltalk? Smalltalk and Lisp are GREAT languages. I like many of the concepts behind them but I’m sure that selecting the right programming language is based on the problem and comes after the problem’s definition. I have a problem that I want to solve and these GREAT languages are not ideal for this problem so I designed Ring. When you design a new language, You can learn from the past but you must look forward and live in the future. What you know about natural programming maybe based on the old knowledge about the power of these paradigms in the practical world and I agree with you but I see other techniques that can be applied to get this to work in practice. What you miss about natural language is that they are context sensitive and this means we can use it and think differently about how we can express our ideas. 84.2. Why is Ring weakly typed? 930
  3. Ring Documentation, Release 1.9 Example : I want window contains 3 buttons. In one sentence I created 4 objects (The window and the three buttons) and added the buttons to the window. The idea of natural programming is to get many things done like that. 84.4 Why is Ring largely focussed on UI creation? Yes UI creation is one of the important things in the language features because it is designed to create a visual pro- gramming tool, But the language is a multi-paradigm language where we can select the programming paradigm based on the problem. 84.5 Is Ring some sort of an improvement of PHP? Ring is not designed to replace PHP, Lua or Smalltalk. Ring’s support for declarative programming and natural language programming is very innovative and much better than staying with procedural, object-oriented and functional languages. Ring see the future in programming without code (using natural languages) and is designed to support that. 84.6 What are the advantages of using Ring over native C or C++? Ring provides a better way to mix between different programming paradigms in solving problems. The different programming paradigms play well together in the same language. 1. It’s easy to switch from one programming paradigm to another one because the language constructs use similar syntax for similar concepts. 2. The paradigms are provided to interact and used together in different layers in the software. for example you can create a game engine using object-oriented programming but write the game code using declara- tive programming or natural programming and behind the scenes your declarative or natural code will use the object- oriented classes. 3. Ring is more productive and natural than C/C++. 4. Ring is a dynamic language. We can generate and execute code during the runtime. Ring is dynamically typed and weakly typed for flexibility. 5. The Garbage collector is generational (escape analysis) and also uses reference counting. it’s very fast and still provides control to the programmer who can delete memory at any time. 6. Ring’s compiler and virtual machine are just 20,000 lines of ANSI C code that can be compiled and used in any platform. 7. You can use C/C++ libraries and Ring comes with code generator to create wrappers from C functions or C++ classes. so when you need more performance or when you need to use more libraries you can easily do that. 84.7 What is the difference between Ring and Python? And is Ring Open Source? Yes the language is Open Source (MIT license) 84.4. Why is Ring largely focussed on UI creation? 931
  4. Ring Documentation, Release 1.9 In general I like Python and Ruby but I was looking for a language more suitable for creating the next version of the Programming Without Coding Technology (PWCT) software so I started the Ring design. Some simple changes that matters for my goal are 1. Not case sensitive 2. The list index start from 1 3. You can call functions before definition 4. Don’t use Python syntax like (indentation, using self, :, pass & _) 5. Weakly typed (convert automatically between types based on context) 6. The programs follow simple and constant structure (Statements then functions then packages and classes) 7. Using the ‘=’ operator for assignment and for testing values Critical changes are 1. Small Language : The Ring compiler + Virtual Machine = 15K lines of C code , the other 500K lines are related to libraries and are optional when we go for using the language in C/C++ programs. 2. The Garbage collector : Uses Escape Analysis/Reference counting and give the programmer the ability to determine when to delete memory using the assignment operator 3. Compact Syntax : Ring is not line sensitive, you don’t need to write ; or press ENTER to separate between statements 4. Using { } to access the object then using the object attributes and methods directly 5. Natural Programming : It’s very easy to create natural interfaces using Ring based on OOP 6. Declarative Programming using Nested Structure The Ring programming language is designed based on my experience from using many other languages like C, C++, C#, Lua, PHP, Python, Ruby, Harbour, Basic and Supernova And the language comes with innovative features added to achieve the language goal • Applications programming language. • Productivity and developing high quality solutions that can scale. • Small and fast language that can be embedded in C/C++ projects. • Simple language that can be used in education and introducing Compiler/VM concepts. • General-Purpose language that can be used for creating domain-specific libraries, frameworks and tools. • Practical language designed for creating the next version of the Programming Without Coding Technology software. 84.8 What are the advantages to using Ring over Perl, PHP, Python or Ruby? 1. Ring is New and Innovative. The language will let you think different about programming. 2. Ring is Smaller. (Lessons learned from the Lua language) 3. Ring is Simple. (Lessons learned from the BASIC and Clipper/Harbour languages) 4. Ring is more Natural. (Lessons learned from the Supernova language) 5. Ring is more Declarative. (Lessons learned from REBOL and QML languages) 84.8. What are the advantages to using Ring over Perl, PHP, Python or Ruby? 932
  5. Ring Documentation, Release 1.9 6. Ring Implementation is Transparent, Visual and comes with Rich Features. 84.9 What are the advantages to using Ring over Tcl or Lua? 1. Clean Code (More Natural) 2. More Features (A lot of useful programming paradigms) 84.10 What are the advantages to using Ring over C# or Java? 1. Compact Code (Clean and Natural), More Productivity and Flexibility. 2. Better support for Declarative Programming and Natural Programming 84.11 The documentation says functional programming is sup- ported, but then this happens? The question was about this code f = func { a = 42 return func { return a } } innerF = call f() call innerF() Output: Using uninitialized variable : a In function _ring_anonymous_func_16601() The Answer: • It’s Anonymous Functions, i.e. Not Closures. • Many developers asked about supporting Closures and during language development we may add new features that doesn’t go against the language goals or spirit. • You can use classes and objects when you want to merge between the state and functions to provide a clear solution. • You can use Lists and put the anonymous function inside the List then return the list that contains the state and the function. Pass the list to the function when you use it. • You can use eval() and substr() to add the variable value directly to the anonymous function before return. • We protect you from other scopes when you define the function. In Ring we provided the Three Scopes Rule where at each point you have only at maximum three scopes (Global, Object Scope and Local Scope). • We don’t get everything from everywhere to be like others! We don’t need to do that. If we will think like that then we will create a very complex language or we will save our time and use other languages. • When you think about learning or studying a new language concentrate about (What is new?) and (What is better in this language?) to know when to use it. Don’t compare a new language just released little months ago with languages started many years ago and expect to find everything that you used to have. 84.9. What are the advantages to using Ring over Tcl or Lua? 933
  6. Ring Documentation, Release 1.9 • Each programming language miss features in other languages. The idea is not the Features. it’s the spirit and ability behind all of the features together. 84.12 Why the ability to define your own languages Instead of just handing over the syntax so you can parse it using whatever code you like? It’s innovation - You create natural statements without the need to learn about parsing. You just use Classes which is intelligent decision (where later we can mix between classes to support more statements based on the context - We can change and translate the defined statements and many more!). Also the statements are added in Ring World where you can use any Ring statement. 84.13 Why you can specify the number of loops you want to break out of? The language supports programming in the small and programming in the large. The selection of what features to use is based on what are you going to do. Any programmer can write poorly code in any language if he/she wants to do that. The idea is what must be done in the language design to prevent errors without causing other problems like killing flexibility. Read some source code in the Linux Kernel and Ruby Implementation for example, You will find good usage for GOTO as a practical example that General Rules are not for All Use Cases and great programmers know when to break the rules. I’m not saying go and use GOTO or saying Ring add things like that. But the ability to break more than one loop and/or the ability to break the loop from sub functions is practical for small programs. Anyway these are some of the small new things added by the language (Not the big idea). 84.14 Why Ring uses ‘See’, ‘Give’, ‘But’ and ‘Ok’ Keywords? See and Give are selected not to be “opposite actions” but to reflect what I want to do as a programmer. When I want to see something on the screen I use ‘See’. When I want to give some input to the program I use ‘Give’. My selection of “but” and “ok” is based on selecting keywords that can be written quickly. Also using “but” is easy to remember than elseif/elif/elsif where each language select a different keyword. In Ring 1.1 and later versions All of this is just an option. You can use ‘Put’ and ‘Get’ instead of ‘See’ and ‘Give’ You can use ‘elseif’ and ‘end’ insetad of ‘But’ and ‘Ok’ It’s your choice. In Ring we have syntax flexibility where we provide more than one style. Also you can change the language keywords and operators. Also you can define new natural languages too. 84.12. Why the ability to define your own languages Instead of just handing over the syntax so you can parse it using whatever code you like? 934
  7. Ring Documentation, Release 1.9 84.15 What is the philosophy behind data types in Ring? The Ring programming language is designed to be SMALL. The language provides the basic constructs that you need to do anything! One of the goals is to keep the basic constructs simple and small as possible. Using Lists in Ring you can • Create Arrays (one data type) • Create Lists (Mix of data types) • Create Tree (Nested arrays) • Use String Index (Looks like Dictionary/Hash Table) The same principle is applied to Numbers • You can use the number for int value • You can use the number for double value • You can use the number for Boolean value (True/False) The sample principle is applied for Strings • You can use the string for storing one character • You can use the string for storing text (one or many lines) • You can use the string for storing binary data • You can use the string for storing date • You can use the string for storing time • You can use the string for storing NULL values (empty strings) And we have Object Oriented Support + Operator Overloading where the programmer can define new data types and use them as default types defined by the language So We have • A small and simple language that someone can pick in little days • A fast language that provide primitive types (String - Number - List - Object) • A flexible language that can be extended using OOP to add new types according to the application domain 84.16 What about the Boolean values in Ring? You can use true for 1 and false for 0 when you test the result of Boolean expressions in your code. Just when you print the value using the see command you will see 1 for (true) and 0 for (false) Why ? Because Ring contains only 4 types of variables 1. Number 2. String 3. List 84.15. What is the philosophy behind data types in Ring? 935
  8. Ring Documentation, Release 1.9 4. Object The first type (Number) is used to represent int, double and Boolean values. The second type (String) is used to represent char, array of characters, date and time. The third type (List) is used to represent Arrays of one type, Arrays of more than one type, Hash (Dictionary), Tree, etc. The object can be an object created from a Ring class (Any Class) or just a C Pointer that we get from calling a C/C++ function/method. Why ? The Ring is designed to give the programmer/developer the most simple constructs that can be used to do everything. The programmer/developer can customize the language by creating new classes (and use operator overloading) to get more types that he care about according to the problem domain. Why ? Because simple is better, and easy to learn and remember! And this provide flexibility to convert between high level types that can be represented using the same basic type 84.17 What is the goal of including the “Main” function in Ring? The main function is very important, you need it when you want to write statements that uses local variables instead of the Global scope. Example: x = 10 myfunc() See "X value = " + X # here I expect that x will be (10) # but I will get another value (6) because myfunc() uses x ! Func myfunc for x = 1 to 5 See x + nl next Output: 1 2 3 4 5 X value = 6 Now using the Main function Func Main x = 10 myfunc() See "X value = " + X Func myfunc for x = 1 to 5 See x + nl next 84.17. What is the goal of including the “Main” function in Ring? 936
  9. Ring Documentation, Release 1.9 Output 1 2 3 4 5 X value = 10 84.18 Why the list index start from 1 in Ring? It’s about how we count in the real world, when we have three apples in our hand we say 1 2 3 We don’t start from 0 The question must be why the other languages start from 0 ? The answer is, because this is related to the machine and how we deal with values and memory address. Example we have array called myarray[5] In memory : myarray will have an address The first item will be stored in that address The second item will come after that address and so on Now when we need to point to the first item we need the address of myarray So we type myarray[0] because myarray + 0 result will still point to the first item for the second item myarray[1] because myarray + 1 result will point to the second item and so on In Low Level languages or languages near to the machine it’s good to be like this But for high level language designed for applications it’s better to be natural Example mylist = [1,2,3,4,5] for x = 1 to len(mylist) see x + nl next In the previous example we start from 1 to the length of the array if the index starts from 0 we will write for x = 0 to len(mylist)-1 or remember the for loop in other languages for(x=0 ; x<nMax ; x++ ) You will use the < operator ! 84.19 Why Ring is not case-sensitive? 1. To be more human-friendly 84.18. Why the list index start from 1 in Ring? 937
  10. Ring Documentation, Release 1.9 2. Like Ada, SQL, Pascal, Delphi, Visual Basic, Visual FoxPro, etc. 3. To help in supporting Natural Language Programming. 4. To be able to select your favorite style when writing the language keywords see "lower case!" SEE "UPPER case!" See "First Letter is UPPER case!" 5. To avoid getting error message when writing quick tests then type “variable” instead of “Variable”. 6. To avoid getting error message when you type “Dosomething()” instead of “doSomething()” 7. In Ring, No conflict between Variables, Method Names & Classes Names We can write person as variable name and Person as class name. person = new Person class Person name address phone 84.20 Why the Assignment operator uses Deep Copy? “Because it’s a poor tradeoff to add complexity for dubious performance gains, a good approach to deep vs. shallow copies is to prefer deep copies until proven otherwise.” , Steve McConnell, Code Complete 1. It’s more natural, When you use the assignment operator, You expect a deep copy. 2. If you don’t need a deep copy, Just don’t use it! 3. The Ring language is designed to reduce references usage as much as possible. 4. The Ring language is designed to make using references simple and possible in special cases where this make sense. 5. We have references when this is natural, like passing lists and objects to functions, creating objects (Like GUI Objects) from a C/C++ library, returning an object stored inside a list. 6. It is a feature, We can use it to create pure functions. The Value() function in the stdlib uses this feature to pass lists & objects by value when we need this. 7. When we need references, It’s recommended to create a class that manage sharing lists and objects. 8. It’s more safe at the application level to avoid many logical errors. 9. In Ring, we start without thinking about the little details and concentrate on the application, You don’t have to write the type (Dynamic Typing), You don’t have to write explicit conversions between numbers and strings (Weakly Typed) and you don’t have to select between using values or references, You don’t have to write the scope (Lexical Scoping). 10. In Ring, we have smart garbage collector (Simple & Fast), We can delete the memory directly at any time using the Assignment operator too. Reducing references usage or using them through managers helps a lot to achieve this goal. by doing this we have full control. 11. If you want to create references and avoid creating a manager, You can use Object2Pointer() and Pointer2Object() functions But It’s not the Ring way “Spirit” to do things. 84.20. Why the Assignment operator uses Deep Copy? 938
Anzeige