Skip to content

Instantly share code, notes, and snippets.

@ailabs-software
Last active June 22, 2020 17:49
Show Gist options
  • Save ailabs-software/6cf267d0ece10039e186c1e11940b75f to your computer and use it in GitHub Desktop.
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.
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