[38aaacc2] | 1 | --
|
---|
| 2 | -- Copyright (c) 2010 Jiri Svoboda
|
---|
| 3 | -- All rights reserved.
|
---|
| 4 | --
|
---|
| 5 | -- Redistribution and use in source and binary forms, with or without
|
---|
| 6 | -- modification, are permitted provided that the following conditions
|
---|
| 7 | -- are met:
|
---|
| 8 | --
|
---|
| 9 | -- o Redistributions of source code must retain the above copyright
|
---|
| 10 | -- notice, this list of conditions and the following disclaimer.
|
---|
| 11 | -- o Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | -- notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | -- documentation and/or other materials provided with the distribution.
|
---|
| 14 | -- o The name of the author may not be used to endorse or promote products
|
---|
| 15 | -- derived from this software without specific prior written permission.
|
---|
| 16 | --
|
---|
| 17 | -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | -- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | -- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | -- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | -- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | -- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | -- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | --
|
---|
| 28 |
|
---|
| 29 | -- Demonstrate generic classes.
|
---|
| 30 |
|
---|
| 31 | class GenericsDemo is
|
---|
[c5cb943d] | 32 | fun Main(), static is
|
---|
[051b3db8] | 33 | Console.WriteLine("Let's try some generics.");
|
---|
[38aaacc2] | 34 |
|
---|
| 35 | var f : B/int/string;
|
---|
| 36 | var g : A/string/int;
|
---|
| 37 | var s : string;
|
---|
| 38 |
|
---|
| 39 | f = new B/int/string();
|
---|
| 40 | g = new A/string/int();
|
---|
| 41 |
|
---|
| 42 | -- This should be okay.
|
---|
| 43 | f = f;
|
---|
| 44 | g = f;
|
---|
| 45 |
|
---|
| 46 | -- Method
|
---|
| 47 | g.set_a("Lorem");
|
---|
| 48 | f.set_a("ipsum");
|
---|
| 49 | s = g.get_a();
|
---|
| 50 | s = f.get_a();
|
---|
| 51 |
|
---|
| 52 | -- Named property
|
---|
| 53 | g.P = "Lorem";
|
---|
| 54 | f.P = "ipsum";
|
---|
| 55 | s = g.P;
|
---|
| 56 | s = f.P;
|
---|
| 57 |
|
---|
| 58 | -- Indexed property
|
---|
| 59 | g[1] = "Lorem";
|
---|
| 60 | f[1] = "ipsum";
|
---|
| 61 | s = g[1];
|
---|
| 62 | s = f[1];
|
---|
| 63 | end
|
---|
| 64 | end
|
---|
| 65 |
|
---|
| 66 | -- Generic class with two type parameters
|
---|
| 67 | class A/u/v is
|
---|
| 68 | -- Variable whose type is one of the parameters
|
---|
| 69 | var a : u;
|
---|
| 70 |
|
---|
| 71 | -- Member function whose argument type is one of the parameters
|
---|
| 72 | fun set_a(arg : u) is
|
---|
| 73 | a = arg;
|
---|
| 74 | end
|
---|
| 75 |
|
---|
| 76 | fun get_a() : u is
|
---|
| 77 | return a;
|
---|
| 78 | end
|
---|
| 79 |
|
---|
| 80 | -- Property whose type is one of the type parameters
|
---|
| 81 | prop P : u is
|
---|
| 82 | get is
|
---|
| 83 | return a;
|
---|
| 84 | end
|
---|
| 85 | set value is
|
---|
| 86 | a = value;
|
---|
| 87 | end
|
---|
| 88 | end
|
---|
| 89 |
|
---|
| 90 | -- Indexed property whose type is one of the type arguments
|
---|
| 91 | prop self[i : int] : u is
|
---|
| 92 | get is
|
---|
| 93 | return a;
|
---|
| 94 | end
|
---|
| 95 | set value is
|
---|
| 96 | a = value;
|
---|
| 97 | end
|
---|
| 98 | end
|
---|
| 99 | end
|
---|
| 100 |
|
---|
| 101 | -- Generic class derived from another generic class
|
---|
| 102 | --
|
---|
| 103 | -- Note the swapping of type arguments.
|
---|
| 104 | --
|
---|
| 105 | class B/v/u : A/u/v is
|
---|
| 106 | end
|
---|