mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-26 22:03:35 -06:00
New Core Tutorial
git-svn-id: svn://ultimatepp.org/upp/trunk@10538 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
dd88feaf2e
commit
6c22e727de
37 changed files with 3125 additions and 0 deletions
77
tutorial/CoreTutorial/Value.cpp
Normal file
77
tutorial/CoreTutorial/Value.cpp
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#include "Tutorial.h"
|
||||
|
||||
void ValueTutorial()
|
||||
{
|
||||
/// .Value
|
||||
|
||||
/// Value is sort of equivalent of polymorphic data types from scripting languages like Python
|
||||
/// or JavaSript. `Value` can represent values of concrete types, some types also have
|
||||
/// extended interoperability with `Value` and it is then possible to e.g. compare `Value`s
|
||||
/// containing such types against each other or serialize them for persistent storage.
|
||||
|
||||
/// Usually, Value compatible types define typecast operator to `Value` and constructor
|
||||
/// from `Value`, so that interaction is for the most part seamless:
|
||||
|
||||
Value a = 1;
|
||||
Value b = 2.34;
|
||||
Value c = GetSysDate();
|
||||
Value d = "hello";
|
||||
|
||||
DUMP(a);
|
||||
DUMP(b);
|
||||
DUMP(c);
|
||||
DUMP(d);
|
||||
|
||||
int x = a;
|
||||
double y = b;
|
||||
Date z = c;
|
||||
String s = d;
|
||||
|
||||
DUMP(x);
|
||||
DUMP(y);
|
||||
DUMP(z);
|
||||
DUMP(s);
|
||||
|
||||
/// As for primitive types, Value seamlessly works with `int`, `int64`, `bool` and `double`.
|
||||
/// Casting `Value` to a type that it does not contain throws an exception:
|
||||
|
||||
try {
|
||||
s = a;
|
||||
DUMP(s); // we never get here....
|
||||
}
|
||||
catch(ValueTypeError) {
|
||||
LOG("Failed Value conversion");
|
||||
}
|
||||
|
||||
/// However, conversion between related types is possible (as long as it is supported by
|
||||
/// these types):
|
||||
|
||||
double i = a;
|
||||
int j = b;
|
||||
Time k = c;
|
||||
WString t = d;
|
||||
|
||||
DUMP(i);
|
||||
DUMP(j);
|
||||
DUMP(k);
|
||||
DUMP(t);
|
||||
|
||||
/// To determine type of value stored in `Value`, you can use `Is` method:
|
||||
|
||||
DUMP(a.Is<int>());
|
||||
DUMP(a.Is<double>());
|
||||
DUMP(b.Is<double>());
|
||||
DUMP(c.Is<int>());
|
||||
DUMP(c.Is<Date>());
|
||||
DUMP(d.Is<String>());
|
||||
|
||||
/// Note that Is tests for absolute type match, not for compatible types. For that reason,
|
||||
/// for widely used compatible types helper functions are defined:
|
||||
|
||||
DUMP(IsNumber(a));
|
||||
DUMP(IsNumber(b));
|
||||
DUMP(IsDateTime(c));
|
||||
DUMP(IsString(d));
|
||||
|
||||
///
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue