Last active
June 22, 2020 17:49
-
-
Save ailabs-software/6cf267d0ece10039e186c1e11940b75f to your computer and use it in GitHub Desktop.
Using type of property key argument's type argument to infer T, and statically check that value type matches.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LayoutBox { } | |
class RgbColour { } | |
class LayoutProperty<T> | |
{ | |
static final LayoutProperty<LayoutBox> margin = new LayoutProperty<LayoutBox>(); | |
static final LayoutProperty<RgbColour> colour = new LayoutProperty<RgbColour>(); | |
static final LayoutProperty<int> gridGap = new LayoutProperty<int>(); | |
static final List< LayoutProperty<Object> > values = [margin, colour, gridGap]; | |
} | |
class LayoutStyleModel2 | |
{ | |
T getProperty<T>(LayoutProperty<T> property) | |
{ | |
T value = null; | |
return value; | |
} | |
void setProperty< T extends LayoutProperty<V>, V>(T property, V value) | |
{ | |
print(property); | |
print(value); | |
} | |
} | |
void main() | |
{ | |
LayoutStyleModel2 model = new LayoutStyleModel2(); | |
model.setProperty(LayoutProperty.margin, LayoutBox.DEFAULT_LAYOUT_BOX); | |
// Statically fails, as one expects | |
model.setProperty(LayoutProperty.colour, LayoutBox.DEFAULT_LAYOUT_BOX); | |
// Statically fails, as one expects | |
RgbColour myValue = model.getProperty(LayoutProperty.margin); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment